Fixed problem with second-level values being overwritten when accessed.
[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
0e3e3555 11my $dbm_factory = new_dbm(
21838116 12 locking => 1,
c9b6d0d8 13 autoflush => 1,
2120a181 14 num_txns => 16,
21838116 15);
0e3e3555 16while ( my $dbm_maker = $dbm_factory->() ) {
17 my $db1 = $dbm_maker->();
18 my $db2 = $dbm_maker->();
21838116 19
0e3e3555 20 $db1->{x} = 'y';
21 is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
22 is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
21838116 23
0e3e3555 24 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
25 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
2120a181 26
0e3e3555 27 throws_ok {
28 $db1->rollback;
29 } qr/Cannot rollback without an active transaction/, "Attempting to rollback without a transaction throws an error";
2120a181 30
0e3e3555 31 throws_ok {
32 $db1->commit;
33 } qr/Cannot commit without an active transaction/, "Attempting to commit without a transaction throws an error";
21838116 34
2120a181 35 $db1->begin_work;
2120a181 36
0e3e3555 37 throws_ok {
38 $db1->begin_work;
39 } qr/Cannot begin_work within an active transaction/, "Attempting to begin_work within a transaction throws an error";
2120a181 40
0e3e3555 41 lives_ok {
42 $db1->rollback;
43 } "Rolling back an empty transaction is ok.";
2120a181 44
45 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
46 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
47
0e3e3555 48 $db1->begin_work;
21838116 49
0e3e3555 50 lives_ok {
51 $db1->commit;
52 } "Committing an empty transaction is ok.";
2120a181 53
0e3e3555 54 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
55 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
2120a181 56
0e3e3555 57 $db1->begin_work;
21838116 58
0e3e3555 59 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
60 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
21838116 61
0e3e3555 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" );
2120a181 64
0e3e3555 65 $db2->{x} = 'a';
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" );
42717e46 68
0e3e3555 69 $db1->{x} = 'z';
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" );
21838116 72
0e3e3555 73 $db1->{z} = '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." );
21838116 76
0e3e3555 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." );
460b1067 80
0e3e3555 81 # Reset to an expected value
82 $db2->{x} = 'y';
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" );
6677de37 85
0e3e3555 86 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
87 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
2120a181 88
0e3e3555 89 $db1->rollback;
633df1fd 90
0e3e3555 91 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
92 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
6677de37 93
0e3e3555 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" );
504185fb 96
0e3e3555 97 $db1->begin_work;
2120a181 98
0e3e3555 99 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
100 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
42717e46 101
0e3e3555 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" );
6677de37 104
0e3e3555 105 $db1->{x} = 'z';
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" );
6677de37 108
0e3e3555 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." );
2120a181 112
0e3e3555 113 $db1->{z} = 'a';
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." );
2120a181 116
0e3e3555 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" );
6677de37 119
0e3e3555 120 $db1->commit;
2120a181 121
122 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
123 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
124
125 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
126 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
127
0e3e3555 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" );
130
131 $db1->begin_work;
2120a181 132
0e3e3555 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" );
6677de37 135
0e3e3555 136 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
137 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
42717e46 138
0e3e3555 139 is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
140 is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
94e8af14 141
0e3e3555 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" );
42717e46 144
0e3e3555 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." );
6677de37 148
0e3e3555 149 cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
150 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
6677de37 151
0e3e3555 152 delete $db1->{x};
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" );
6677de37 155
0e3e3555 156 cmp_bag( [ keys %$db1 ], [qw( other_x z )], "DB1 keys correct" );
157 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
42717e46 158
0e3e3555 159 $db1->rollback;
6677de37 160
0e3e3555 161 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
162 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
2120a181 163
0e3e3555 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" );
6677de37 166
0e3e3555 167 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
2120a181 168 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
42717e46 169
0e3e3555 170 $db1->begin_work;
94e8af14 171
0e3e3555 172 delete $db1->{x};
173 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
94e8af14 174
0e3e3555 175 is( $db2->{x}, 'z', "But, DB2 can still see it" );
42717e46 176
0e3e3555 177 cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" );
178 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
94e8af14 179
0e3e3555 180 $db1->commit;
94e8af14 181
0e3e3555 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" );
42717e46 184
0e3e3555 185 $db1->{foo} = 'bar';
186 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
187 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
94e8af14 188
0e3e3555 189 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
190 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 191
0e3e3555 192 $db1->begin_work;
42717e46 193
0e3e3555 194 %$db1 = (); # clear()
195 ok( !exists $db1->{foo}, "Cleared foo" );
196 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
f9a320bb 197
0e3e3555 198 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
199 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 200
0e3e3555 201 $db1->rollback;
42717e46 202
0e3e3555 203 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
204 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
2120a181 205
206 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
207 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 208
0e3e3555 209 SKIP: {
210 skip "Optimize tests skipped on Win32", 7
211 if $^O eq 'MSWin32' || $^O eq 'cygwin';
42717e46 212
0e3e3555 213 $db1->optimize;
13ff93d5 214
0e3e3555 215 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
216 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
217
218 is( $db1->{z}, 'a', 'After optimize, everything is ok' );
219 is( $db2->{z}, 'a', 'After optimize, everything is ok' );
220
221 cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
222 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
223
224 $db1->begin_work;
225
226 cmp_ok( $db1->_engine->trans_id, '==', 1, "Transaction ID has been reset after optimize" );
227
228 $db1->rollback;
229 }
13ff93d5 230}
42717e46 231
0e3e3555 232done_testing;