5 use Test::More tests => 9;
6 use File::Temp qw( tmpnam );
10 my $filename = tmpnam();
11 my $db = DBM::Deep->new(
17 # create some unused space
19 $db->{key1} = "value1";
20 $db->{key2} = "value2";
24 $db->{a}{c} = 'value2';
26 my $b = $db->{a}->{b};
44 # take byte count readings before, and after optimize
46 my $before = (stat($db->_fh()))[7];
47 my $result = $db->optimize();
48 my $after = (stat($db->_fh()))[7];
50 ok( $result, "optimize succeeded" );
51 ok( $after < $before, "file size has shrunk" ); # make sure file shrunk
53 is( $db->{key1}, 'value1', "key1's value is still there after optimize" );
54 is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" );
56 #print keys %{$db->{a}}, $/;
59 # now for the tricky one -- try to store a new key while file is being
60 # optimized and locked by another process. filehandle should be invalidated,
61 # and automatically re-opened transparently. Cannot test on Win32, due to
62 # problems with fork()ing, flock()ing, etc. Win32 very bad.
66 skip "Fork tests skipped on Win32", 4
67 if $^O eq 'MSWin32' || $^O eq 'cygwin';
70 # first things first, get us about 1000 keys so the optimize() will take
71 # at least a few seconds on any machine, and re-open db with locking
73 for (1..1000) { $db->STORE( $_, $_ ); }
77 # now, fork a process for the optimize()
98 ok( defined($pid), "fork was successful" ); # make sure fork was successful
101 $db = DBM::Deep->new(
107 # sleep for 1 second to make sure optimize() is running in the other fork
110 # now, try to get a lock and store a key
111 $db->{parentfork} = "hello";
113 # see if it was stored successfully
114 is( $db->{parentfork}, "hello", "stored key while optimize took place" );
117 # $db = DBM::Deep->new(
123 # now check some existing values from before
124 is( $db->{key1}, 'value1', "key1's value is still there after optimize" );
125 is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" );