7 plan skip_all => "Skipping the optimize tests on Win32/cygwin for now."
8 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' );
12 use t::common qw( new_fh );
14 use_ok( 'DBM::Deep' );
16 my ($fh, $filename) = new_fh();
17 my $db = DBM::Deep->new(
23 # create some unused space
25 $db->{key1} = "value1";
26 $db->{key2} = "value2";
30 $db->{a}{c} = 'value2';
32 my $b = $db->{a}->{b};
50 # take byte count readings before, and after optimize
52 my $before = (stat($filename))[7];
53 my $result = $db->optimize();
54 my $after = (stat($filename))[7];
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" );
62 $db->_get_self->_engine->storage->close( $db->_get_self );
65 # now for the tricky one -- try to store a new key while file is being
66 # optimized and locked by another process. filehandle should be invalidated,
67 # and automatically re-opened transparently. Cannot test on Win32, due to
68 # problems with fork()ing, flock()ing, etc. Win32 very bad.
72 skip "Fork tests skipped until fh/filename question solved.", 4;
73 skip "Fork tests skipped on Win32", 4
74 if $^O eq 'MSWin32' || $^O eq 'cygwin';
77 # first things first, get us about 1000 keys so the optimize() will take
78 # at least a few seconds on any machine, and re-open db with locking
80 for (1..1000) { $db->STORE( $_, $_ +1 ); }
84 # now, fork a process for the optimize()
104 ok( defined($pid), "fork was successful" ); # make sure fork was successful
107 $db = DBM::Deep->new(
113 # sleep for 1 second to make sure optimize() is running in the other fork
116 # now, try to get a lock and store a key
117 $db->{parentfork} = "hello";
119 # see if it was stored successfully
120 is( $db->{parentfork}, "hello", "stored key while optimize took place" );
123 $db = DBM::Deep->new(
129 # now check some existing values from before
130 is( $db->{key1}, 'value1', "key1's value is still there after optimize" );
131 is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" );