Commit | Line | Data |
fee0243f |
1 | use strict; |
8cb9205a |
2 | use Test::More tests => 65; |
42717e46 |
3 | use Test::Deep; |
fee0243f |
4 | use t::common qw( new_fh ); |
5 | |
6 | use_ok( 'DBM::Deep' ); |
7 | |
8 | my ($fh, $filename) = new_fh(); |
21838116 |
9 | my $db1 = DBM::Deep->new( |
fee0243f |
10 | file => $filename, |
21838116 |
11 | locking => 1, |
c9b6d0d8 |
12 | autoflush => 1, |
fee0243f |
13 | ); |
14 | |
21838116 |
15 | my $db2 = DBM::Deep->new( |
16 | file => $filename, |
17 | locking => 1, |
c9b6d0d8 |
18 | autoflush => 1, |
21838116 |
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 | |
8cb9205a |
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 | |
21838116 |
31 | $db1->begin_work; |
32 | |
8cb9205a |
33 | eval { $db1->begin_work }; |
34 | ok( $@, "Attempting to begin_work within a transaction throws an error" ); |
35 | |
633df1fd |
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" ); |
21838116 |
38 | |
633df1fd |
39 | $db1->{x} = 'z'; |
40 | is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" ); |
41 | is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" ); |
c9f02899 |
42 | __END__ |
633df1fd |
43 | $db2->{other_x} = 'foo'; |
44 | is( $db2->{other_x}, 'foo', "DB2 set other_x within DB1's transaction, so DB2 can see it" ); |
13ff93d5 |
45 | ok( !exists $db1->{other_x}, "Since other_x was added after the transaction began, DB1 doesn't see it." ); |
21838116 |
46 | |
42717e46 |
47 | cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" ); |
42717e46 |
48 | cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" ); |
49 | |
21838116 |
50 | $db1->rollback; |
51 | |
52 | is( $db1->{x}, 'y', "After rollback, DB1's X is Y" ); |
53 | is( $db2->{x}, 'y', "After rollback, DB2's X is Y" ); |
54 | |
55 | is( $db1->{other_x}, 'foo', "After DB1 transaction is over, DB1 can see other_x" ); |
56 | is( $db2->{other_x}, 'foo', "After DB1 transaction is over, DB2 can still see other_x" ); |
460b1067 |
57 | |
6677de37 |
58 | $db1->begin_work; |
59 | |
633df1fd |
60 | is( $db1->{x}, 'y', "DB1 transaction started, no actions - DB1's X is Y" ); |
61 | is( $db2->{x}, 'y', "DB1 transaction started, no actions - DB2's X is Y" ); |
62 | |
63 | $db1->{x} = 'z'; |
64 | is( $db1->{x}, 'z', "Within DB1 transaction, DB1's X is Z" ); |
65 | is( $db2->{x}, 'y', "Within DB1 transaction, DB2's X is still Y" ); |
6677de37 |
66 | |
504185fb |
67 | $db2->{other_x} = 'bar'; |
68 | is( $db2->{other_x}, 'bar', "DB2 set other_x within DB1's transaction, so DB2 can see it" ); |
69 | is( $db1->{other_x}, 'foo', "Since other_x was modified after the transaction began, DB1 doesn't see the change." ); |
70 | |
42717e46 |
71 | cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" ); |
72 | cmp_bag( [ keys %$db2 ], [qw( x other_x )], "DB2 keys correct" ); |
73 | |
6677de37 |
74 | $db1->commit; |
75 | |
76 | is( $db1->{x}, 'z', "After commit, DB1's X is Z" ); |
77 | is( $db2->{x}, 'z', "After commit, DB2's X is Z" ); |
78 | |
79 | $db1->begin_work; |
80 | |
633df1fd |
81 | delete $db2->{other_x}; |
94e8af14 |
82 | ok( !exists $db2->{other_x}, "DB2 deleted other_x in DB1's transaction, so it can't see it anymore" ); |
504185fb |
83 | is( $db1->{other_x}, 'bar', "Since other_x was deleted after the transaction began, DB1 still sees it." ); |
6677de37 |
84 | |
42717e46 |
85 | cmp_bag( [ keys %$db1 ], [qw( x other_x )], "DB1 keys correct" ); |
86 | cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" ); |
87 | |
633df1fd |
88 | delete $db1->{x}; |
94e8af14 |
89 | ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" ); |
22e20cce |
90 | is( $db2->{x}, 'z', "But, DB2 can still see it" ); |
94e8af14 |
91 | |
42717e46 |
92 | cmp_bag( [ keys %$db1 ], [qw( other_x )], "DB1 keys correct" ); |
42717e46 |
93 | cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" ); |
94 | |
6677de37 |
95 | $db1->rollback; |
96 | |
42717e46 |
97 | ok( !exists $db2->{other_x}, "It's still deleted for DB2" ); |
98 | ok( !exists $db1->{other_x}, "And now DB1 sees the deletion" ); |
6677de37 |
99 | |
100 | is( $db1->{x}, 'z', "The transaction was rolled back, so DB1 can see X now" ); |
101 | is( $db2->{x}, 'z', "DB2 can still see it" ); |
102 | |
42717e46 |
103 | cmp_bag( [ keys %$db1 ], [qw( x )], "DB1 keys correct" ); |
104 | cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" ); |
105 | |
6677de37 |
106 | $db1->begin_work; |
107 | |
633df1fd |
108 | delete $db1->{x}; |
94e8af14 |
109 | ok( !exists $db1->{x}, "DB1 deleted X in a transaction, so it can't see it anymore" ); |
ea2f6d67 |
110 | #__END__ |
633df1fd |
111 | is( $db2->{x}, 'z', "But, DB2 can still see it" ); |
6677de37 |
112 | |
42717e46 |
113 | cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" ); |
42717e46 |
114 | cmp_bag( [ keys %$db2 ], [qw( x )], "DB2 keys correct" ); |
115 | |
6677de37 |
116 | $db1->commit; |
117 | |
42717e46 |
118 | ok( !exists $db1->{x}, "The transaction was committed, so DB1 still deleted X" ); |
119 | ok( !exists $db2->{x}, "DB2 can now see the deletion of X" ); |
94e8af14 |
120 | |
121 | $db1->{foo} = 'bar'; |
122 | is( $db1->{foo}, 'bar', "Set foo to bar in DB1" ); |
123 | is( $db2->{foo}, 'bar', "Set foo to bar in DB2" ); |
124 | |
42717e46 |
125 | cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" ); |
126 | cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" ); |
127 | |
94e8af14 |
128 | $db1->begin_work; |
129 | |
130 | %$db1 = (); # clear() |
131 | ok( !exists $db1->{foo}, "Cleared foo" ); |
132 | is( $db2->{foo}, 'bar', "But in DB2, we can still see it" ); |
133 | |
42717e46 |
134 | cmp_bag( [ keys %$db1 ], [qw()], "DB1 keys correct" ); |
135 | cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" ); |
136 | |
94e8af14 |
137 | $db1->rollback; |
138 | |
139 | is( $db1->{foo}, 'bar', "Rollback means 'foo' is still there" ); |
140 | is( $db2->{foo}, 'bar', "Rollback means 'foo' is still there" ); |
42717e46 |
141 | |
142 | cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" ); |
143 | cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" ); |
144 | |
13ff93d5 |
145 | SKIP: { |
146 | skip "Optimize tests skipped on Win32", 5 |
147 | if $^O eq 'MSWin32' || $^O eq 'cygwin'; |
f9a320bb |
148 | |
13ff93d5 |
149 | $db1->optimize; |
42717e46 |
150 | |
13ff93d5 |
151 | is( $db1->{foo}, 'bar', 'After optimize, everything is ok' ); |
152 | is( $db2->{foo}, 'bar', 'After optimize, everything is ok' ); |
42717e46 |
153 | |
13ff93d5 |
154 | cmp_bag( [ keys %$db1 ], [qw( foo )], "DB1 keys correct" ); |
155 | cmp_bag( [ keys %$db2 ], [qw( foo )], "DB2 keys correct" ); |
42717e46 |
156 | |
13ff93d5 |
157 | $db1->begin_work; |
42717e46 |
158 | |
83371fe3 |
159 | cmp_ok( $db1->_storage->transaction_id, '==', 1, "Transaction ID has been reset after optimize" ); |
13ff93d5 |
160 | |
161 | $db1->rollback; |
162 | } |
42717e46 |
163 | |
164 | __END__ |
165 | |
166 | Tests to add: |
167 | * Two transactions running at the same time |
168 | * Doing a clear on the head while a transaction is running |
169 | # More than just two keys |