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