Added missing testfile
[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     );
22
23     my $db2 = DBM::Deep->new(
24         file => $filename,
25         locking => 1,
26         autoflush => 1,
27     );
28
29     $db1->{x} = 'y';
30     is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
31     is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
32
33     $db1->begin_work;
34
35         cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
36         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
37
38         # Add enough keys to force a reindex
39         $db1->{"K$_"} = "V$_" for 1 .. 16;
40
41         cmp_bag( [ keys %$db1 ], ['x', (map { "K$_" } 1 .. 16)], "DB1 keys correct" );
42         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
43
44     $db1->rollback;
45
46     cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
47     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
48
49     ok( !exists $db1->{"K$_"}, "DB1: Key K$_ doesn't exist" ) for 1 .. 16;
50     ok( !exists $db2->{"K$_"}, "DB2: Key K$_ doesn't exist" ) for 1 .. 16;
51 }
52
53 {
54     my ($fh, $filename) = new_fh();
55     my $db1 = DBM::Deep->new(
56         file => $filename,
57         locking => 1,
58         autoflush => 1,
59     );
60
61     my $db2 = DBM::Deep->new(
62         file => $filename,
63         locking => 1,
64         autoflush => 1,
65     );
66
67     $db1->{x} = 'y';
68     is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
69     is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
70
71     $db1->begin_work;
72
73         cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
74         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
75
76         # Add enough keys to force a reindex
77         $db1->{"K$_"} = "V$_" for 1 .. 16;
78
79         cmp_bag( [ keys %$db1 ], ['x', (map { "K$_" } 1 .. 16)], "DB1 keys correct" );
80         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
81
82     $db1->commit;
83
84     cmp_bag( [ keys %$db1 ], ['x', (map { "K$_" } 1 .. 16)], "DB1 keys correct" );
85     cmp_bag( [ keys %$db2 ], ['x', (map { "K$_" } 1 .. 16)], "DB2 keys correct" );
86
87     ok( exists $db1->{"K$_"}, "DB1: Key K$_ doesn't exist" ) for 1 .. 16;
88     ok( exists $db2->{"K$_"}, "DB2: Key K$_ doesn't exist" ) for 1 .. 16;
89 }