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