Debian Codegraph @

lang/perl/BerkeleyDB/t/txn.t

raw
1 #!./perl -w
2
3 use strict ;
4
5 use lib 't' ;
6 use BerkeleyDB;
7 use util ;
8
9 use Test::More ;
10
11 plan tests => 58;
12
13 my $Dfile = "dbhash.tmp";
14
15 umask(0);
16
17 {
18 # error cases
19
20 my $lex = new LexFile $Dfile ;
21 my %hash ;
22 my $value ;
23
24 my $home = "./fred" ;
25 ok my $lexD = new LexDir($home);
26 ok my $env = new BerkeleyDB::Env -Home => $home, @StdErrFile,
27 -Flags => DB_CREATE| DB_INIT_MPOOL;
28 eval { $env->txn_begin() ; } ;
29 ok $@ =~ /^BerkeleyDB Aborting: Transaction Manager not enabled at/ ;
30
31 eval { my $txn_mgr = $env->TxnMgr() ; } ;
32 ok $@ =~ /^BerkeleyDB Aborting: Transaction Manager not enabled at/ ;
33 undef $env ;
34
35 }
36
37 {
38 # transaction - abort works
39
40 my $lex = new LexFile $Dfile ;
41 my %hash ;
42 my $value ;
43
44 my $home = "./fred" ;
45 ok my $lexD = new LexDir($home);
46 ok my $env = new BerkeleyDB::Env -Home => $home, @StdErrFile,
47 -Flags => DB_CREATE|DB_INIT_TXN|
48 DB_INIT_MPOOL|DB_INIT_LOCK ;
49 ok my $txn = $env->txn_begin() ;
50 ok my $db1 = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
51 -Flags => DB_CREATE ,
52 -Env => $env,
53 -Txn => $txn ;
54
55
56 ok $txn->txn_commit() == 0 ;
57 ok $txn = $env->txn_begin() ;
58 $db1->Txn($txn);
59
60 # create some data
61 my %data = (
62 "red" => "boat",
63 "green" => "house",
64 "blue" => "sea",
65 ) ;
66
67 my $ret = 0 ;
68 while (my ($k, $v) = each %data) {
69 $ret += $db1->db_put($k, $v) ;
70 }
71 ok $ret == 0 ;
72
73 # should be able to see all the records
74
75 ok my $cursor = $db1->db_cursor() ;
76 my ($k, $v) = ("", "") ;
77 my $count = 0 ;
78 # sequence forwards
79 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
80 ++ $count ;
81 }
82 ok $count == 3 ;
83 undef $cursor ;
84
85 # now abort the transaction
86 ok $txn->txn_abort() == 0 ;
87
88 # there shouldn't be any records in the database
89 $count = 0 ;
90 # sequence forwards
91 ok $cursor = $db1->db_cursor() ;
92 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
93 ++ $count ;
94 }
95 ok $count == 0 ;
96
97 my $stat = $env->txn_stat() ;
98 ok $stat->{'st_naborts'} == 1 ;
99
100 undef $txn ;
101 undef $cursor ;
102 undef $db1 ;
103 undef $env ;
104 untie %hash ;
105 }
106
107 {
108 # transaction - abort works via txnmgr
109
110 my $lex = new LexFile $Dfile ;
111 my %hash ;
112 my $value ;
113
114 my $home = "./fred" ;
115 ok my $lexD = new LexDir($home);
116 ok my $env = new BerkeleyDB::Env -Home => $home, @StdErrFile,
117 -Flags => DB_CREATE|DB_INIT_TXN|
118 DB_INIT_MPOOL|DB_INIT_LOCK ;
119 ok my $txn_mgr = $env->TxnMgr() ;
120 ok my $txn = $txn_mgr->txn_begin() ;
121 ok my $db1 = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
122 -Flags => DB_CREATE ,
123 -Env => $env,
124 -Txn => $txn ;
125
126 ok $txn->txn_commit() == 0 ;
127 ok $txn = $env->txn_begin() ;
128 $db1->Txn($txn);
129
130 # create some data
131 my %data = (
132 "red" => "boat",
133 "green" => "house",
134 "blue" => "sea",
135 ) ;
136
137 my $ret = 0 ;
138 while (my ($k, $v) = each %data) {
139 $ret += $db1->db_put($k, $v) ;
140 }
141 ok $ret == 0 ;
142
143 # should be able to see all the records
144
145 ok my $cursor = $db1->db_cursor() ;
146 my ($k, $v) = ("", "") ;
147 my $count = 0 ;
148 # sequence forwards
149 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
150 ++ $count ;
151 }
152 ok $count == 3 ;
153 undef $cursor ;
154
155 # now abort the transaction
156 ok $txn->txn_abort() == 0 ;
157
158 # there shouldn't be any records in the database
159 $count = 0 ;
160 # sequence forwards
161 ok $cursor = $db1->db_cursor() ;
162 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
163 ++ $count ;
164 }
165 ok $count == 0 ;
166
167 my $stat = $txn_mgr->txn_stat() ;
168 ok $stat->{'st_naborts'} == 1 ;
169
170 undef $txn ;
171 undef $cursor ;
172 undef $db1 ;
173 undef $txn_mgr ;
174 undef $env ;
175 untie %hash ;
176 }
177
178 {
179 # transaction - commit works
180
181 my $lex = new LexFile $Dfile ;
182 my %hash ;
183 my $value ;
184
185 my $home = "./fred" ;
186 ok my $lexD = new LexDir($home);
187 ok my $env = new BerkeleyDB::Env -Home => $home, @StdErrFile,
188 -Flags => DB_CREATE|DB_INIT_TXN|
189 DB_INIT_MPOOL|DB_INIT_LOCK ;
190 ok my $txn = $env->txn_begin() ;
191 ok my $db1 = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
192 -Flags => DB_CREATE ,
193 -Env => $env,
194 -Txn => $txn ;
195
196
197 ok $txn->txn_commit() == 0 ;
198 ok $txn = $env->txn_begin() ;
199 $db1->Txn($txn);
200
201 # create some data
202 my %data = (
203 "red" => "boat",
204 "green" => "house",
205 "blue" => "sea",
206 ) ;
207
208 my $ret = 0 ;
209 while (my ($k, $v) = each %data) {
210 $ret += $db1->db_put($k, $v) ;
211 }
212 ok $ret == 0 ;
213
214 # should be able to see all the records
215
216 ok my $cursor = $db1->db_cursor() ;
217 my ($k, $v) = ("", "") ;
218 my $count = 0 ;
219 # sequence forwards
220 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
221 ++ $count ;
222 }
223 ok $count == 3 ;
224 undef $cursor ;
225
226 # now commit the transaction
227 ok $txn->txn_commit() == 0 ;
228
229 $count = 0 ;
230 # sequence forwards
231 ok $cursor = $db1->db_cursor() ;
232 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
233 ++ $count ;
234 }
235 ok $count == 3 ;
236
237 my $stat = $env->txn_stat() ;
238 ok $stat->{'st_naborts'} == 0 ;
239
240 undef $txn ;
241 undef $cursor ;
242 undef $db1 ;
243 undef $env ;
244 untie %hash ;
245 }
246
247 {
248 # transaction - commit works via txnmgr
249
250 my $lex = new LexFile $Dfile ;
251 my %hash ;
252 my $value ;
253
254 my $home = "./fred" ;
255 ok my $lexD = new LexDir($home);
256 ok my $env = new BerkeleyDB::Env -Home => $home, @StdErrFile,
257 -Flags => DB_CREATE|DB_INIT_TXN|
258 DB_INIT_MPOOL|DB_INIT_LOCK ;
259 ok my $txn_mgr = $env->TxnMgr() ;
260 ok my $txn = $txn_mgr->txn_begin() ;
261 ok my $db1 = tie %hash, 'BerkeleyDB::Hash', -Filename => $Dfile,
262 -Flags => DB_CREATE ,
263 -Env => $env,
264 -Txn => $txn ;
265
266 ok $txn->txn_commit() == 0 ;
267 ok $txn = $env->txn_begin() ;
268 $db1->Txn($txn);
269
270 # create some data
271 my %data = (
272 "red" => "boat",
273 "green" => "house",
274 "blue" => "sea",
275 ) ;
276
277 my $ret = 0 ;
278 while (my ($k, $v) = each %data) {
279 $ret += $db1->db_put($k, $v) ;
280 }
281 ok $ret == 0 ;
282
283 # should be able to see all the records
284
285 ok my $cursor = $db1->db_cursor() ;
286 my ($k, $v) = ("", "") ;
287 my $count = 0 ;
288 # sequence forwards
289 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
290 ++ $count ;
291 }
292 ok $count == 3 ;
293 undef $cursor ;
294
295 # now commit the transaction
296 ok $txn->txn_commit() == 0 ;
297
298 $count = 0 ;
299 # sequence forwards
300 ok $cursor = $db1->db_cursor() ;
301 while ($cursor->c_get($k, $v, DB_NEXT) == 0) {
302 ++ $count ;
303 }
304 ok $count == 3 ;
305
306 my $stat = $txn_mgr->txn_stat() ;
307 ok $stat->{'st_naborts'} == 0 ;
308
309 undef $txn ;
310 undef $cursor ;
311 undef $db1 ;
312 undef $txn_mgr ;
313 undef $env ;
314 untie %hash ;
315 }
316