a4ca5c3bebe4fd9d23c678d91942fdd278642974
[dbsrgits/DBM-Deep.git] / t / 33_transactions.t
1 use strict;
2 use warnings FATAL => 'all';
3
4 use Test::More;
5 use Test::Deep;
6 use Test::Exception;
7 use t::common qw( new_dbm );
8
9 use_ok( 'DBM::Deep' );
10
11 my $dbm_factory = new_dbm(
12     locking => 1,
13     autoflush => 1,
14     num_txns  => 16,
15 );
16 while ( my $dbm_maker = $dbm_factory->() ) {
17     my $db1 = $dbm_maker->();
18     my $db2 = $dbm_maker->();
19
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" );
23
24     cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
25     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
26
27     throws_ok {
28         $db1->rollback;
29     } qr/Cannot rollback without an active transaction/, "Attempting to rollback without a transaction throws an error";
30
31     throws_ok {
32         $db1->commit;
33     } qr/Cannot commit without an active transaction/, "Attempting to commit without a transaction throws an error";
34
35     $db1->begin_work;
36
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";
40
41     lives_ok {
42         $db1->rollback;
43     } "Rolling back an empty transaction is ok.";
44
45     cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
46     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
47
48     $db1->begin_work;
49
50     lives_ok {
51         $db1->commit;
52     } "Committing an empty transaction is ok.";
53
54     cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
55     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
56
57     $db1->begin_work;
58
59         cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
60         cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
61
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" );
64
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" );
68
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" );
72
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." );
76
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." );
80
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" );
85
86         cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
87         cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
88
89     $db1->rollback;
90
91     is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
92     is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
93
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" );
96
97     $db1->begin_work;
98
99         cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
100         cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
101
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" );
104
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" );
108
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." );
112
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." );
116
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" );
119
120     $db1->commit;
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
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;
132
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" );
135
136         is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
137         is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
138
139         is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
140         is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
141
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" );
144
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." );
148
149         cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" );
150         cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
151
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" );
155
156         cmp_bag( [ keys %$db1 ], [qw( other_x z )], "DB1 keys correct" );
157         cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
158
159     $db1->rollback;
160
161     ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
162     ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
163
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" );
166
167     cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
168     cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
169
170     $db1->begin_work;
171
172         delete $db1->{x};
173         ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
174
175         is( $db2->{x}, 'z', "But, DB2 can still see it" );
176
177         cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" );
178         cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
179
180     $db1->commit;
181
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" );
184
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" );
188
189     cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
190     cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
191
192     $db1->begin_work;
193
194         %$db1 = (); # clear()
195         ok( !exists $db1->{foo}, "Cleared foo" );
196         is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
197
198         cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
199         cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
200
201     $db1->rollback;
202
203     is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
204     is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
205
206     cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
207     cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
208
209     SKIP: {
210         skip "Optimize tests skipped on Win32", 7
211             if $^O eq 'MSWin32' || $^O eq 'cygwin';
212
213         $db1->optimize;
214
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     }
230 }
231
232 done_testing;