All tests pass except for the transaction tests under MySQL. InnoDB sucks
[dbsrgits/DBM-Deep.git] / t / 33_transactions.t
CommitLineData
fee0243f 1use strict;
0e3e3555 2use warnings FATAL => 'all';
3
4use Test::More;
42717e46 5use Test::Deep;
2120a181 6use Test::Exception;
0e3e3555 7use t::common qw( new_dbm );
fee0243f 8
9use_ok( 'DBM::Deep' );
10
4f034d8f 11if ( $ENV{NO_TEST_TRANSACTIONS} ) {
12 done_testing;
13 exit;
14}
15
0e3e3555 16my $dbm_factory = new_dbm(
21838116 17 locking => 1,
c9b6d0d8 18 autoflush => 1,
2120a181 19 num_txns => 16,
21838116 20);
0e3e3555 21while ( my $dbm_maker = $dbm_factory->() ) {
22 my $db1 = $dbm_maker->();
23 my $db2 = $dbm_maker->();
21838116 24
0e3e3555 25 $db1->{x} = 'y';
26 is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
27 is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
21838116 28
0e3e3555 29 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
30 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
2120a181 31
0e3e3555 32 throws_ok {
33 $db1->rollback;
34 } qr/Cannot rollback without an active transaction/, "Attempting to rollback without a transaction throws an error";
2120a181 35
0e3e3555 36 throws_ok {
37 $db1->commit;
38 } qr/Cannot commit without an active transaction/, "Attempting to commit without a transaction throws an error";
21838116 39
2120a181 40 $db1->begin_work;
2120a181 41
0e3e3555 42 throws_ok {
43 $db1->begin_work;
44 } qr/Cannot begin_work within an active transaction/, "Attempting to begin_work within a transaction throws an error";
2120a181 45
0e3e3555 46 lives_ok {
47 $db1->rollback;
48 } "Rolling back an empty transaction is ok.";
2120a181 49
50 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
51 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
52
0e3e3555 53 $db1->begin_work;
21838116 54
0e3e3555 55 lives_ok {
56 $db1->commit;
57 } "Committing an empty transaction is ok.";
2120a181 58
0e3e3555 59 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
60 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
2120a181 61
0e3e3555 62 $db1->begin_work;
21838116 63
0e3e3555 64 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
65 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
21838116 66
0e3e3555 67 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
68 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
2120a181 69
0e3e3555 70 $db2->{x} = 'a';
71 is( $db1->{x}, 'y', "Within DB1 transaction, DB1's X is still Y" );
72 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is now A" );
42717e46 73
0e3e3555 74 $db1->{x} = 'z';
75 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
76 is( $db2->{x}, 'a', "Within DB1 transaction, DB2's X is still A" );
21838116 77
0e3e3555 78 $db1->{z} = 'a';
79 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
80 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
21838116 81
0e3e3555 82 $db2->{other_x} = 'foo';
83 is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
84 ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." );
460b1067 85
0e3e3555 86 # Reset to an expected value
87 $db2->{x} = 'y';
88 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is istill Z" );
89 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is now Y" );
6677de37 90
0e3e3555 91 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
92 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
2120a181 93
0e3e3555 94 $db1->rollback;
633df1fd 95
0e3e3555 96 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
97 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
6677de37 98
0e3e3555 99 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
100 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
504185fb 101
0e3e3555 102 $db1->begin_work;
2120a181 103
0e3e3555 104 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
105 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
42717e46 106
0e3e3555 107 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
108 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
6677de37 109
0e3e3555 110 $db1->{x} = 'z';
111 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
112 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
6677de37 113
0e3e3555 114 $db2->{other_x} = 'bar';
115 is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
116 is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
2120a181 117
0e3e3555 118 $db1->{z} = 'a';
119 is( $db1->{z}, 'a', "Within DB1 transaction, DB1's Z is A" );
120 ok( !exists $db2->{z}, "Since z was added after the transaction began, DB2 doesn't see it." );
2120a181 121
0e3e3555 122 cmp_bag( [ keys %$db1 ], [qw( x other_x z )], "DB1 keys correct" );
123 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
6677de37 124
0e3e3555 125 $db1->commit;
2120a181 126
127 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
128 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
129
130 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
131 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
132
0e3e3555 133 is( $db1->{other_x}, 'bar', "After commit, DB1's other_x is bar" );
134 is( $db2->{other_x}, 'bar', "After commit, DB2's other_x is bar" );
135
136 $db1->begin_work;
2120a181 137
0e3e3555 138 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
139 cmp_bag( [ keys %$db2 ], [qw( x z other_x )], "DB2 keys correct" );
6677de37 140
0e3e3555 141 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
142 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
42717e46 143
0e3e3555 144 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
145 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
94e8af14 146
0e3e3555 147 is( $db1->{other_x}, 'bar', "After begin_work, DB1's other_x is still bar" );
148 is( $db2->{other_x}, 'bar', "After begin_work, DB2's other_x is still bar" );
42717e46 149
0e3e3555 150 delete $db2->{other_x};
151 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
152 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
6677de37 153
0e3e3555 154 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
155 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
6677de37 156
0e3e3555 157 delete $db1->{x};
158 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
159 is( $db2->{x}, 'z', "But, DB2 can still see it" );
6677de37 160
0e3e3555 161 cmp_bag( [ keys %$db1 ], [qw( other_x z )], "DB1 keys correct" );
162 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
42717e46 163
0e3e3555 164 $db1->rollback;
6677de37 165
0e3e3555 166 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
167 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
2120a181 168
0e3e3555 169 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
170 is( $db2->{x}, 'z', "DB2 can still see it" );
6677de37 171
0e3e3555 172 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
2120a181 173 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
42717e46 174
0e3e3555 175 $db1->begin_work;
94e8af14 176
0e3e3555 177 delete $db1->{x};
178 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
94e8af14 179
0e3e3555 180 is( $db2->{x}, 'z', "But, DB2 can still see it" );
42717e46 181
0e3e3555 182 cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" );
183 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
94e8af14 184
0e3e3555 185 $db1->commit;
94e8af14 186
0e3e3555 187 ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
188 ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
42717e46 189
0e3e3555 190 $db1->{foo} = 'bar';
191 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
192 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
94e8af14 193
0e3e3555 194 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
195 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 196
0e3e3555 197 $db1->begin_work;
42717e46 198
0e3e3555 199 %$db1 = (); # clear()
200 ok( !exists $db1->{foo}, "Cleared foo" );
201 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
f9a320bb 202
0e3e3555 203 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
204 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 205
0e3e3555 206 $db1->rollback;
42717e46 207
0e3e3555 208 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
209 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
2120a181 210
211 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
212 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 213
0e3e3555 214 SKIP: {
215 skip "Optimize tests skipped on Win32", 7
216 if $^O eq 'MSWin32' || $^O eq 'cygwin';
42717e46 217
0e3e3555 218 $db1->optimize;
13ff93d5 219
0e3e3555 220 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
221 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
222
223 is( $db1->{z}, 'a', 'After optimize, everything is ok' );
224 is( $db2->{z}, 'a', 'After optimize, everything is ok' );
225
226 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
227 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
228
229 $db1->begin_work;
230
231 cmp_ok( $db1->_engine->trans_id, '==', 1, "Transaction ID has been reset after optimize" );
232
233 $db1->rollback;
234 }
13ff93d5 235}
42717e46 236
0e3e3555 237done_testing;