2 use Test::More tests => 91;
4 use t::common qw( new_fh );
8 my ($fh, $filename) = new_fh();
9 my $db1 = DBM::Deep->new(
16 my $db2 = DBM::Deep->new(
24 is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
25 is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
27 eval { $db1->rollback };
28 ok( $@, "Attempting to rollback without a transaction throws an error" );
30 eval { $db1->commit };
31 ok( $@, "Attempting to commit without a transaction throws an error" );
35 eval { $db1->begin_work };
36 ok( $@, "Attempting to begin_work within a transaction throws an error" );
38 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
39 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
41 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
42 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
45 is( $db1->{x}, 'y', "Within DB1 transaction, DB1's X is still Y" );
46 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is now A" );
49 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
50 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is still A" );
53 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
54 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
56 $db2->{other_x} = 'foo';
57 is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
58 ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." );
60 # Reset to an expected value
62 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is istill Z" );
63 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is now Y" );
65 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
66 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
70 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
71 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
73 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
74 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
78 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
79 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
81 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
82 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
85 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
86 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
88 $db2->{other_x} = 'bar';
89 is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
90 is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
93 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
94 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
96 cmp_bag( [ keys %$db1 ], [qw( x other_x z )], "DB1 keys correct" );
97 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
101 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
102 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
104 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
105 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
107 is( $db1->{other_x}, 'bar', "After commit, DB1's other_x is bar" );
108 is( $db2->{other_x}, 'bar', "After commit, DB2's other_x is bar" );
112 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
113 cmp_bag( [ keys %$db2 ], [qw( x z other_x )], "DB2 keys correct" );
115 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
116 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
118 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
119 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
121 is( $db1->{other_x}, 'bar', "After begin_work, DB1's other_x is still bar" );
122 is( $db2->{other_x}, 'bar', "After begin_work, DB2's other_x is still bar" );
124 delete $db2->{other_x};
125 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
126 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
128 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
129 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
132 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
133 is( $db2->{x}, 'z', "But, DB2 can still see it" );
135 cmp_bag( [ keys %$db1 ], [qw( other_x z )], "DB1 keys correct" );
136 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
140 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
141 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
143 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
144 is( $db2->{x}, 'z', "DB2 can still see it" );
146 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
147 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
152 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
154 is( $db2->{x}, 'z', "But, DB2 can still see it" );
156 cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" );
157 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
161 ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
162 ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
165 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
166 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
168 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
169 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
173 %$db1 = (); # clear()
174 ok( !exists $db1->{foo}, "Cleared foo" );
175 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
177 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
178 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
182 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
183 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
185 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
186 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
189 skip "Optimize tests skipped on Win32", 5
190 if $^O eq 'MSWin32' || $^O eq 'cygwin';
194 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
195 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
197 is( $db1->{z}, 'a', 'After optimize, everything is ok' );
198 is( $db2->{z}, 'a', 'After optimize, everything is ok' );
200 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
201 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
205 cmp_ok( $db1->_engine->trans_id, '==', 1, "Transaction ID has been reset after optimize" );
213 * Two transactions running at the same time
214 * Doing a clear on the head while a transaction is running