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