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