From: rkinyon Date: Fri, 9 Nov 2007 15:59:00 +0000 (+0000) Subject: Added BrowserUk's changes so that the tests work in Win32. Have verified that they... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ad7645e943bc122d8727844baef9f90f001e0a41;p=dbsrgits%2FDBM-Deep.git Added BrowserUk's changes so that the tests work in Win32. Have verified that they work in OSX, will test Win32 via Parallels soon. --- diff --git a/t/04_array.t b/t/04_array.t index e4616ee..24b52ec 100644 --- a/t/04_array.t +++ b/t/04_array.t @@ -10,8 +10,9 @@ use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new( - file => $filename, - type => DBM::Deep->TYPE_ARRAY + file => $filename, + fh => $fh, + type => DBM::Deep->TYPE_ARRAY ); ## @@ -62,7 +63,7 @@ is( $db->fetch(4), 'elem4.1' ); throws_ok { $db->[-6] = 'whoops!'; -} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown"; +} qr/Modification of non-creatable array value attempted, subscript -6/, "Correct error thrown"; my $popped = $db->pop; is( $db->length, 4, "... and we have four after popping" ); @@ -251,6 +252,7 @@ throws_ok { my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new( file => $filename, + fh => $fh, type => DBM::Deep->TYPE_ARRAY ); diff --git a/t/07_locking.t b/t/07_locking.t index b36086c..004e03b 100644 --- a/t/07_locking.t +++ b/t/07_locking.t @@ -10,8 +10,9 @@ use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new( - file => $filename, - locking => 1, + file => $filename, + fh => $fh, + locking => 1, ); lives_ok { diff --git a/t/11_optimize.t b/t/11_optimize.t index 0ae0ed8..07b1130 100644 --- a/t/11_optimize.t +++ b/t/11_optimize.t @@ -3,14 +3,15 @@ ## use strict; use Test::More tests => 9; -use File::Temp qw( tmpnam ); + +use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); -my $filename = tmpnam(); +my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new( - file => $filename, - autoflush => 1, + file => $filename, + autoflush => 1, ); ## @@ -53,19 +54,22 @@ ok( $after < $before, "file size has shrunk" ); # make sure file shrunk is( $db->{key1}, 'value1', "key1's value is still there after optimize" ); is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" ); +$db->_get_self->_storage->close( $db->_get_self ); + ## # now for the tricky one -- try to store a new key while file is being -# optimized and locked by another process. filehandle should be invalidated, -# and automatically re-opened transparently. Cannot test on Win32, due to +# optimized and locked by another process. filehandle should be invalidated, +# and automatically re-opened transparently. Cannot test on Win32, due to # problems with fork()ing, flock()ing, etc. Win32 very bad. ## SKIP: { + skip "Fork tests skipped until fh/filename question solved.", 4; skip "Fork tests skipped on Win32", 4 if $^O eq 'MSWin32' || $^O eq 'cygwin'; ## - # first things first, get us about 1000 keys so the optimize() will take + # first things first, get us about 1000 keys so the optimize() will take # at least a few seconds on any machine, and re-open db with locking ## for (1..1000) { $db->STORE( $_, $_ +1 ); } @@ -78,14 +82,14 @@ SKIP: { unless ( $pid ) { # child fork - + # re-open db $db = DBM::Deep->new( file => $filename, autoflush => 1, locking => 1 ); - + # optimize and exit $db->optimize(); @@ -93,7 +97,7 @@ SKIP: { } # parent fork ok( defined($pid), "fork was successful" ); # make sure fork was successful - + # re-open db $db = DBM::Deep->new( file => $filename, @@ -103,10 +107,10 @@ SKIP: { # sleep for 1 second to make sure optimize() is running in the other fork sleep(1); - + # now, try to get a lock and store a key $db->{parentfork} = "hello"; - + # see if it was stored successfully is( $db->{parentfork}, "hello", "stored key while optimize took place" ); @@ -116,7 +120,7 @@ SKIP: { autoflush => 1, locking => 1 ); - + # now check some existing values from before is( $db->{key1}, 'value1', "key1's value is still there after optimize" ); is( $db->{a}{c}, 'value2', "key2's value is still there after optimize" ); diff --git a/t/16_circular.t b/t/16_circular.t index 501435d..9752816 100644 --- a/t/16_circular.t +++ b/t/16_circular.t @@ -8,7 +8,7 @@ use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); -my $db = DBM::Deep->new( $filename ); +my $db = DBM::Deep->new( file => $filename, fh => $fh, ); ## # put/get simple keys diff --git a/t/17_import.t b/t/17_import.t index c5e034e..108aae2 100644 --- a/t/17_import.t +++ b/t/17_import.t @@ -14,6 +14,7 @@ foreach my $type ( DBM::Deep->TYPE_HASH, DBM::Deep->TYPE_ARRAY ) { my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new({ file => $filename, + fh => $fh, type => $type, }); @@ -49,6 +50,7 @@ foreach my $type ( DBM::Deep->TYPE_HASH, DBM::Deep->TYPE_ARRAY ) { my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new({ file => $filename, + fh => $fh, autobless => 1, }); @@ -96,6 +98,7 @@ foreach my $type ( DBM::Deep->TYPE_HASH, DBM::Deep->TYPE_ARRAY ) { my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new({ file => $filename, + fh => $fh, type => DBM::Deep->TYPE_ARRAY, }); @@ -129,6 +132,7 @@ foreach my $type ( DBM::Deep->TYPE_HASH, DBM::Deep->TYPE_ARRAY ) { my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new({ file => $filename, + fh => $fh, autobless => 1, }); @@ -137,7 +141,7 @@ foreach my $type ( DBM::Deep->TYPE_HASH, DBM::Deep->TYPE_ARRAY ) { my $x; my $struct = { key1 => [ - 2, \$x, 3, + 2, \$x, 3, ], }; diff --git a/t/18_export.t b/t/18_export.t index 949697a..1ff8051 100644 --- a/t/18_export.t +++ b/t/18_export.t @@ -27,6 +27,7 @@ my %struct = ( my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new({ file => $filename, + fh => $fh, autobless => 1, }); diff --git a/t/19_crossref.t b/t/19_crossref.t index 67a3589..ff99319 100644 --- a/t/19_crossref.t +++ b/t/19_crossref.t @@ -9,7 +9,7 @@ use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); my ($fh2, $filename2) = new_fh(); -my $db2 = DBM::Deep->new( $filename2 ); +my $db2 = DBM::Deep->new( file => $filename2, fh => $fh2, ); SKIP: { skip "Apparently, we cannot detect a tied scalar?", 1; @@ -35,7 +35,7 @@ SKIP: { { my ($fh, $filename) = new_fh(); - my $db = DBM::Deep->new( $filename ); + my $db = DBM::Deep->new( file => $filename, fh => $fh, ); ## # Create structure in $db diff --git a/t/21_tie_access.t b/t/21_tie_access.t index 29a5ef6..4059fd4 100644 --- a/t/21_tie_access.t +++ b/t/21_tie_access.t @@ -16,6 +16,7 @@ my ($fh, $filename) = new_fh(); $hash{key1} = 'value'; is( $hash{key1}, 'value', 'Set and retrieved key1' ); + tied( %hash )->_get_self->_storage->close( tied( %hash )->_get_self ); } { @@ -26,6 +27,7 @@ my ($fh, $filename) = new_fh(); is( keys %hash, 1, "There's one key so far" ); ok( exists $hash{key1}, "... and it's key1" ); + tied( %hash )->_get_self->_storage->close( tied( %hash )->_get_self ); } { @@ -34,12 +36,13 @@ my ($fh, $filename) = new_fh(); file => $filename, type => DBM::Deep->TYPE_ARRAY, }; + tied( @array )->_get_self->_storage->close( tied( @array )->_get_self ); } qr/DBM::Deep: File type mismatch/, "\$SIG_TYPE doesn't match file's type"; } { my ($fh, $filename) = new_fh(); - DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_ARRAY ); + my $db = DBM::Deep->new( file => $filename, type => DBM::Deep->TYPE_ARRAY ); throws_ok { tie my %hash, 'DBM::Deep', { @@ -47,4 +50,5 @@ my ($fh, $filename) = new_fh(); type => DBM::Deep->TYPE_HASH, }; } qr/DBM::Deep: File type mismatch/, "\$SIG_TYPE doesn't match file's type"; + $db->_get_self->_storage->close( $db->_get_self ); } diff --git a/t/22_internal_copy.t b/t/22_internal_copy.t index f1a51a5..0988f8d 100644 --- a/t/22_internal_copy.t +++ b/t/22_internal_copy.t @@ -8,16 +8,16 @@ use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); -my $db = DBM::Deep->new( $filename ); +my $db = DBM::Deep->new( file => $filename, fh => $fh, ); ## # Create structure in $db ## $db->import({ - hash1 => { - subkey1 => "subvalue1", - subkey2 => "subvalue2", - }, + hash1 => { + subkey1 => "subvalue1", + subkey2 => "subvalue2", + }, hash2 => { subkey3 => 'subvalue3', }, @@ -49,16 +49,19 @@ my $max_keys = 1000; my ($fh2, $filename2) = new_fh(); { - my $db = DBM::Deep->new( $filename2 ); + my $db = DBM::Deep->new( file => $filename2, fh => $fh2, ); $db->{foo} = [ 1 .. 3 ]; for ( 0 .. $max_keys ) { $db->{'foo' . $_} = $db->{foo}; } + ## Rewind handle otherwise the signature is not recognised below. + ## The signature check should probably rewind the fh? + seek $db->_get_self->_storage->{fh}, 0, 0; } { - my $db = DBM::Deep->new( $filename2 ); + my $db = DBM::Deep->new( fh => $fh2, ); my $base_offset = $db->{foo}->_base_offset; my $count = -1; diff --git a/t/24_autobless.t b/t/24_autobless.t index 251fc7e..e2de696 100644 --- a/t/24_autobless.t +++ b/t/24_autobless.t @@ -16,6 +16,7 @@ my ($fh, $filename) = new_fh(); { my $db = DBM::Deep->new( file => $filename, + fh => $fh, autobless => 1, ); @@ -52,6 +53,7 @@ my ($fh, $filename) = new_fh(); is( $db->{unblessed}{b}[2], 3 ); $db->{blessed_long} = bless {}, 'a' x 1000; + $db->_get_self->_storage->close( $db->_get_self ); } { @@ -87,6 +89,7 @@ my ($fh, $filename) = new_fh(); is( $db->{blessed}{c}, 'new' ); isa_ok( $db->{blessed_long}, 'a' x 1000 ); + $db->_get_self->_storage->close( $db->_get_self ); } { @@ -98,7 +101,7 @@ my ($fh, $filename) = new_fh(); my $structure = $db->export(); use Data::Dumper;print Dumper $structure; - + my $obj = $structure->{blessed}; isa_ok( $obj, 'Foo' ); can_ok( $obj, 'export', 'foo' ); @@ -121,6 +124,7 @@ my ($fh, $filename) = new_fh(); is( $structure->{unblessed}{b}[0], 1 ); is( $structure->{unblessed}{b}[1], 2 ); is( $structure->{unblessed}{b}[2], 3 ); + $db->_get_self->_storage->close( $db->_get_self ); } { @@ -151,6 +155,7 @@ my ($fh, $filename) = new_fh(); is( $db->{unblessed}{b}[0], 1 ); is( $db->{unblessed}{b}[1], 2 ); is( $db->{unblessed}{b}[2], 3 ); + $db->_get_self->_storage->close( $db->_get_self ); } { @@ -158,6 +163,7 @@ my ($fh, $filename) = new_fh(); { my $db = DBM::Deep->new( file => $filename2, + fh => $fh2, autobless => 1, ); my $obj = bless { @@ -166,6 +172,7 @@ my ($fh, $filename) = new_fh(); }, 'Foo'; $db->import( { blessed => $obj } ); + $db->_get_self->_storage->close( $db->_get_self ); } { @@ -177,18 +184,20 @@ my ($fh, $filename) = new_fh(); my $blessed = $db->{blessed}; isa_ok( $blessed, 'Foo' ); is( $blessed->{a}, 1 ); + $db->_get_self->_storage->close( $db->_get_self ); } } { - ## - # test blessing hash into short named class (Foo), then re-blessing into - # longer named class (FooFoo) and replacing key in db file, then validating - # content after that point in file to check for corruption. - ## + ## + # test blessing hash into short named class (Foo), then re-blessing into + # longer named class (FooFoo) and replacing key in db file, then validating + # content after that point in file to check for corruption. + ## my ($fh3, $filename3) = new_fh(); my $db = DBM::Deep->new( file => $filename3, + fh => $fh3, autobless => 1, ); @@ -196,9 +205,9 @@ my ($fh, $filename) = new_fh(); $db->{blessed} = $obj; $db->{after} = "hello"; - + my $obj2 = bless {}, 'FooFoo'; - + $db->{blessed} = $obj2; is( $db->{after}, "hello" ); diff --git a/t/31_references.t b/t/31_references.t index af9bc30..4d46796 100644 --- a/t/31_references.t +++ b/t/31_references.t @@ -8,7 +8,7 @@ use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); -my $db = DBM::Deep->new( $filename ); +my $db = DBM::Deep->new( file => $filename, fh => $fh, ); my %hash = ( foo => 1, diff --git a/t/38_data_sector_size.t b/t/38_data_sector_size.t index 8414066..ebdbff8 100644 --- a/t/38_data_sector_size.t +++ b/t/38_data_sector_size.t @@ -6,6 +6,18 @@ use Test::More tests => 8; use t::common qw( new_fh ); +sub do_stuff { + my ($db) = @_; + + $db->{foo}{bar} = [ 1 .. 3 ]; +} + +sub verify { + my ($db) = @_; + + cmp_ok( $db->{foo}{bar}[2], '==', 3, "Correct value found" ); +} + use_ok( 'DBM::Deep' ); my %sizes; @@ -15,6 +27,7 @@ my %sizes; { my $db = DBM::Deep->new( file => $filename, + fh => $fh, data_sector_size => 32, ); @@ -24,8 +37,9 @@ my %sizes; $sizes{32} = -s $filename; { - my $db = DBM::Deep->new( $filename ); + my $db = DBM::Deep->new( file => $filename ); verify( $db ); + $db->_get_self->_storage->close( $db->_get_self ); } } @@ -34,6 +48,7 @@ my %sizes; { my $db = DBM::Deep->new( file => $filename, + fh => $fh, data_sector_size => 64, ); @@ -45,6 +60,7 @@ my %sizes; { my $db = DBM::Deep->new( $filename ); verify( $db ); + $db->_get_self->_storage->close( $db->_get_self ); } } @@ -53,6 +69,7 @@ my %sizes; { my $db = DBM::Deep->new( file => $filename, + fh => $fh, data_sector_size => 128, ); @@ -64,6 +81,7 @@ my %sizes; { my $db = DBM::Deep->new( $filename ); verify( $db ); + $db->_get_self->_storage->close( $db->_get_self ); } } @@ -72,6 +90,7 @@ my %sizes; { my $db = DBM::Deep->new( file => $filename, + fh => $fh, data_sector_size => 256, ); @@ -83,6 +102,7 @@ my %sizes; { my $db = DBM::Deep->new( $filename ); verify( $db ); + $db->_get_self->_storage->close( $db->_get_self ); } } @@ -90,14 +110,3 @@ cmp_ok( $sizes{256}, '>', $sizes{128}, "Filesize for 256 > filesize for 128" ); cmp_ok( $sizes{128}, '>', $sizes{64}, "Filesize for 128 > filesize for 64" ); cmp_ok( $sizes{64}, '>', $sizes{32}, "Filesize for 64 > filesize for 32" ); -sub do_stuff { - my ($db) = @_; - - $db->{foo}{bar} = [ 1 .. 3 ]; -} - -sub verify { - my ($db) = @_; - - cmp_ok( $db->{foo}{bar}[2], '==', 3, "Correct value found" ); -} diff --git a/t/40_freespace.t b/t/40_freespace.t index 7b0645d..d242710 100644 --- a/t/40_freespace.t +++ b/t/40_freespace.t @@ -12,6 +12,7 @@ use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new({ file => $filename, + fh => $fh, autoflush => 1, }); @@ -63,6 +64,7 @@ use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new({ file => $filename, + fh => $fh, autoflush => 1, }); @@ -74,7 +76,7 @@ use_ok( 'DBM::Deep' ); # trigger a reindex. This requires knowing how much space is taken. Good thing # we wrote this dreck ... my $size = -s $filename; - + my $data_sector_size = $db->_engine->data_sector_size; my $expected = $size + 9 * ( 2 * $data_sector_size ); diff --git a/t/41_transaction_multilevel.t b/t/41_transaction_multilevel.t index 3351e98..b8985e1 100644 --- a/t/41_transaction_multilevel.t +++ b/t/41_transaction_multilevel.t @@ -8,13 +8,16 @@ use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); my $db1 = DBM::Deep->new( file => $filename, + fh => $fh, locking => 1, autoflush => 1, num_txns => 2, ); +seek $db1->_get_self->_storage->{fh}, 0, 0; my $db2 = DBM::Deep->new( file => $filename, + fh => $fh, locking => 1, autoflush => 1, num_txns => 2, @@ -78,3 +81,6 @@ cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" ); cmp_bag( [ keys %{$db1->{x}} ], [qw( bar )], "DB1->X keys correct" ); cmp_bag( [ keys %{$db2->{x}} ], [qw( bar )], "DB2->X keys correct" ); + +$db1->_get_self->_storage->close( $db1->_get_self ); +$db2->_get_self->_storage->close( $db2->_get_self ); diff --git a/t/45_references.t b/t/45_references.t index d39ba0a..3ed2407 100644 --- a/t/45_references.t +++ b/t/45_references.t @@ -11,13 +11,17 @@ use_ok( 'DBM::Deep' ); my ($fh, $filename) = new_fh(); my $db = DBM::Deep->new( file => $filename, + fh => $fh, locking => 1, autoflush => 1, num_txns => 16, ); +seek $db->_get_self->_storage->{fh}, 0, 0; + my $db2 = DBM::Deep->new( file => $filename, + fh => $fh, locking => 1, autoflush => 1, num_txns => 16, diff --git a/t/common.pm b/t/common.pm index 2348cb9..38e232d 100644 --- a/t/common.pm +++ b/t/common.pm @@ -20,7 +20,7 @@ my $parent = $ENV{WORK_DIR} || File::Spec->tmpdir; my $dir = tempdir( CLEANUP => 1, DIR => $parent ); sub new_fh { - my ($fh, $filename) = tempfile( 'tmpXXXX', DIR => $dir ); + my ($fh, $filename) = tempfile( 'tmpXXXX', DIR => $dir, UNLINK => 1 ); # This is because tempfile() returns a flock'ed $fh on MacOSX. flock $fh, LOCK_UN;