blib/lib/DBIO/Test/AsyncParity/Cases.pm

filter: Statementstmt 67.2% 357 / 531 Branchbran 32.6% 15 / 46 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 69.2% 164 / 237 Podpod 100% 2 / 2 totaltotal 64.9% 538 / 829 CC 33 SCAR 7.3
CC
33
Cov
64%
__ANON__
12.0
_hold_all
12.0
__ANON__
12.0
CRAP 2.1
« blib/lib/DBIO/ChangeLog/Entry.pm ↑ Summary blib/lib/DBIO/Storage/Async/PipelineContext.pm »
1 package DBIO::Test::AsyncParity::Cases;
2 # ABSTRACT: The case table of DBIO::Test::AsyncParity: ResultSet operations, once synchronous and once asynchronous
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = '0.900007';
8  
9 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 184 ▶sub fixture {
Subroutine fixture: called CC=1 SCAR=0.0
Pod documented
14 return {
15 184 artists => [
16 [ 'Caterwauler McCrae' => 10 ],
17 [ 'Random Boy Band' => 20 ],
18 [ 'We Are Goth' => 30 ],
19 ],
20 cds => [
21 [ 'Caterwauler McCrae', 'Spoonful of bees', 1999 ],
22 [ 'Caterwauler McCrae', 'Forkful of chips', 2001 ],
23 [ 'Random Boy Band', "Caterwaulin' Blues", 1997 ],
24 [ 'We Are Goth', 'Generic Manufactured Singles', 2001 ],
25 ],
26 tracks => [
27 [ 'Spoonful of bees', 1, 'Boring Name' ],
28 [ 'Spoonful of bees', 2, 'Boring Song' ],
29 [ 'Spoonful of bees', 3, 'No More Ideas' ],
30 [ 'Forkful of chips', 1, 'Stripy' ],
31 ],
32 };
33 }
34  
35  
36 # --------------------------------------------------------------- the helpers
37  
38 5 my $FIRST = 'Caterwauler McCrae';
39 5 my $SECOND = 'Random Boy Band';
40  
41 272 ▶sub _rs { return $_[0]->resultset($_[1]) }
Subroutine _rs: called CC=1 SCAR=0.0
42  
43 # A seeded artist, found by name through the synchronous path. Reads are the
44 # same on both ways: a case's own subject is what differs.
45 24 ▶sub _artist {
Subroutine _artist: called CC=2 SCAR=7.0
46 24 my ($schema, $name) = @_;
47 24 my $row = _rs($schema, 'Artist')->search({ name => $name })->single;
48 24 ▶ DBIO::Exception->throw("the fixture artist '$name' is not there -- was it seeded?")
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 24 24
49 unless $row;
50 24 return $row;
51 }
52  
53 0 ▶sub _cd {
Subroutine _cd: not called CC=2 SCAR=17.9
54 0 my ($schema, $title) = @_;
55 0 my $row = _rs($schema, 'CD')->search({ title => $title })->single;
56 0 ▶ DBIO::Exception->throw("the fixture CD '$title' is not there -- was it seeded?")
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
57 unless $row;
58 0 return $row;
59 }
60  
61 152 ▶sub _pick {
Subroutine _pick: called CC=2 SCAR=7.0
62 152 my ($row, @cols) = @_;
63 152 ▶ return undef unless defined $row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 0 152 152
64 152 return { map { $_ => $row->get_column($_) } @cols };
65 }
66  
67 391 ▶sub _picks {
Subroutine _picks: called CC=1 SCAR=0.0
68 391 my @cols = @_;
69 391 ▶ return sub { [ map { _pick($_, @cols) } @_ ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
70 }
71  
72 391 ▶sub _count_named {
Subroutine _count_named: called CC=1 SCAR=0.0
73 391 my ($source, $col, $value) = @_;
74 391 ▶ return sub { _rs($_[0], $source)->search({ $col => $value })->count };
Subroutine __ANON__: called CC=1 SCAR=0.0
75 }
76  
77 # The storage-level insert both ways of the transaction cases use.
78 16 ▶sub _insert_args {
Subroutine _insert_args: called CC=1 SCAR=0.0
79 16 my ($schema, $name) = @_;
80 16 return ($schema->source('Artist'), { name => $name, rank => 5 });
81 }
82  
83 # Chain a list of async steps one after the other, resolving with $last->().
84 4 ▶sub _sequence {
Subroutine _sequence: called CC=2 SCAR=6.9
85 4 my ($fc, $steps, $last) = @_;
86 4 my $f = $fc->done;
87 4 for my $step (@$steps) {
88 10 my $s = $step;
89 10 ▶ $f = $f->then(sub { $s->() });
Subroutine __ANON__: called CC=1 SCAR=0.0
90 }
91 4 ▶ return $f->then(sub { $last->() });
Subroutine __ANON__: called CC=1 SCAR=0.0
92 }
93  
94 # --------------------------------------------------------------------- cases
95  
96  
97 23 ▶sub cases {
Subroutine cases: called CC=1 SCAR=0.0
Pod documented
98 23 my @case;
99  
100 # ------------------------------------------------------------------ reads
101 push @case, {
102 no => 1, name => 'find-pk', kind => 'direct',
103 doc => 'find by primary key',
104 4 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
105 4 my ($s) = @_;
106 4 my $id = _artist($s, $FIRST)->artistid;
107 4 return _rs($s, 'Artist')->find($id);
108 },
109 4 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
110 4 my ($s) = @_;
111 4 my $id = _artist($s, $FIRST)->artistid;
112 4 return _rs($s, 'Artist')->find_async($id);
113 },
114 23 norm => _picks(qw(name rank)),
115 };
116  
117 push @case, {
118 no => 2, name => 'find-unique', kind => 'direct',
119 doc => 'find by a named unique constraint (ADR 0034)',
120 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 2 return _rs($_[0], 'Artist')->find({ name => $SECOND }, { key => 'artist_name' });
122 },
123 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 2 return _rs($_[0], 'Artist')->find_async({ name => $SECOND }, { key => 'artist_name' });
125 },
126 23 norm => _picks(qw(name rank)),
127 };
128  
129 push @case, {
130 no => 3, name => 'find-miss', kind => 'direct',
131 doc => 'find that matches nothing answers undef, not an error',
132 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 2 return _rs($_[0], 'Artist')->find({ name => 'No Such Artist' }, { key => 'artist_name' });
134 },
135 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
136 2 return _rs($_[0], 'Artist')->find_async({ name => 'No Such Artist' }, { key => 'artist_name' });
137 },
138 4 ▶ norm => sub { [ map { defined $_ ? 'row' : undef } @_ ] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 4 4
139 23 };
140  
141 push @case, {
142 no => 4, name => 'find-rel-key', kind => 'direct',
143 doc => 'find whose key holds a relationship value (ResultSet find, ADR 0034)',
144 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
145 2 my ($s) = @_;
146 2 return _rs($s, 'CD')->find({ artist => _artist($s, $FIRST), title => 'Spoonful of bees' });
147 },
148 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
149 2 my ($s) = @_;
150 2 return _rs($s, 'CD')->find_async({ artist => _artist($s, $FIRST), title => 'Spoonful of bees' });
151 },
152 23 norm => _picks(qw(title year)),
153 };
154  
155 push @case, {
156 no => 5, name => 'search-all', kind => 'direct',
157 doc => 'search with a condition, all rows',
158 6 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 6 return _rs($_[0], 'Artist')->search({ rank => { '>=' => 10 } }, { order_by => 'name' })->all;
160 },
161 6 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 6 return _rs($_[0], 'Artist')->search({ rank => { '>=' => 10 } }, { order_by => 'name' })->all_async;
163 },
164 23 norm => _picks(qw(name rank)),
165 };
166  
167 push @case, {
168 no => 6, name => 'order-rows-offset', kind => 'direct',
169 doc => 'order_by with rows and offset',
170 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
171 2 return _rs($_[0], 'Artist')
172 ->search({}, { order_by => { -desc => 'name' }, rows => 2, offset => 1 })->all;
173 },
174 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
175 2 return _rs($_[0], 'Artist')
176 ->search({}, { order_by => { -desc => 'name' }, rows => 2, offset => 1 })->all_async;
177 },
178 23 norm => _picks(qw(name rank)),
179 };
180  
181 push @case, {
182 no => 7, name => 'group-by', kind => 'direct',
183 doc => 'group_by over a column subset',
184 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 2 return _rs($_[0], 'CD')
186 ->search({}, { columns => ['year'], group_by => ['year'], order_by => 'year' })->all;
187 },
188 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
189 2 return _rs($_[0], 'CD')
190 ->search({}, { columns => ['year'], group_by => ['year'], order_by => 'year' })->all_async;
191 },
192 23 norm => _picks('year'),
193 };
194  
195 push @case, {
196 no => 8, name => 'prefetch', kind => 'direct',
197 doc => 'prefetch of a has_many, collapsed',
198 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
199 2 return _rs($_[0], 'Artist')
200 ->search({ 'me.name' => $FIRST }, { prefetch => 'cds', order_by => 'cds.year' })->all;
201 },
202 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
203 2 return _rs($_[0], 'Artist')
204 ->search({ 'me.name' => $FIRST }, { prefetch => 'cds', order_by => 'cds.year' })->all_async;
205 },
206 4 ▶ norm => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
207 4 [ map { { name => $_->name, cds => [ map { $_->title } $_->cds->all ] } } @_ ];
208 },
209 23 };
210  
211 push @case, {
212 no => 9, name => 'join', kind => 'direct',
213 doc => 'join on a relationship, columns of the base table only',
214 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 2 return _rs($_[0], 'Artist')->search(
216 { 'cds.year' => 2001 },
217 { join => 'cds', columns => ['name'], distinct => 1, order_by => 'name' },
218 )->all;
219 },
220 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
221 2 return _rs($_[0], 'Artist')->search(
222 { 'cds.year' => 2001 },
223 { join => 'cds', columns => ['name'], distinct => 1, order_by => 'name' },
224 )->all_async;
225 },
226 23 norm => _picks('name'),
227 };
228  
229 push @case, {
230 no => 10, name => 'search-related', kind => 'composed',
231 doc => 'a related resultset of a row; the chain has no async method of its own',
232 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
233 2 my ($s) = @_;
234 2 return _artist($s, $FIRST)->search_related('cds', {}, { order_by => 'year' })->all;
235 },
236 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
237 2 my ($s) = @_;
238 2 return _artist($s, $FIRST)->search_related('cds', {}, { order_by => 'year' })->all_async;
239 },
240 23 norm => _picks(qw(title year)),
241 };
242  
243 push @case, {
244 no => 11, name => 'first', kind => 'direct',
245 doc => 'first row of an ordered set',
246 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
247 2 return _rs($_[0], 'Artist')->search({}, { order_by => 'name' })->first;
248 },
249 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
250 2 return _rs($_[0], 'Artist')->search({}, { order_by => 'name' })->first_async;
251 },
252 23 norm => _picks(qw(name rank)),
253 };
254  
255 push @case, {
256 no => 12, name => 'single', kind => 'direct',
257 doc => 'single row by a condition',
258 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
259 2 return _rs($_[0], 'Artist')->search({ name => $SECOND })->single;
260 },
261 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
262 2 return _rs($_[0], 'Artist')->search({ name => $SECOND })->single_async;
263 },
264 23 norm => _picks(qw(name rank)),
265 };
266  
267 push @case, {
268 no => 13, name => 'count', kind => 'direct',
269 doc => 'count of a condition',
270 3 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 3 return _rs($_[0], 'CD')->search({ year => 2001 })->count;
272 },
273 3 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
274 3 return _rs($_[0], 'CD')->search({ year => 2001 })->count_async;
275 },
276 6 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
277 23 };
278  
279 push @case, {
280 no => 14, name => 'count-literal', kind => 'direct',
281 doc => 'count with a literal SQL condition and a bind value',
282 5 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
283 5 return _rs($_[0], 'Artist')->count_literal($_[0]->storage->sql_maker->_quote('rank') . ' > ?', 15);
284 },
285 5 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
286 5 return _rs($_[0], 'Artist')->count_literal_async($_[0]->storage->sql_maker->_quote('rank') . ' > ?', 15);
287 },
288 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
289 23 };
290  
291 push @case, {
292 no => 15, name => 'results-exist', kind => 'direct',
293 doc => 'the EXISTS question, for a hit and a miss',
294 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
295 2 my ($s) = @_;
296 return (
297 2 _rs($s, 'Artist')->search({ name => $FIRST })->results_exist,
298 _rs($s, 'Artist')->search({ name => 'No Such Artist' })->results_exist,
299 );
300 },
301 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
302 2 my ($s) = @_;
303 2 my $hit = _rs($s, 'Artist')->search({ name => $FIRST })->results_exist_async;
304 2 ▶ return $hit->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
305 2 my $first = $_[0];
306 return _rs($s, 'Artist')->search({ name => 'No Such Artist' })->results_exist_async
307 2 ▶ ->then(sub { [ $first, $_[0] ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
308 2 });
309 },
310 4 ▶ norm => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
311 4 ▶ my @r = ref $_[0] eq 'ARRAY' ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 2 4
312 4 ▶ return [ map { $_ ? 1 : 0 } @r ];
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 4 4 8
313 },
314 23 };
315  
316 push @case, {
317 no => 16, name => 'rsc-all', kind => 'direct',
318 doc => 'all values of one column',
319 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
320 2 return _rs($_[0], 'Artist')->search({}, { order_by => 'name' })->get_column('name')->all;
321 },
322 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
323 2 return _rs($_[0], 'Artist')->search({}, { order_by => 'name' })->get_column('name')->all_async;
324 },
325 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
326 23 };
327  
328 push @case, {
329 no => 17, name => 'rsc-single', kind => 'direct',
330 doc => 'single value of one column',
331 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
332 2 return _rs($_[0], 'Artist')->search({ name => $FIRST })->get_column('rank')->single;
333 },
334 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
335 2 return _rs($_[0], 'Artist')->search({ name => $FIRST })->get_column('rank')->single_async;
336 },
337 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
338 23 };
339  
340 push @case, {
341 no => 18, name => 'rsc-min-max-sum', kind => 'direct',
342 doc => 'min, max and sum of a column',
343 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
344 2 my $col = _rs($_[0], 'Artist')->get_column('rank');
345 2 return [ scalar $col->min, scalar $col->max, scalar $col->sum ];
346 },
347 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
348 2 my $col = _rs($_[0], 'Artist')->get_column('rank');
349 2 ▶ return $col->min_async->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
350 2 my $min = $_[0];
351 2 ▶ return $col->max_async->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
352 2 my $max = $_[0];
353 2 ▶ return $col->sum_async->then(sub { [ $min, $max, $_[0] ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
354 2 });
355 2 });
356 },
357 4 ▶ norm => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
358 23 };
359  
360 push @case, {
361 no => 19, name => 'rsc-func', kind => 'direct',
362 doc => 'an aggregate function by name',
363 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
364 2 return scalar _rs($_[0], 'Artist')->get_column('rank')->func('MAX');
365 },
366 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
367 2 return _rs($_[0], 'Artist')->get_column('rank')->func_async('MAX');
368 },
369 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
370 23 };
371  
372 # ----------------------------------------------------------------- writes
373 push @case, {
374 no => 20, name => 'create', kind => 'direct',
375 doc => 'create a row',
376 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
377 2 return _rs($_[0], 'Artist')->create({ name => 'Parity Created', rank => 5 });
378 },
379 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
380 2 return _rs($_[0], 'Artist')->create_async({ name => 'Parity Created', rank => 5 });
381 },
382 23 norm => _picks(qw(name rank)),
383 observe => _count_named('Artist', name => 'Parity Created'),
384 };
385  
386 4 ▶ my $cascade = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
387 return {
388 4 artist => { name => 'Parity Cascade Artist', rank => 7 },
389 title => 'Parity Cascade CD',
390 year => 2020,
391 tracks => [ { position => 1, title => 'Parity Cascade Track' } ],
392 };
393 23 };
394 push @case, {
395 no => 21, name => 'create-cascade', kind => 'composed',
396 doc => 'create with nested data: related rows in one multi-create',
397 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
398 2 return _rs($_[0], 'CD')->create($cascade->());
399 },
400 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
401 2 return _rs($_[0], 'CD')->create_async($cascade->());
402 },
403 4 ▶ norm => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
404 4 [ map { { title => $_->title, year => $_->year, artist => $_->artist->name } } @_ ];
405 },
406 23 observe => _count_named('Artist', name => 'Parity Cascade Artist'),
407 };
408  
409 push @case, {
410 no => 22, name => 'row-insert', kind => 'direct',
411 doc => 'insert a row built with new_result',
412 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
413 2 return _rs($_[0], 'Artist')->new_result({ name => 'Parity Inserted', rank => 6 })->insert;
414 },
415 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
416 2 return _rs($_[0], 'Artist')->new_result({ name => 'Parity Inserted', rank => 6 })->insert_async;
417 },
418 4 ▶ norm => sub { [ map { { %{ _pick($_, qw(name rank)) }, in_storage => $_->in_storage ? 1 : 0 } } @_ ] },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_->in_storage ? : 4 0 4
419 23 observe => _count_named('Artist', name => 'Parity Inserted'),
420 };
421  
422 push @case, {
423 no => 23, name => 'row-update', kind => 'direct',
424 doc => 'update a fetched row',
425 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
426 2 return _artist($_[0], $SECOND)->update({ rank => 99 });
427 },
428 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
429 2 return _artist($_[0], $SECOND)->update_async({ rank => 99 });
430 },
431 23 norm => _picks(qw(name rank)),
432 observe => _count_named('Artist', rank => 99),
433 };
434  
435 push @case, {
436 no => 24, name => 'row-update-rs-loop', kind => 'composed',
437 doc => 'update of a set: ResultSet update has no async twin, so row by row',
438 2 ▶ sync => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
439 2 my @rows = _rs($_[0], 'CD')->search({ year => 2001 }, { order_by => 'title' })->all;
440 2 $_->update({ year => 2002 }) for @rows;
441 2 return scalar @rows;
442 },
443 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
444 2 my ($s) = @_;
445 2 my $fc = $s->storage->future_class;
446 2 ▶ return _rs($s, 'CD')->search({ year => 2001 }, { order_by => 'title' })->all_async->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
447 2 my @rows = @_;
448 return _sequence(
449 $fc,
450 4 ▶ [ map { my $r = $_; sub { $r->update_async({ year => 2002 }) } } @rows ],
Subroutine __ANON__: called CC=1 SCAR=0.0
451 2 ▶ sub { scalar @rows },
Subroutine __ANON__: called CC=1 SCAR=0.0
452 2 );
453 2 });
454 },
455 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
456 23 observe => _count_named('CD', year => 2002),
457 };
458  
459 push @case, {
460 no => 25, name => 'row-delete', kind => 'direct',
461 doc => 'delete a fetched row (OneKey: a source with nothing to cascade into)',
462 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
463 2 my $row = _rs($_[0], 'OneKey')->create({ artist => 4242, cd => 1 });
464 2 return $row->delete;
465 },
466 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
467 2 my $row = _rs($_[0], 'OneKey')->create({ artist => 4242, cd => 1 });
468 2 return $row->delete_async;
469 },
470 4 ▶ norm => sub { [ map { $_->in_storage ? 1 : 0 } @_ ] },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_->in_storage ? : 0 4 4
471 23 observe => _count_named('OneKey', artist => 4242),
472 };
473  
474 push @case, {
475 no => 26, name => 'row-delete-rs-loop', kind => 'composed',
476 doc => 'delete of a set: ResultSet delete has no async twin, so row by row',
477 2 ▶ sync => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
478 2 my ($s) = @_;
479 2 _rs($s, 'Artist')->create({ name => "Parity Doomed $_", rank => 1 }) for qw(A B);
480 2 my @rows = _rs($s, 'Artist')->search({ name => { -like => 'Parity Doomed %' } })->all;
481 2 $_->delete for @rows;
482 2 return scalar @rows;
483 },
484 2 ▶ async => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
485 2 my ($s) = @_;
486 2 my $fc = $s->storage->future_class;
487 2 _rs($s, 'Artist')->create({ name => "Parity Doomed $_", rank => 1 }) for qw(A B);
488 2 ▶ return _rs($s, 'Artist')->search({ name => { -like => 'Parity Doomed %' } })->all_async->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
489 2 my @rows = @_;
490 return _sequence(
491 $fc,
492 6 ▶ [ map { my $r = $_; sub { $r->delete_async } } @rows ],
Subroutine __ANON__: called CC=1 SCAR=0.0
493 2 ▶ sub { scalar @rows },
Subroutine __ANON__: called CC=1 SCAR=0.0
494 2 );
495 2 });
496 },
497 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
498 4 ▶ observe => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
499 4 _rs($_[0], 'Artist')->search({ name => { -like => 'Parity Doomed %' } })->count;
500 },
501 23 };
502  
503 push @case, {
504 no => 27, name => 'row-copy', kind => 'direct',
505 doc => 'copy a row with new values (OneKey: no cascade_copy relationship)',
506 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
507 2 my $row = _rs($_[0], 'OneKey')->create({ artist => 4243, cd => 1 });
508 2 return $row->copy({ cd => 2 });
509 },
510 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
511 2 my $row = _rs($_[0], 'OneKey')->create({ artist => 4243, cd => 1 });
512 2 return $row->copy_async({ cd => 2 });
513 },
514 23 norm => _picks(qw(artist cd)),
515 observe => _count_named('OneKey', artist => 4243),
516 };
517  
518 push @case, {
519 no => 28, name => 'row-reload', kind => 'direct',
520 doc => 'get_from_storage and discard_changes',
521 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
522 2 my $row = _artist($_[0], $FIRST);
523 2 $row->rank(1234);
524 2 my $fresh = $row->get_from_storage;
525 2 $row->discard_changes;
526 2 return [ _pick($fresh, qw(name rank)), _pick($row, qw(name rank)) ];
527 },
528 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
529 2 my $row = _artist($_[0], $FIRST);
530 2 $row->rank(1234);
531 2 ▶ return $row->get_from_storage_async->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
532 2 my $fresh = $_[0];
533 2 ▶ return $row->discard_changes_async->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
534 2 return [ _pick($fresh, qw(name rank)), _pick($row, qw(name rank)) ];
535 2 });
536 2 });
537 },
538 4 ▶ norm => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
539 23 };
540  
541 push @case, {
542 no => 29, name => 'update-or-insert', kind => 'direct',
543 doc => 'update_or_insert of a row not in storage inserts it',
544 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
545 2 return _rs($_[0], 'Artist')->new_result({ name => 'Parity Uoi', rank => 3 })->update_or_insert;
546 },
547 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
548 2 return _rs($_[0], 'Artist')->new_result({ name => 'Parity Uoi', rank => 3 })->update_or_insert_async;
549 },
550 4 ▶ norm => sub { [ map { { %{ _pick($_, qw(name rank)) }, in_storage => $_->in_storage ? 1 : 0 } } @_ ] },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_->in_storage ? : 4 0 4
551 23 observe => _count_named('Artist', name => 'Parity Uoi'),
552 };
553  
554 push @case, {
555 no => 30, name => 'find-or-create', kind => 'direct',
556 doc => 'find_or_create of a row that is not there creates it',
557 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
558 2 return _rs($_[0], 'Artist')->find_or_create({ name => 'Parity Foc', rank => 4 });
559 },
560 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
561 2 return _rs($_[0], 'Artist')->find_or_create_async({ name => 'Parity Foc', rank => 4 });
562 },
563 23 norm => _picks(qw(name rank)),
564 observe => _count_named('Artist', name => 'Parity Foc'),
565 };
566  
567 push @case, {
568 no => 31, name => 'find-or-new', kind => 'direct',
569 doc => 'find_or_new of a row that is not there builds it, unstored',
570 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
571 2 return _rs($_[0], 'Artist')->find_or_new({ name => 'Parity For New', rank => 4 });
572 },
573 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
574 2 return _rs($_[0], 'Artist')->find_or_new_async({ name => 'Parity For New', rank => 4 });
575 },
576 4 ▶ norm => sub { [ map { { %{ _pick($_, qw(name rank)) }, in_storage => $_->in_storage ? 1 : 0 } } @_ ] },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_->in_storage ? : 0 4 4
577 23 };
578  
579 push @case, {
580 no => 32, name => 'update-or-create', kind => 'direct',
581 doc => 'update_or_create of a row that is there updates it',
582 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
583 2 return _rs($_[0], 'Artist')->update_or_create({ name => $SECOND, rank => 77 });
584 },
585 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
586 2 return _rs($_[0], 'Artist')->update_or_create_async({ name => $SECOND, rank => 77 });
587 },
588 23 norm => _picks(qw(name rank)),
589 observe => _count_named('Artist', rank => 77),
590 };
591  
592 push @case, {
593 no => 33, name => 'update-or-new', kind => 'direct',
594 doc => 'update_or_new of a row that is there updates it',
595 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
596 2 return _rs($_[0], 'Artist')->update_or_new({ name => $SECOND, rank => 88 });
597 },
598 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
599 2 return _rs($_[0], 'Artist')->update_or_new_async({ name => $SECOND, rank => 88 });
600 },
601 4 ▶ norm => sub { [ map { { %{ _pick($_, qw(name rank)) }, in_storage => $_->in_storage ? 1 : 0 } } @_ ] },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_->in_storage ? : 4 0 4
602 23 };
603  
604 push @case, {
605 no => 34, name => 'upsert', kind => 'direct',
606 doc => 'single-statement upsert (needs the supports_upsert capability, ADR 0036)',
607 3 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
608 3 return _rs($_[0], 'Artist')->upsert({ name => 'Parity Upsert', rank => 9 }, { conflict_target => ['name'] });
609 },
610 3 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
611 3 return _rs($_[0], 'Artist')->upsert_async({ name => 'Parity Upsert', rank => 9 }, { conflict_target => ['name'] });
612 },
613 23 norm => _picks(qw(name rank)),
614 observe => _count_named('Artist', name => 'Parity Upsert'),
615 };
616  
617 # ----------------------------------------------- transactions and context
618 push @case, {
619 no => 35, name => 'txn-commit', kind => 'direct',
620 doc => 'a transaction that commits: txn_do and txn_do_async',
621 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
622 2 my ($s) = @_;
623 2 my $st = $s->storage;
624 2 ▶ return $st->txn_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
625 2 $st->insert(_insert_args($s, 'Parity Txn Commit'));
626 2 return 'committed';
627 2 });
628 },
629 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
630 2 my ($s) = @_;
631 2 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
632 2 my ($ctx) = @_;
633 return $ctx->insert_async(_insert_args($s, 'Parity Txn Commit'))
634 2 ▶ ->then(sub { 'committed' });
Subroutine __ANON__: called CC=1 SCAR=0.0
635 2 });
636 },
637 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
638 23 observe => _count_named('Artist', name => 'Parity Txn Commit'),
639 };
640  
641 push @case, {
642 no => 36, name => 'txn-rollback', kind => 'direct',
643 doc => 'a transaction that fails rolls back and rethrows',
644 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
645 2 my ($s) = @_;
646 2 my $st = $s->storage;
647 2 ▶ return $st->txn_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
648 2 $st->insert(_insert_args($s, 'Parity Txn Rollback'));
649 2 DBIO::Exception->throw('parity rollback');
650 2 });
651 },
652 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
653 2 my ($s) = @_;
654 2 my $fc = $s->storage->future_class;
655 2 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
656 2 my ($ctx) = @_;
657 2 ▶ return $ctx->insert_async(_insert_args($s, 'Parity Txn Rollback'))->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
658 2 eval { DBIO::Exception->throw('parity rollback') };
659 2 return $fc->fail($@);
660 2 });
661 2 });
662 },
663 expect_error => qr/parity rollback/,
664 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
665 23 observe => _count_named('Artist', name => 'Parity Txn Rollback'),
666 };
667  
668 push @case, {
669 no => 37, name => 'txn-with-context', kind => 'composed',
670 doc => 'a ResultSet bound to the transaction context writes inside it (with_context)',
671 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
672 2 my ($s) = @_;
673 2 ▶ return $s->storage->txn_do(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
674 2 return _rs($s, 'Artist')->create({ name => 'Parity Txn Context', rank => 8 });
675 2 });
676 },
677 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
678 2 my ($s) = @_;
679 2 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
680 2 my ($ctx) = @_;
681 2 return _rs($s, 'Artist')->with_context($ctx)
682 ->create_async({ name => 'Parity Txn Context', rank => 8 });
683 2 });
684 },
685 23 norm => _picks(qw(name rank)),
686 observe => _count_named('Artist', name => 'Parity Txn Context'),
687 };
688  
689 push @case, {
690 no => 38, name => 'txn-adopts-future', kind => 'async-only',
691 doc => 'the Future a txn_do_async coderef returns is adopted: its value is the result',
692 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
693 2 my ($s) = @_;
694 2 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
695 2 my ($ctx) = @_;
696 return $ctx->insert_async(_insert_args($s, 'Parity Txn Adopt'))
697 2 ▶ ->then(sub { 'adopted' });
Subroutine __ANON__: called CC=1 SCAR=0.0
698 2 });
699 },
700 expect => [ 'adopted' ],
701 2 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
702 23 observe => _count_named('Artist', name => 'Parity Txn Adopt'),
703 };
704  
705 # ----------------------------------------------------------------- errors
706 push @case, {
707 no => 39, name => 'err-unique', kind => 'direct',
708 doc => 'a unique violation is the same exception class on both ways',
709 4 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
710 4 return _rs($_[0], 'Artist')->create({ name => $FIRST, rank => 1 });
711 },
712 4 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
713 4 return _rs($_[0], 'Artist')->create_async({ name => $FIRST, rank => 1 });
714 },
715 expect_error => 1,
716 4 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
717 23 };
718  
719 push @case, {
720 no => 40, name => 'async-on-sync-connection', kind => 'async-only',
721 doc => 'an *_async call on a connection without an async mode croaks, it does not degrade',
722 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
723 2 my ($s, $env) = @_;
724 2 return _rs($env->{sync_schema}->(), 'Artist')->all_async;
725 },
726 expect_error => qr/not an async connection/,
727 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
728 23 };
729  
730 # ---------------------------------------------------- no async twin: named
731 push @case, {
732 no => 41, name => 'populate', kind => 'composed',
733 doc => 'populate has no async twin (ResultSet, section ASYNC); stated, not invented',
734 2 ▶ sync => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
735 2 my @rows = _rs($_[0], 'Artist')->populate([
736 { name => 'Parity Populate 1', rank => 1 },
737 { name => 'Parity Populate 2', rank => 2 },
738 ]);
739 2 return scalar @rows;
740 },
741 2 ▶ async => sub {
Subroutine __ANON__: called CC=2 SCAR=13.0
742 2 my $rs = _rs($_[0], 'Artist');
743 2 ▶ DBIO::Exception->throw('ResultSet populate has no async twin: there is no populate_async')
Branch: 1 branch
BranchTrueFalseTotal
unless $rs->can("populate_async") 2 0 2
744 unless $rs->can('populate_async');
745 return $rs->populate_async([
746 { name => 'Parity Populate 1', rank => 1 },
747 { name => 'Parity Populate 2', rank => 2 },
748 0 ▶ ])->then(sub { scalar @_ == 1 && ref $_[0] eq 'ARRAY' ? scalar @{ $_[0] } : scalar @_ });
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 && ref $_[0] eq 'ARRAY' ? : 0 0 0
Condition0%0/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
749 },
750 absent => qr/populate has no async twin/,
751 2 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
752 4 ▶ observe => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
753 4 _rs($_[0], 'Artist')->search({ name => { -like => 'Parity Populate %' } })->count;
754 },
755 23 };
756  
757 23 push @case, _bracket_cases();
758 23 push @case, _failure_cases();
759 23 push @case, _pool_cases();
760 23 push @case, _transport_cases();
761  
762 23 return \@case;
763 }
764  
765 # ------------------------------------------------------------ the helpers (2)
766  
767 0 ▶sub _async { return $_[0]->storage->async }
Subroutine _async: not called CC=1 SCAR=6.9
768 2 ▶sub _fc { return $_[0]->storage->future_class }
Subroutine _fc: called CC=1 SCAR=0.0
769  
770 # The number of artists with a name, asked on a connection of its own: the
771 # follow-up every case ends with, which shows the pool still serves.
772 2 ▶sub _count_async {
Subroutine _count_async: called CC=2 SCAR=7.0
773 2 my ($s, $name) = @_;
774 2 my $rs = _rs($s, 'Artist');
775 2 ▶ $rs = $rs->search({ name => $name }) if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 2 0 2
776 2 return $rs->count_async;
777 }
778  
779 # The connections of the pool, all of them, held: a Future of the list.
780 0 ▶sub _hold_all {
Subroutine _hold_all: not called CC=3 SCAR=24.8
781 0 my ($s, $leave) = @_;
782 0 my $pool = _async($s)->pool;
783 0 ▶ my $n = $pool->max_size - ($leave // 0);
Condition0%0/2
$leave // 0
l!l
00
MC/DC0%0/1
$leave // 0
$leave
Truth table0%0/2
A
A= $leave
Aresult
11
00
784 0 ▶ return _fc($s)->done if $n < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 0 0 0
785 0 return _fc($s)->needs_all(map { $pool->acquire } 1 .. $n);
786 }
787  
788 0 ▶sub _release_all {
Subroutine _release_all: not called CC=2 SCAR=17.9
789 0 my ($s, @conn) = @_;
790 0 my $pool = _async($s)->pool;
791 0 $pool->release($_) for @conn;
792 0 return;
793 }
794  
795 # ---------------------------- pipeline, cancel (karr k396, k274, k232)
796  
797 23 ▶sub _bracket_cases {
Subroutine _bracket_cases: called CC=1 SCAR=4.8
798 23 my @case;
799  
800 push @case, {
801 no => 42, name => 'pipeline-commit', kind => 'async-only',
802 requires => ['pipeline'],
803 doc => 'a pipeline of two inserts is sent as one batch and both rows are there',
804 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
805 0 my ($s) = @_;
806 0 my $fc = _fc($s);
807 0 ▶ return _async($s)->pipeline(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
808 0 my ($p) = @_;
809 0 return $fc->needs_all(
810 $p->insert_async(_insert_args($s, 'Parity Pipe 1')),
811 $p->insert_async(_insert_args($s, 'Parity Pipe 2')),
812 );
813 0 ▶ })->then(sub { $fc->done('piped') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
814 },
815 expect => [ 'piped' ],
816 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
817 0 ▶ observe => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
818 0 _rs($_[0], 'Artist')->search({ name => { -like => 'Parity Pipe %' } })->count;
819 },
820 23 };
821  
822 # The batch fails on its second statement. The connection is retired, the
823 # open statements fail with the batch's error, and the pool serves the next
824 # caller (karr core k396).
825 push @case, {
826 no => 43, name => 'pipeline-fail', kind => 'async-only',
827 requires => ['pipeline'],
828 doc => 'a pipeline with a failing statement fails; the storage serves the next statement',
829 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
830 0 my ($s) = @_;
831 0 my $fc = _fc($s);
832 0 ▶ return _async($s)->pipeline(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
833 0 my ($p) = @_;
834 0 return $fc->needs_all(
835 $p->insert_async(_insert_args($s, 'Parity Pipe Fail')),
836 $p->insert_async($s->source('Artist'), { name => $FIRST, rank => 1 }),
837 );
838 })->then(
839 0 ▶ sub { _count_async($s)->then(sub { $fc->done('batch succeeded') }) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
840 0 ▶ sub { _count_async($s)->then(sub { $fc->done('batch failed', 'pool serves') }) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
841 0 );
842 },
843 expect => [ 'batch failed', 'pool serves' ],
844 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
845 23 };
846  
847 push @case, {
848 no => 44, name => 'cancel-txn', kind => 'async-only',
849 requires => ['cancel'],
850 doc => 'a cancelled transaction rolls back; its row is not there and the pool serves',
851 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
852 0 my ($s) = @_;
853 0 my $fc = _fc($s);
854 0 ▶ my $txn = $s->storage->txn_do_async(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
855 0 my ($ctx) = @_;
856 0 $ctx->insert_async(_insert_args($s, 'Parity Cancel Txn'));
857 0 return $fc->new;
858 0 });
859 0 $txn->cancel;
860 0 return _count_async($s, 'Parity Cancel Txn');
861 },
862 expect => [ 0 ],
863 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
864 23 };
865  
866 push @case, {
867 no => 45, name => 'cancel-statement', kind => 'async-only',
868 requires => ['cancel'],
869 doc => 'a cancelled statement gives its connection back; the pool serves',
870 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
871 0 my ($s) = @_;
872 0 _rs($s, 'Artist')->all_async->cancel;
873 0 return _count_async($s);
874 },
875 expect => [ 3 ],
876 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
877 23 };
878  
879 push @case, {
880 no => 46, name => 'cancel-batch', kind => 'async-only',
881 requires => [ 'pipeline', 'cancel' ],
882 doc => 'a cancelled pipeline retires its connection; the pool serves',
883 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
884 0 my ($s) = @_;
885 0 my $fc = _fc($s);
886 0 ▶ my $batch = _async($s)->pipeline(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
887 0 my ($p) = @_;
888 0 $p->insert_async(_insert_args($s, 'Parity Cancel Batch'));
889 0 return $fc->new;
890 0 });
891 0 $batch->cancel;
892 0 ▶ return _count_async($s)->then(sub { $fc->done('pool serves') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
893 },
894 expect => [ 'pool serves' ],
895 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
896 23 };
897  
898 23 return @case;
899 }
900  
901 # ------------------- ROLLBACK, statements in flight, disconnect (karr k395, k236)
902  
903 23 ▶sub _failure_cases {
Subroutine _failure_cases: called CC=1 SCAR=0.3
904 23 my @case;
905  
906 push @case, {
907 no => 47, name => 'rollback-failed', kind => 'async-only',
908 requires => ['fault:rollback_fails'],
909 doc => 'a ROLLBACK the server does not confirm: the transaction fails, the connection is retired, the pool serves',
910 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
911 2 my ($s, $env) = @_;
912 2 my $fc = _fc($s);
913 2 my $guard = $env->{fault}->('rollback_fails');
914 2 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
915 2 my ($ctx) = @_;
916 2 ▶ return $ctx->insert_async(_insert_args($s, 'Parity Rollback Failed'))->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
917 2 eval { DBIO::Exception->throw('parity rollback failed') };
918 2 return $fc->fail($@);
919 2 });
920 })->then(
921 0 ▶ sub { undef $guard; return $fc->done('committed') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
922 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
923 2 my ($error) = @_;
924 2 undef $guard;
925 2 ▶ return $fc->done("$error" =~ /parity rollback failed/ ? 'failed' : 'failed other');
Branch: 1 branch
BranchTrueFalseTotal
"$error" =~ /parity rollback failed/ ? : 2 0 2
926 },
927 2 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
928 2 my ($state) = @_;
929 2 ▶ return _count_async($s, 'Parity Rollback Failed')->then(sub { $fc->done($state, $_[0]) });
Subroutine __ANON__: called CC=1 SCAR=0.0
930 2 });
931 },
932 expect => [ 'failed', 0 ],
933 2 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
934 23 };
935  
936 # The context counts every statement it hands out; COMMIT waits for them.
937 push @case, {
938 no => 48, name => 'txn-statement-in-flight', kind => 'async-only',
939 doc => 'a statement still running when the coderef returns is awaited before COMMIT',
940 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
941 2 my ($s) = @_;
942 2 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
943 2 my ($ctx) = @_;
944 2 $ctx->insert_async(_insert_args($s, 'Parity In Flight'));
945 2 return 'returned at once';
946 2 });
947 },
948 expect => [ 'returned at once' ],
949 2 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
950 23 observe => _count_named('Artist', name => 'Parity In Flight'),
951 };
952  
953 push @case, {
954 no => 49, name => 'txn-statement-in-flight-fails', kind => 'async-only',
955 doc => 'a dropped statement that fails fails the transaction; nothing of it is kept',
956 2 ▶ async => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
957 2 my ($s) = @_;
958 2 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
959 2 my ($ctx) = @_;
960 2 $ctx->insert_async(_insert_args($s, 'Parity In Flight Fails'));
961 2 $ctx->insert_async($s->source('Artist'), { name => $FIRST, rank => 1 });
962 2 return 'returned at once';
963 2 });
964 },
965 expect_error => 1,
966 2 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
967 23 observe => _count_named('Artist', name => 'Parity In Flight Fails'),
968 };
969  
970 push @case, {
971 no => 50, name => 'disconnect-in-txn', kind => 'async-only',
972 requires => ['fault:disconnect'],
973 doc => 'the connection is lost in the middle of a transaction: it fails, nothing is kept, the pool serves',
974 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
975 0 my ($s, $env) = @_;
976 0 my $fc = _fc($s);
977 0 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
978 0 my ($ctx) = @_;
979 0 ▶ return $ctx->insert_async(_insert_args($s, 'Parity Disconnect'))->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
980 0 $env->{fault}->('disconnect', $ctx);
981 0 return $ctx->insert_async(_insert_args($s, 'Parity Disconnect 2'));
982 0 });
983 })->then(
984 0 ▶ sub { return $fc->done('committed') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
985 0 ▶ sub { return $fc->done('failed') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
986 0 ▶ )->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
987 0 my ($state) = @_;
988 0 ▶ return _count_async($s, 'Parity Disconnect')->then(sub { $fc->done($state, $_[0]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
989 0 });
990 },
991 expect => [ 'failed', 0 ],
992 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
993 23 };
994  
995 23 return @case;
996 }
997  
998 # ----------------------- the limits of the pool, session state (karr k397, k398)
999  
1000 23 ▶sub _pool_cases {
Subroutine _pool_cases: called CC=1 SCAR=6.0
1001 23 my @case;
1002  
1003 # A transaction holds a connection; the Future its coderef returns is a call
1004 # on the storage, which has to queue for a connection of the same pool.
1005 push @case, {
1006 no => 51, name => 'pool-self-deadlock', kind => 'async-only',
1007 requires => ['pool_queue'],
1008 doc => 'a transaction whose coderef returns a call queued for its own pool fails at once',
1009 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1010 0 my ($s) = @_;
1011 0 my $fc = _fc($s);
1012 0 my @held;
1013 0 ▶ return _hold_all($s, 1)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1014 0 @held = @_;
1015 0 ▶ return $s->storage->txn_do_async(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1016 0 return _rs($s, 'Artist')->search({ name => 'Parity Never' })->all_async;
1017 0 });
1018 })->then(
1019 0 ▶ sub { _release_all($s, @held); return $fc->done('not refused') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1020 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1021 0 my ($error) = @_;
1022 0 _release_all($s, @held);
1023 0 ▶ my $state = "$error" =~ /queued for this transaction.s own pool/ ? "refused" : "refused otherwise";
Branch: 1 branch
BranchTrueFalseTotal
"$error" =~ /queued for this transaction.s own pool/ ? : 0 0 0
1024 0 ▶ return _count_async($s)->then(sub { $fc->done($state, 'pool serves') });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1025 },
1026 0 );
1027 },
1028 expect => [ 'refused', 'pool serves' ],
1029 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1030 23 };
1031  
1032 push @case, {
1033 no => 52, name => 'pool-max-waiters', kind => 'async-only',
1034 requires => ['pool_queue'],
1035 doc => 'with the pool taken, max_waiters callers queue and the next one is refused at once',
1036 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1037 0 my ($s) = @_;
1038 0 my $fc = _fc($s);
1039 0 my $pool = _async($s)->pool;
1040 0 my @held;
1041 0 my $old = $pool->{max_waiters};
1042 0 ▶ return _hold_all($s)->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1043 0 @held = @_;
1044 0 $pool->{max_waiters} = 1;
1045 0 my $queued = $pool->acquire;
1046 0 my $refused = $pool->acquire;
1047 0 $pool->{max_waiters} = $old;
1048 0 ▶ my $state = $refused->is_failed && $refused->failure =~ /pool exhausted: 1 callers waiting/
Branch: 1 branch
BranchTrueFalseTotal
$refused->is_failed && $refused->failure =~ /pool exhausted: 1 callers waiting/ ? : 0 0 0
Condition0%0/3
$refused->is_failed and $refused->failure =~ /pool exhausted: 1 callers waiting/
!ll&&!rl&&r
000
MC/DC0%0/2
$refused->is_failed and $refused->failure =~ /pool exhausted: 1 callers waiting/
$refused->is_failed $refused->failure =~ /pool exhausted: 1 callers waiting/
Truth table0%0/3
A and B
A= $refused->is_failedB= $refused->failure =~ /pool exhausted: 1 callers waiting/
ABresult
0X0
100
111
1049 ? 'refused' : 'not refused';
1050 0 _release_all($s, @held);
1051 0 ▶ return $queued->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1052 0 my ($conn) = @_;
1053 0 $pool->release($conn);
1054 0 return $fc->done('queued', $state);
1055 0 });
1056 0 });
1057 },
1058 expect => [ 'queued', 'refused' ],
1059 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1060 23 };
1061  
1062 push @case, {
1063 no => 53, name => 'pool-acquire-timeout', kind => 'async-only',
1064 requires => ['pool_queue'],
1065 doc => 'with the pool taken, a caller that waits longer than acquire_timeout fails',
1066 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1067 0 my ($s) = @_;
1068 0 my $fc = _fc($s);
1069 0 my $pool = _async($s)->pool;
1070 0 my @held;
1071 0 my $old = $pool->{acquire_timeout};
1072 0 ▶ return _hold_all($s)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1073 0 @held = @_;
1074 0 $pool->{acquire_timeout} = 0.2;
1075 0 my $waiter = $pool->acquire;
1076 0 $pool->{acquire_timeout} = $old;
1077 return $waiter->then(
1078 0 ▶ sub { $pool->release($_[0]); return $fc->done('served') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1079 0 ▶ sub { return $fc->done("$_[0]" =~ /no pool connection within/ ? 'timed out' : 'failed otherwise') },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
"$_[0]" =~ /no pool connection within/ ? : 0 0 0
1080 0 );
1081 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1082 0 my ($state) = @_;
1083 0 _release_all($s, @held);
1084 0 return $fc->done($state);
1085 0 });
1086 },
1087 expect => [ 'timed out' ],
1088 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1089 23 };
1090  
1091 # Session state of one borrower never reaches the next (ADR 0079). A
1092 # temporary table is session state; every connection of the pool gets one and
1093 # is given back, then every connection is asked for it.
1094 push @case, {
1095 no => 54, name => 'session-reset', kind => 'async-only',
1096 requires => ['pool_queue'],
1097 doc => 'a connection given back is reset: the session state of its last borrower is gone',
1098 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1099 0 my ($s, $env) = @_;
1100 0 my $fc = _fc($s);
1101 0 my $async = _async($s);
1102 0 my ($set, $probe) = @{ $env->{session_sql} };
1103 0 my @held;
1104 0 ▶ return _hold_all($s)->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1105 0 @held = @_;
1106 0 return $fc->needs_all(map { $async->_query_async_pinned($_, $set, []) } @held);
1107 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1108 0 _release_all($s, @held);
1109 0 return _hold_all($s);
1110 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1111 0 @held = @_;
1112 return $fc->needs_all(map {
1113 0 $async->_query_async_pinned($_, $probe, [])->then(
1114 0 ▶ sub { $fc->done('seen') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1115 0 ▶ sub { $fc->done('gone') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1116 0 );
1117 } @held);
1118 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1119 0 my @seen = @_;
1120 0 _release_all($s, @held);
1121 0 ▶ return $fc->done((grep { $_ ne 'gone' } @seen) ? 'leaked' : 'reset');
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ ne 'gone';} @seen) ? : 0 0 0
1122 0 });
1123 },
1124 expect => [ 'reset' ],
1125 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1126 23 };
1127  
1128 23 return @case;
1129 }
1130  
1131 # ----------------------------- LISTEN/NOTIFY, a connection that cannot come up
1132  
1133 23 ▶sub _transport_cases {
Subroutine _transport_cases: called CC=1 SCAR=5.3
1134 23 my @case;
1135  
1136 push @case, {
1137 no => 55, name => 'listen-notify', kind => 'async-only',
1138 requires => [ 'listen', 'notify' ],
1139 doc => 'a notification sent on one connection arrives at the listener, with its payload',
1140 0 ▶ async => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1141 0 my ($s) = @_;
1142 0 my $fc = _fc($s);
1143 0 my $async = _async($s);
1144 0 my $got = $fc->new;
1145 0 my $channel = 'parity_listen';
1146 0 ▶ return $async->listen($channel, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1147 0 my ($chan, $payload) = @_;
1148 0 ▶ $got->done($chan, $payload) unless $got->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $got->is_ready 0 0 0
1149 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1150 0 return $async->notify($channel, 'parity payload');
1151 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1152 0 return $got;
1153 0 ▶ })->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1154 0 my @answer = @_;
1155 0 ▶ return $async->unlisten($channel)->then(sub { $fc->done(@answer) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1156 0 });
1157 },
1158 expect => [ 'parity_listen', 'parity payload' ],
1159 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1160 23 };
1161  
1162 # The error of a connection that cannot come up reaches the caller without
1163 # the password the connection was made with (karr k305, k400).
1164 push @case, {
1165 no => 56, name => 'connect-error-masked', kind => 'async-only',
1166 requires => ['unreachable_connection'],
1167 doc => 'the error of a connection that cannot be made never carries the connection\'s password',
1168 0 ▶ async => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1169 0 my ($s, $env) = @_;
1170 0 my $fc = _fc($s);
1171 0 my ($schema, $secret) = $env->{unreachable}->();
1172 # the failure arrives as a failed Future; an exception out of the call
1173 # itself is a deviation and counts as a failure of the case
1174 0 my $future = eval { _rs($schema, 'Artist')->count_async };
1175 0 ▶ return $fc->done('thrown') if !$future;
Branch: 1 branch
BranchTrueFalseTotal
unless $future 0 0 0
1176 return $future->then(
1177 0 ▶ sub { return $fc->done('connected') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1178 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1179 0 my ($error) = @_;
1180 0 ▶ return $fc->done(index("$error", $secret) < 0 ? 'masked' : 'leaked');
Branch: 1 branch
BranchTrueFalseTotal
index("$error", $secret) == -1 ? : 0 0 0
1181 },
1182 0 );
1183 },
1184 expect => [ 'masked' ],
1185 0 ▶ norm => sub { [ @_ ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1186 23 };
1187  
1188 23 return @case;
1189 }
1190  
1191 5 1;
1192  
1193 __END__
« blib/lib/DBIO/ChangeLog/Entry.pm ↑ Summary blib/lib/DBIO/Storage/Async/PipelineContext.pm »