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