r14427@Rob-Kinyons-PowerBook: rob | 2006-06-19 09:14:51 -0400
[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
e06824f8 56#print keys %{$db->{a}}, $/;
57
a59a8dca 58##
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.
63##
64
656abae9 65SKIP: {
13ff93d5 66 skip "Fork tests skipped on Win32", 4
67 if $^O eq 'MSWin32' || $^O eq 'cygwin';
656abae9 68
69 ##
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
72 ##
1ad1fc2b 73 for (11..11) { $db->STORE( $_, $_ +1 ); }
656abae9 74 undef $db;
75
76 ##
77 # now, fork a process for the optimize()
78 ##
79 my $pid = fork();
80
81 unless ( $pid ) {
82 # child fork
83
84 # re-open db
85 $db = DBM::Deep->new(
2a81bf9e 86 file => $filename,
656abae9 87 autoflush => 1,
88 locking => 1
89 );
656abae9 90
91 # optimize and exit
92 $db->optimize();
93
94 exit( 0 );
95 }
1ad1fc2b 96=pod
656abae9 97 # parent fork
98 ok( defined($pid), "fork was successful" ); # make sure fork was successful
99
100 # re-open db
101 $db = DBM::Deep->new(
2a81bf9e 102 file => $filename,
656abae9 103 autoflush => 1,
104 locking => 1
105 );
1ad1fc2b 106
656abae9 107 # sleep for 1 second to make sure optimize() is running in the other fork
108 sleep(1);
109
110 # now, try to get a lock and store a key
111 $db->{parentfork} = "hello";
112
113 # see if it was stored successfully
114 is( $db->{parentfork}, "hello", "stored key while optimize took place" );
e06824f8 115
116# undef $db;
117# $db = DBM::Deep->new(
118# file => $filename,
119# autoflush => 1,
120# locking => 1
121# );
656abae9 122
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" );
1ad1fc2b 126=cut
a59a8dca 127}