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