Test::More 0.88 is required for done_testing
[dbsrgits/DBM-Deep.git] / t / 42_transaction_indexsector.t
1 use strict;
2 use Test::More tests => 81;
3 use Test::Deep;
4 use t::common qw( new_fh );
5
6 use_ok( 'DBM::Deep' );
7
8 # This testfile is in sections because the goal is to verify the behavior
9 # when a reindex occurs during an active transaction, both as a result of the
10 # transaction's actions as well as the result of the HEAD's actions. In order
11 # to keep this test quick, it's easier to restart and hit the known
12 # reindexing at 17 keys vs. attempting to hit the second-level reindex which
13 # can occur as early as 18 keys and as late as 4097 (256*16+1) keys.
14
15 {
16     my ($fh, $filename) = new_fh();
17     my $db1 = DBM::Deep->new(
18         file => $filename,
19         locking => 1,
20         autoflush => 1,
21         num_txns  => 16,
22     );
23
24     my $db2 = DBM::Deep->new(
25         file => $filename,
26         locking => 1,
27         autoflush => 1,
28         num_txns  => 16,
29     );
30
31     $db1->{x} = 'y';
32     is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
33     is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
34
35     $db1->begin_work;
36
37         cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
38         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
39
40         # Add enough keys to force a reindex
41         $db1->{"K$_"} = "V$_" for 1 .. 16;
42
43         cmp_bag( [ keys %$db1 ], ['x', (map { "K$_" } 1 .. 16)], "DB1 keys correct" );
44         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
45
46     $db1->rollback;
47
48     cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
49     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
50
51     ok( !exists $db1->{"K$_"}, "DB1: Key K$_ doesn't exist" ) for 1 .. 16;
52     ok( !exists $db2->{"K$_"}, "DB2: Key K$_ doesn't exist" ) for 1 .. 16;
53 }
54
55 {
56     my ($fh, $filename) = new_fh();
57     my $db1 = DBM::Deep->new(
58         file => $filename,
59         locking => 1,
60         autoflush => 1,
61         num_txns  => 16,
62     );
63
64     my $db2 = DBM::Deep->new(
65         file => $filename,
66         locking => 1,
67         autoflush => 1,
68         num_txns  => 16,
69     );
70
71     $db1->{x} = 'y';
72     is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
73     is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
74
75     $db1->begin_work;
76
77         cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
78         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
79
80         # Add enough keys to force a reindex
81         $db1->{"K$_"} = "V$_" for 1 .. 16;
82
83         cmp_bag( [ keys %$db1 ], ['x', (map { "K$_" } 1 .. 16)], "DB1 keys correct" );
84         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
85
86     $db1->commit;
87
88     cmp_bag( [ keys %$db1 ], ['x', (map { "K$_" } 1 .. 16)], "DB1 keys correct" );
89     cmp_bag( [ keys %$db2 ], ['x', (map { "K$_" } 1 .. 16)], "DB2 keys correct" );
90
91     ok( exists $db1->{"K$_"}, "DB1: Key K$_ doesn't exist" ) for 1 .. 16;
92     ok( exists $db2->{"K$_"}, "DB2: Key K$_ doesn't exist" ) for 1 .. 16;
93 }