r15625@rob-kinyons-computer (orig r9171): rkinyon | 2007-02-26 11:56:32 -0500
[dbsrgits/DBM-Deep.git] / t / 11_optimize.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 9;
6 use File::Temp qw( tmpnam );
7
8 use_ok( 'DBM::Deep' );
9
10 my $filename = tmpnam();
11 my $db = DBM::Deep->new(
12         file => $filename,
13         autoflush => 1,
14 );
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
26 my $b = $db->{a}->{b};
27 $b->[0] = 1;
28 $b->[1] = 2;
29 $b->[2] = {};
30 $b->[2]->{c} = [];
31
32 my $c = $b->[2]->{c};
33 $c->[0] = 'd';
34 $c->[1] = {};
35 $c->[1]->{e} = 'f';
36
37 undef $c;
38 undef $b;
39
40 delete $db->{key2};
41 delete $db->{a}{b};
42
43 ##
44 # take byte count readings before, and after optimize
45 ##
46 my $before = (stat($db->_fh()))[7];
47 my $result = $db->optimize();
48 my $after = (stat($db->_fh()))[7];
49
50 ok( $result, "optimize succeeded" );
51 ok( $after < $before, "file size has shrunk" ); # make sure file shrunk
52
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" );
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
63 SKIP: {
64     skip "Fork tests skipped on Win32", 4
65         if $^O eq 'MSWin32' || $^O eq 'cygwin';
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     ##
71     for (1..1000) { $db->STORE( $_, $_ +1 ); }
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(
84             file => $filename,
85             autoflush => 1,
86             locking => 1
87         );
88         
89         # optimize and exit
90         $db->optimize();
91
92         exit( 0 );
93     }
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(
99         file => $filename,
100         autoflush => 1,
101         locking => 1
102     );
103
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" );
112
113     undef $db;
114     $db = DBM::Deep->new(
115         file => $filename,
116         autoflush => 1,
117         locking => 1
118     );
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" );
123 }