2 use warnings FATAL => 'all';
7 use t::common qw( new_dbm );
11 my $dbm_factory = new_dbm(
16 while ( my $dbm_maker = $dbm_factory->() ) {
17 my $db1 = $dbm_maker->();
18 my $db2 = $dbm_maker->();
21 is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
22 is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
24 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
25 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
29 } qr/Cannot rollback without an active transaction/, "Attempting to rollback without a transaction throws an error";
33 } qr/Cannot commit without an active transaction/, "Attempting to commit without a transaction throws an error";
39 } qr/Cannot begin_work within an active transaction/, "Attempting to begin_work within a transaction throws an error";
43 } "Rolling back an empty transaction is ok.";
45 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
46 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
52 } "Committing an empty transaction is ok.";
54 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
55 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
59 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
60 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
62 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
63 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
66 is( $db1->{x}, 'y', "Within DB1 transaction, DB1's X is still Y" );
67 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is now A" );
70 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
71 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is still A" );
74 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
75 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
77 $db2->{other_x} = 'foo';
78 is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
79 ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." );
81 # Reset to an expected value
83 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is istill Z" );
84 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is now Y" );
86 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
87 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
91 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
92 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
94 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
95 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
99 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
100 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
102 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
103 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
106 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
107 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
109 $db2->{other_x} = 'bar';
110 is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
111 is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
114 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
115 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
117 cmp_bag( [ keys %$db1 ], [qw( x other_x z )], "DB1 keys correct" );
118 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
122 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
123 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
125 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
126 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
128 is( $db1->{other_x}, 'bar', "After commit, DB1's other_x is bar" );
129 is( $db2->{other_x}, 'bar', "After commit, DB2's other_x is bar" );
133 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
134 cmp_bag( [ keys %$db2 ], [qw( x z other_x )], "DB2 keys correct" );
136 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
137 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
139 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
140 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
142 is( $db1->{other_x}, 'bar', "After begin_work, DB1's other_x is still bar" );
143 is( $db2->{other_x}, 'bar', "After begin_work, DB2's other_x is still bar" );
145 delete $db2->{other_x};
146 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
147 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
149 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
150 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
153 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( other_x z )], "DB1 keys correct" );
157 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
161 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
162 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
164 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
165 is( $db2->{x}, 'z', "DB2 can still see it" );
167 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
168 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
173 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
175 is( $db2->{x}, 'z', "But, DB2 can still see it" );
177 cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" );
178 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
182 ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
183 ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
186 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
187 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
189 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
190 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
194 %$db1 = (); # clear()
195 ok( !exists $db1->{foo}, "Cleared foo" );
196 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
198 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
199 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
203 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
204 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
206 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
207 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
210 skip "Optimize tests skipped on Win32", 7
211 if $^O eq 'MSWin32' || $^O eq 'cygwin';
215 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
216 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
218 is( $db1->{z}, 'a', 'After optimize, everything is ok' );
219 is( $db2->{z}, 'a', 'After optimize, everything is ok' );
221 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
222 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
226 cmp_ok( $db1->_engine->trans_id, '==', 1, "Transaction ID has been reset after optimize" );