2 use Test::More tests => 62;
4 use t::common qw( new_fh );
8 my ($fh, $filename) = new_fh();
9 my $db1 = DBM::Deep->new(
15 my $db2 = DBM::Deep->new(
22 is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
23 is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
27 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
28 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
31 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
32 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
34 $db2->{other_x} = 'foo';
35 is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
36 ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." );
38 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
39 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
43 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
44 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
46 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
47 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
51 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
52 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
55 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
56 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
58 $db2->{other_x} = 'bar';
59 is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
60 is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
62 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
63 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
67 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
68 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
72 delete $db2->{other_x};
73 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
74 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
76 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
77 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
80 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
81 is( $db2->{x}, 'z', "But, DB2 can still see it" );
83 cmp_bag( [ keys %$db1 ], [qw( other_x )], "DB1 keys correct" );
84 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
88 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
89 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
91 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
92 is( $db2->{x}, 'z', "DB2 can still see it" );
94 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
95 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
100 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
102 is( $db2->{x}, 'z', "But, DB2 can still see it" );
104 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
105 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
109 ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
110 ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
113 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
114 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
116 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
117 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
121 %$db1 = (); # clear()
122 ok( !exists $db1->{foo}, "Cleared foo" );
123 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
125 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
126 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
130 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
131 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
133 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
134 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
137 skip "Optimize tests skipped on Win32", 5
138 if $^O eq 'MSWin32' || $^O eq 'cygwin';
142 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
143 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
145 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
146 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
150 cmp_ok( $db1->_fileobj->transaction_id, '==', 1, "Transaction ID has been reset after optimize" );
158 * Two transactions running at the same time
159 * Doing a clear on the head while a transaction is running
160 # More than just two keys