Commit | Line | Data |
fee0243f |
1 | use strict; |
2120a181 |
2 | use Test::More tests => 99; |
42717e46 |
3 | use Test::Deep; |
2120a181 |
4 | use Test::Exception; |
fee0243f |
5 | use t::common qw( new_fh ); |
6 | |
7 | use_ok( 'DBM::Deep' ); |
8 | |
9 | my ($fh, $filename) = new_fh(); |
21838116 |
10 | my $db1 = DBM::Deep->new( |
fee0243f |
11 | file => $filename, |
21838116 |
12 | locking => 1, |
c9b6d0d8 |
13 | autoflush => 1, |
2120a181 |
14 | num_txns => 16, |
fee0243f |
15 | ); |
16 | |
21838116 |
17 | my $db2 = DBM::Deep->new( |
18 | file => $filename, |
19 | locking => 1, |
c9b6d0d8 |
20 | autoflush => 1, |
2120a181 |
21 | num_txns => 16, |
21838116 |
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 | |
2120a181 |
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 | |
21838116 |
39 | $db1->begin_work; |
40 | |
2120a181 |
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" ); |
db2eb673 |
50 | __END__ |
51 | warn "4\n"; |
2120a181 |
52 | cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" ); |
db2eb673 |
53 | warn "5\n"; |
2120a181 |
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 | |
633df1fd |
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" ); |
21838116 |
71 | |
2120a181 |
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 | |
633df1fd |
76 | $db1->{x} = 'z'; |
77 | is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" ); |
2120a181 |
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." ); |
21838116 |
83 | |
633df1fd |
84 | $db2->{other_x} = 'foo'; |
85 | is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" ); |
13ff93d5 |
86 | ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." ); |
21838116 |
87 | |
2120a181 |
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" ); |
42717e46 |
94 | cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" ); |
95 | |
21838116 |
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" ); |
460b1067 |
103 | |
6677de37 |
104 | $db1->begin_work; |
105 | |
2120a181 |
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 | |
633df1fd |
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" ); |
6677de37 |
115 | |
504185fb |
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 | |
2120a181 |
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" ); |
42717e46 |
125 | cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" ); |
126 | |
6677de37 |
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 | |
2120a181 |
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 | |
6677de37 |
138 | $db1->begin_work; |
139 | |
2120a181 |
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 | |
633df1fd |
152 | delete $db2->{other_x}; |
94e8af14 |
153 | ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" ); |
504185fb |
154 | is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." ); |
6677de37 |
155 | |
2120a181 |
156 | cmp_bag( [ keys %$db1 ], [qw( x z other_x )], "DB1 keys correct" ); |
157 | cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" ); |
42717e46 |
158 | |
633df1fd |
159 | delete $db1->{x}; |
94e8af14 |
160 | ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" ); |
22e20cce |
161 | is( $db2->{x}, 'z', "But, DB2 can still see it" ); |
94e8af14 |
162 | |
2120a181 |
163 | cmp_bag( [ keys %$db1 ], [qw( other_x z )], "DB1 keys correct" ); |
164 | cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" ); |
42717e46 |
165 | |
6677de37 |
166 | $db1->rollback; |
167 | |
42717e46 |
168 | ok( !exists $db2->{other_x}, "It's still deleted for DB2" ); |
169 | ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" ); |
6677de37 |
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 | |
2120a181 |
174 | cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" ); |
175 | cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" ); |
42717e46 |
176 | |
6677de37 |
177 | $db1->begin_work; |
178 | |
633df1fd |
179 | delete $db1->{x}; |
94e8af14 |
180 | ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" ); |
2120a181 |
181 | |
633df1fd |
182 | is( $db2->{x}, 'z', "But, DB2 can still see it" ); |
6677de37 |
183 | |
2120a181 |
184 | cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" ); |
185 | cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" ); |
42717e46 |
186 | |
6677de37 |
187 | $db1->commit; |
188 | |
42717e46 |
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" ); |
94e8af14 |
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 | |
2120a181 |
196 | cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" ); |
197 | cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" ); |
42717e46 |
198 | |
94e8af14 |
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 | |
42717e46 |
205 | cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" ); |
2120a181 |
206 | cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" ); |
42717e46 |
207 | |
94e8af14 |
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" ); |
42717e46 |
212 | |
2120a181 |
213 | cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" ); |
214 | cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" ); |
42717e46 |
215 | |
13ff93d5 |
216 | SKIP: { |
45f047f8 |
217 | skip "Optimize tests skipped on Win32", 7 |
13ff93d5 |
218 | if $^O eq 'MSWin32' || $^O eq 'cygwin'; |
f9a320bb |
219 | |
13ff93d5 |
220 | $db1->optimize; |
42717e46 |
221 | |
13ff93d5 |
222 | is( $db1->{foo}, 'bar', 'After optimize, everything is ok' ); |
223 | is( $db2->{foo}, 'bar', 'After optimize, everything is ok' ); |
42717e46 |
224 | |
2120a181 |
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" ); |
42717e46 |
230 | |
13ff93d5 |
231 | $db1->begin_work; |
42717e46 |
232 | |
2120a181 |
233 | cmp_ok( $db1->_engine->trans_id, '==', 1, "Transaction ID has been reset after optimize" ); |
13ff93d5 |
234 | |
235 | $db1->rollback; |
236 | } |
42717e46 |
237 | |
238 | __END__ |