Fixed bug where overwrites weren't transaction-aware
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
1 use strict;
2 use Test::More tests => 58;
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 $db1->begin_work;
26
27     is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
28     is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
29
30     $db1->{x} = 'z';
31     is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
32     is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
33
34     $db2->{other_x} = 'foo';
35     is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
36     is( $db1->{other_x}, undef, "Since other_x was added after the transaction began, DB1 doesn't see it." );
37
38 TODO: {
39     local $TODO = "keys aren't working yet";
40     cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
41 }
42     cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
43
44 $db1->rollback;
45
46 is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
47 is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
48
49 is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
50 is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
51
52 $db1->begin_work;
53
54     is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
55     is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
56
57     $db1->{x} = 'z';
58     is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
59     is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
60
61     $db2->{other_x} = 'bar';
62     is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
63     is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
64
65     cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
66     cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
67
68 $db1->commit;
69
70 is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
71 is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
72
73 $db1->begin_work;
74
75     delete $db2->{other_x};
76     ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
77     is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
78
79     cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
80     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
81
82     delete $db1->{x};
83     ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
84     is( $db2->{x}, 'z', "But, DB2 can still see it" );
85
86 TODO: {
87     local $TODO = "keys aren't working yet";
88     cmp_bag( [ keys %$db1 ], [qw( other_x )], "DB1 keys correct" );
89 }
90     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
91
92 $db1->rollback;
93
94 ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
95 ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
96
97 is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
98 is( $db2->{x}, 'z', "DB2 can still see it" );
99
100 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
101 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
102
103 $db1->begin_work;
104
105     delete $db1->{x};
106     ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
107     is( $db2->{x}, 'z', "But, DB2 can still see it" );
108
109 TODO: {
110     local $TODO = "keys aren't working yet";
111     cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
112 }
113     cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
114
115 $db1->commit;
116
117 ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
118 ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
119
120 $db1->{foo} = 'bar';
121 is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
122 is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
123
124 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
125 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
126
127 TODO: {
128     todo_skip 'Still need to work on clear()', 4;
129
130 $db1->begin_work;
131
132     %$db1 = (); # clear()
133     ok( !exists $db1->{foo}, "Cleared foo" );
134     is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
135
136     cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
137     cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
138
139 $db1->rollback;
140
141 is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
142 is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
143
144 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
145 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
146
147 }
148
149 $db1->optimize;
150 is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
151 is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
152
153 cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
154 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
155
156 $db1->begin_work;
157
158     cmp_ok( $db1->_fileobj->transaction_id, '==', 1, "Transaction ID has been reset after optimize" );
159
160 $db1->rollback;
161
162 __END__
163
164 Tests to add:
165 * Two transactions running at the same time
166 * Doing a clear on the head while a transaction is running
167 # More than just two keys
168 * Arrays (in particular, how is length handled?)