5 use Test::More tests => 9;
10 my $db = DBM::Deep->new(
15 die "ERROR: " . $db->error();
19 # create some unused space
21 $db->{key1} = "value1";
22 $db->{key2} = "value2";
26 $db->{a}{c} = 'value2';
28 my $b = $db->{a}->{b};
46 # take byte count readings before, and after optimize
48 my $before = (stat($db->_fh()))[7];
49 my $result = $db->optimize();
50 my $after = (stat($db->_fh()))[7];
53 die "ERROR: " . $db->error();
56 ok( $result, "optimize succeeded" );
57 ok( $after < $before, "file size has shrunk" ); # make sure file shrunk
59 is( $db->{key1}, 'value1', "key1's value is still there after optimize" );
60 is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" );
63 # now for the tricky one -- try to store a new key while file is being
64 # optimized and locked by another process. filehandle should be invalidated,
65 # and automatically re-opened transparently. Cannot test on Win32, due to
66 # problems with fork()ing, flock()ing, etc. Win32 very bad.
70 skip "Fork tests skipped on Win32", 4 if $^O eq 'MSWin32';
73 # first things first, get us about 1000 keys so the optimize() will take
74 # at least a few seconds on any machine, and re-open db with locking
76 for (1..1000) { $db->STORE( $_, $_ ); }
80 # now, fork a process for the optimize()
94 die "ERROR: " . $db->error();
104 ok( defined($pid), "fork was successful" ); # make sure fork was successful
107 $db = DBM::Deep->new(
113 die "ERROR: " . $db->error();
116 # sleep for 1 second to make sure optimize() is running in the other fork
119 # now, try to get a lock and store a key
120 $db->{parentfork} = "hello";
122 # see if it was stored successfully
123 is( $db->{parentfork}, "hello", "stored key while optimize took place" );
126 # now check some existing values from before
127 is( $db->{key1}, 'value1', "key1's value is still there after optimize" );
128 is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" );