Phantom reads because transactional writes aren't deleted yet have been fixed
[dbsrgits/DBM-Deep.git] / t / 28_transactions.t
CommitLineData
fee0243f 1use strict;
633df1fd 2use Test::More tests => 31;
fee0243f 3use Test::Exception;
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
38$db1->rollback;
39
40is( $db1->{x}, 'y', "After rollback, DB1's X is Y" );
41is( $db2->{x}, 'y', "After rollback, DB2's X is Y" );
42
43is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" );
44is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" );
460b1067 45
6677de37 46$db1->begin_work;
47
633df1fd 48 is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" );
49 is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" );
50
51 $db1->{x} = 'z';
52 is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" );
53 is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" );
6677de37 54
55$db1->commit;
56
57is( $db1->{x}, 'z', "After commit, DB1's X is Z" );
58is( $db2->{x}, 'z', "After commit, DB2's X is Z" );
59
60$db1->begin_work;
61
633df1fd 62 delete $db2->{other_x};
63 is( $db2->{other_x}, undef, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" );
64 is( $db1->{other_x}, 'foo', "Since other_x was deleted after the transaction began, DB1 still sees it." );
6677de37 65
633df1fd 66 delete $db1->{x};
67 is( $db1->{x}, undef, "DB1 deleted X in a transaction, so it can't see it anymore" );
68 is( $db2->{x}, 'z', "But, DB2 can still see it" );
6677de37 69
70$db1->rollback;
71
72is( $db2->{other_x}, undef, "It's still deleted for DB2" );
73is( $db1->{other_x}, undef, "And now DB1 sees the deletion" );
74
75is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" );
76is( $db2->{x}, 'z', "DB2 can still see it" );
77
78$db1->begin_work;
79
633df1fd 80 delete $db1->{x};
81 is( $db1->{x}, undef, "DB1 deleted X in a transaction, so it can't see it anymore" );
82 is( $db2->{x}, 'z', "But, DB2 can still see it" );
6677de37 83
84$db1->commit;
85
86is( $db1->{x}, undef, "The transaction was committed, so DB1 still deleted X" );
87is( $db2->{x}, undef, "DB2 can now see the deletion of X" );