r14214@rob-kinyons-computer (orig r8081): rkinyon | 2006-11-17 20:51:21 -0500
[dbsrgits/DBM-Deep.git] / t / 11_optimize.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
a59a8dca 5use Test::More tests => 9;
2a81bf9e 6use File::Temp qw( tmpnam );
ffed8b01 7
8use_ok( 'DBM::Deep' );
9
2a81bf9e 10my $filename = tmpnam();
ffed8b01 11my $db = DBM::Deep->new(
2a81bf9e 12 file => $filename,
ffed8b01 13 autoflush => 1,
14);
ffed8b01 15
16##
17# create some unused space
18##
19$db->{key1} = "value1";
20$db->{key2} = "value2";
21
22$db->{a} = {};
23$db->{a}{b} = [];
24$db->{a}{c} = 'value2';
25
26my $b = $db->{a}->{b};
27$b->[0] = 1;
28$b->[1] = 2;
29$b->[2] = {};
30$b->[2]->{c} = [];
31
32my $c = $b->[2]->{c};
33$c->[0] = 'd';
34$c->[1] = {};
35$c->[1]->{e} = 'f';
36
37undef $c;
38undef $b;
39
40delete $db->{key2};
41delete $db->{a}{b};
42
43##
44# take byte count readings before, and after optimize
45##
4d35d856 46my $before = (stat($db->_fh()))[7];
ffed8b01 47my $result = $db->optimize();
4d35d856 48my $after = (stat($db->_fh()))[7];
ffed8b01 49
a59a8dca 50ok( $result, "optimize succeeded" );
51ok( $after < $before, "file size has shrunk" ); # make sure file shrunk
ffed8b01 52
53is( $db->{key1}, 'value1', "key1's value is still there after optimize" );
54is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" );
a59a8dca 55
56##
57# now for the tricky one -- try to store a new key while file is being
58# optimized and locked by another process. filehandle should be invalidated,
59# and automatically re-opened transparently. Cannot test on Win32, due to
60# problems with fork()ing, flock()ing, etc. Win32 very bad.
61##
62
656abae9 63SKIP: {
13ff93d5 64 skip "Fork tests skipped on Win32", 4
65 if $^O eq 'MSWin32' || $^O eq 'cygwin';
656abae9 66
67 ##
68 # first things first, get us about 1000 keys so the optimize() will take
69 # at least a few seconds on any machine, and re-open db with locking
70 ##
9a63e1f2 71 for (1..1000) { $db->STORE( $_, $_ +1 ); }
656abae9 72 undef $db;
73
74 ##
75 # now, fork a process for the optimize()
76 ##
77 my $pid = fork();
78
79 unless ( $pid ) {
80 # child fork
81
82 # re-open db
83 $db = DBM::Deep->new(
2a81bf9e 84 file => $filename,
656abae9 85 autoflush => 1,
86 locking => 1
87 );
656abae9 88
89 # optimize and exit
90 $db->optimize();
91
92 exit( 0 );
93 }
656abae9 94 # parent fork
95 ok( defined($pid), "fork was successful" ); # make sure fork was successful
96
97 # re-open db
98 $db = DBM::Deep->new(
2a81bf9e 99 file => $filename,
656abae9 100 autoflush => 1,
101 locking => 1
102 );
1ad1fc2b 103
656abae9 104 # sleep for 1 second to make sure optimize() is running in the other fork
105 sleep(1);
106
107 # now, try to get a lock and store a key
108 $db->{parentfork} = "hello";
109
110 # see if it was stored successfully
111 is( $db->{parentfork}, "hello", "stored key while optimize took place" );
e06824f8 112
9a63e1f2 113 undef $db;
114 $db = DBM::Deep->new(
115 file => $filename,
116 autoflush => 1,
117 locking => 1
118 );
656abae9 119
120 # now check some existing values from before
121 is( $db->{key1}, 'value1', "key1's value is still there after optimize" );
122 is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" );
a59a8dca 123}