Converted to using only 2 transactions by default and added the num_txns to the header
[dbsrgits/DBM-Deep.git] / t / 33_transactions.t
CommitLineData
fee0243f 1use strict;
638e9b2b 2use Test::More tests => 91;
42717e46 3use Test::Deep;
fee0243f 4use t::common qw( new_fh );
5
6use_ok( 'DBM::Deep' );
7
8my ($fh, $filename) = new_fh();
21838116 9my $db1 = DBM::Deep->new(
fee0243f 10 file => $filename,
21838116 11 locking => 1,
c9b6d0d8 12 autoflush => 1,
36d630c6 13 num_txns => 16,
fee0243f 14);
15
21838116 16my $db2 = DBM::Deep->new(
17 file => $filename,
18 locking => 1,
c9b6d0d8 19 autoflush => 1,
36d630c6 20 num_txns => 16,
21838116 21);
22
23$db1->{x} = 'y';
24is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
25is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
26
8cb9205a 27eval { $db1->rollback };
28ok( $@, "Attempting to rollback without a transaction throws an error" );
29
30eval { $db1->commit };
31ok( $@, "Attempting to commit without a transaction throws an error" );
32
21838116 33$db1->begin_work;
34
8cb9205a 35eval { $db1->begin_work };
36ok( $@, "Attempting to begin_work within a transaction throws an error" );
37
17164f8a 38 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
39 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
40
633df1fd 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" );
21838116 43
2432d6cc 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
633df1fd 48 $db1->{x} = 'z';
49 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
2432d6cc 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
633df1fd 56 $db2->{other_x} = 'foo';
57 is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
13ff93d5 58 ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." );
21838116 59
42dff4d9 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
2432d6cc 65 cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
42717e46 66 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
2432d6cc 67
21838116 68$db1->rollback;
69
70is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
71is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
72
73is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
74is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
460b1067 75
6677de37 76$db1->begin_work;
77
17164f8a 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
633df1fd 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" );
6677de37 87
504185fb 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
6f999f6e 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" );
42717e46 97 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
98
6677de37 99$db1->commit;
100
101is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
102is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
103
6f999f6e 104is( $db1->{z}, 'a', "After commit, DB1's Z is A" );
105is( $db2->{z}, 'a', "After commit, DB2's Z is A" );
106
107is( $db1->{other_x}, 'bar', "After commit, DB1's other_x is bar" );
108is( $db2->{other_x}, 'bar', "After commit, DB2's other_x is bar" );
109
6677de37 110$db1->begin_work;
111
17164f8a 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
6f999f6e 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" );
6de4e4e9 123
633df1fd 124 delete $db2->{other_x};
94e8af14 125 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
504185fb 126 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
17164f8a 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" );
42717e46 130
633df1fd 131 delete $db1->{x};
94e8af14 132 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
22e20cce 133 is( $db2->{x}, 'z', "But, DB2 can still see it" );
94e8af14 134
17164f8a 135 cmp_bag( [ keys %$db1 ], [qw( other_x z )], "DB1 keys correct" );
136 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
42717e46 137
6677de37 138$db1->rollback;
139
42717e46 140ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
141ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
6677de37 142
143is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
144is( $db2->{x}, 'z', "DB2 can still see it" );
145
17164f8a 146cmp_bag( [ keys %$db1 ], [qw( x z )], "DB1 keys correct" );
147cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
42717e46 148
6677de37 149$db1->begin_work;
150
633df1fd 151 delete $db1->{x};
94e8af14 152 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
17164f8a 153
633df1fd 154 is( $db2->{x}, 'z', "But, DB2 can still see it" );
6677de37 155
17164f8a 156 cmp_bag( [ keys %$db1 ], [qw( z )], "DB1 keys correct" );
157 cmp_bag( [ keys %$db2 ], [qw( x z )], "DB2 keys correct" );
42717e46 158
6677de37 159$db1->commit;
160
42717e46 161ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
162ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
94e8af14 163
164$db1->{foo} = 'bar';
165is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
166is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
167
17164f8a 168cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
169cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 170
94e8af14 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
42717e46 177 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
17164f8a 178 cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 179
94e8af14 180$db1->rollback;
181
182is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
183is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
42717e46 184
17164f8a 185cmp_bag( [ keys %$db1 ], [qw( foo z )], "DB1 keys correct" );
186cmp_bag( [ keys %$db2 ], [qw( foo z )], "DB2 keys correct" );
42717e46 187
13ff93d5 188SKIP: {
189 skip "Optimize tests skipped on Win32", 5
190 if $^O eq 'MSWin32' || $^O eq 'cygwin';
f9a320bb 191
13ff93d5 192 $db1->optimize;
42717e46 193
13ff93d5 194 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
195 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
42717e46 196
638e9b2b 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" );
42717e46 202
13ff93d5 203 $db1->begin_work;
42717e46 204
638e9b2b 205 cmp_ok( $db1->_engine->trans_id, '==', 1, "Transaction ID has been reset after optimize" );
13ff93d5 206
207 $db1->rollback;
208}
42717e46 209
210__END__
211
212Tests to add:
213* Two transactions running at the same time
214* Doing a clear on the head while a transaction is running