Tagged 0.99_01
[dbsrgits/DBM-Deep.git] / t / 33_transactions.t
CommitLineData
fee0243f 1use strict;
15d754dd 2use Test::More tests => 62;
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 38 cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
42717e46 39 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
40
21838116 41$db1->rollback;
42
43is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
44is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
45
46is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
47is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
460b1067 48
6677de37 49$db1->begin_work;
50
633df1fd 51 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
52 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
53
54 $db1->{x} = 'z';
55 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
56 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
6677de37 57
504185fb 58 $db2->{other_x} = 'bar';
59 is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" );
60 is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." );
61
42717e46 62 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
63 cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" );
64
6677de37 65$db1->commit;
66
67is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
68is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
69
70$db1->begin_work;
71
633df1fd 72 delete $db2->{other_x};
94e8af14 73 ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
504185fb 74 is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." );
6677de37 75
42717e46 76 cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" );
77 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
78
633df1fd 79 delete $db1->{x};
94e8af14 80 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
22e20cce 81 is( $db2->{x}, 'z', "But, DB2 can still see it" );
94e8af14 82
42717e46 83 cmp_bag( [ keys %$db1 ], [qw( other_x )], "DB1 keys correct" );
42717e46 84 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
85
6677de37 86$db1->rollback;
87
42717e46 88ok( !exists $db2->{other_x}, "It's still deleted for DB2" );
89ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" );
6677de37 90
91is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
92is( $db2->{x}, 'z', "DB2 can still see it" );
93
42717e46 94cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" );
95cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
96
6677de37 97$db1->begin_work;
98
633df1fd 99 delete $db1->{x};
94e8af14 100 ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" );
15d754dd 101#__END__
633df1fd 102 is( $db2->{x}, 'z', "But, DB2 can still see it" );
6677de37 103
42717e46 104 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
42717e46 105 cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" );
106
6677de37 107$db1->commit;
108
42717e46 109ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" );
110ok( !exists $db2->{x}, "DB2 can now see the deletion of X" );
94e8af14 111
112$db1->{foo} = 'bar';
113is( $db1->{foo}, 'bar', "Set foo to bar in DB1" );
114is( $db2->{foo}, 'bar', "Set foo to bar in DB2" );
115
42717e46 116cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
117cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
118
94e8af14 119$db1->begin_work;
120
121 %$db1 = (); # clear()
122 ok( !exists $db1->{foo}, "Cleared foo" );
123 is( $db2->{foo}, 'bar', "But in DB2, we can still see it" );
124
42717e46 125 cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" );
126 cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
127
94e8af14 128$db1->rollback;
129
130is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" );
131is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" );
42717e46 132
133cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
134cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
135
42717e46 136$db1->optimize;
15d754dd 137
42717e46 138is( $db1->{foo}, 'bar', 'After optimize, everything is ok' );
139is( $db2->{foo}, 'bar', 'After optimize, everything is ok' );
140
141cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" );
142cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" );
143
144$db1->begin_work;
145
146 cmp_ok( $db1->_fileobj->transaction_id, '==', 1, "Transaction ID has been reset after optimize" );
147
148$db1->rollback;
149
150__END__
151
152Tests to add:
153* Two transactions running at the same time
154* Doing a clear on the head while a transaction is running
155# More than just two keys