Fixed bug where overwrites weren't transaction-aware
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
CommitLineData
fee0243f 1use strict;
504185fb 2use Test::More tests => 58;
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,
fee0243f 13);
14
21838116 15my $db2 = DBM::Deep->new(
16 file => $filename,
17 locking => 1,
c9b6d0d8 18 autoflush => 1,
21838116 19);
20
21$db1->{x} = 'y';
22is( $db1->{x}, 'y', "Before transaction, DB1's X is Y" );
23is( $db2->{x}, 'y', "Before transaction, DB2's X is Y" );
24
25$db1->begin_work;
26
633df1fd 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" );
21838116 29
633df1fd 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" );
21838116 33
633df1fd 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." );
21838116 37
42717e46 38TODO: {
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
21838116 44$db1->rollback;
45
46is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
47is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
48
49is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
50is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
460b1067 51
6677de37 52$db1->begin_work;
53
633df1fd 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" );
6677de37 60
504185fb 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
42717e46 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
6677de37 68$db1->commit;
69
70is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
71is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
72
73$db1->begin_work;
74
633df1fd 75 delete $db2->{other_x};
94e8af14 76 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
504185fb 77 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
6677de37 78
42717e46 79 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
80 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
81
633df1fd 82 delete $db1->{x};
94e8af14 83 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
22e20cce 84 is( $db2->{x}, 'z', "But, DB2 can still see it" );
94e8af14 85
42717e46 86TODO: {
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
6677de37 92$db1->rollback;
93
42717e46 94ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
95ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
6677de37 96
97is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
98is( $db2->{x}, 'z', "DB2 can still see it" );
99
42717e46 100cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
101cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
102
6677de37 103$db1->begin_work;
104
633df1fd 105 delete $db1->{x};
94e8af14 106 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
633df1fd 107 is( $db2->{x}, 'z', "But, DB2 can still see it" );
6677de37 108
42717e46 109TODO: {
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
6677de37 115$db1->commit;
116
42717e46 117ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
118ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
94e8af14 119
120$db1->{foo} = 'bar';
121is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
122is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
123
42717e46 124cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
125cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
126
94e8af14 127TODO: {
42717e46 128 todo_skip 'Still need to work on clear()', 4;
94e8af14 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
42717e46 136 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
137 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
138
94e8af14 139$db1->rollback;
140
141is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
142is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
42717e46 143
144cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
145cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
146
94e8af14 147}
42717e46 148
149$db1->optimize;
150is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
151is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
152
153cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
154cmp_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
164Tests 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?)