blib/lib/DBIO/Test/AsyncHarness.pm

filter: Statementstmt 76.7% 152 / 198 Branchbran 67.3% 66 / 98 Conditioncond 60.0% 45 / 75 MC/DCmcdc 35.7% 15 / 42 Subroutinesub 82.7% 24 / 29 Podpod 100% 14 / 14 totaltotal 69.3% 316 / 456 CC 81 SCAR 32.9
CC
81
Cov
71%
run_on_each_pooled_connection
66.5
await
46.2
skip_all_unless_live
20.0
CRAP 26.8
« blib/lib/DBIO/Ordered.pm ↑ Summary blib/lib/DBIO/SQLMaker/ClassicExtensions.pm »
1 package DBIO::Test::AsyncHarness;
2 # ABSTRACT: Parameterised live harness base for DBIO async transports
3  
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 our $VERSION = '0.900007';
8  
9 9 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Test::More ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use base 'Class::Accessor::Grouped';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18  
19 9 __PACKAGE__->mk_group_accessors(inherited => qw(
20 harness_env_prefix
21 harness_async_mode
22 harness_required_modules
23 harness_schema_class
24 harness_default_timeout
25 ));
26  
27 9 __PACKAGE__->harness_required_modules([]);
28 9 __PACKAGE__->harness_default_timeout(30);
29  
30 # The declarations are class data with no sensible generic default, so read
31 # them through one place that says which subclass forgot which one, rather than
32 # letting an undef reach an environment lookup or a ->connect and fail there.
33 15 ▶sub _harness_declaration {
Subroutine _harness_declaration: called CC=4 SCAR=14.3
34 15 my ($class, $name) = @_;
35 15 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0150
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
36 15 my $value = $class->$name;
37 15 ▶ croak "$class must declare $name (see DBIO::Test::AsyncHarness)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value ne "" 2 13 15
Condition66%2/3
defined $value and $value ne ""
!ll&&!rl&&r
2013
MC/DC50%1/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table66%2/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
38 unless defined $value && $value ne '';
39 13 return $value;
40 }
41  
42  
43 12 ▶sub env_var {
Subroutine env_var: called CC=1 SCAR=0.0
Pod documented
44 12 my ($class, $suffix) = @_;
45 12 return $class->_harness_declaration('harness_env_prefix') . '_' . $suffix;
46 }
47  
48  
49 4 ▶sub live_available {
Subroutine live_available: called CC=5 SCAR=16.5
Pod documented
50 4 my $class = shift;
51 4 ▶ return 0 unless $ENV{ $class->env_var('DSN') };
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{$class->env_var("DSN")} 1 3 4
52 3 ▶ for my $module (@{ $class->harness_required_modules || [] }) {
Condition50%1/2
$class->harness_required_modules || []
l!l
30
MC/DC0%0/1
$class->harness_required_modules || []
$class->harness_required_modules
Truth table50%1/2
A
A= $class->harness_required_modules
Aresult
11
00
53 2 (my $file = $module) =~ s{::}{/}g;
54 0 ▶ return 0 unless eval { require "$file.pm"; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require "$file.pm"; 1 } } 2 0 2
55 }
56 1 return 1;
57 }
58  
59  
60 0 ▶sub skip_all_unless_live {
Subroutine skip_all_unless_live: not called CC=4 SCAR=30.0
Pod documented
61 0 my $class = shift;
62 0 ▶ return if $class->live_available;
Branch: 1 branch
BranchTrueFalseTotal
if $class->live_available 0 0 0
63  
64 0 ▶ my @modules = @{ $class->harness_required_modules || [] };
Condition0%0/2
$class->harness_required_modules || []
l!l
00
MC/DC0%0/1
$class->harness_required_modules || []
$class->harness_required_modules
Truth table0%0/2
A
A= $class->harness_required_modules
Aresult
11
00
65 0 ▶ Test::More::plan(skip_all =>
Branch: 1 branch
BranchTrueFalseTotal
@modules ? : 0 0 0
66 'Set ' . $class->env_var('DSN')
67 . (@modules ? ' (and install ' . join(', ', @modules) . ')' : '')
68 . ' to run the live '
69 . $class->_harness_declaration('harness_async_mode')
70 . ' harness tests');
71 }
72  
73  
74 3 ▶sub new {
Subroutine new: called CC=10 SCAR=26.4
Pod documented
75 3 my ($class, %opts) = @_;
76  
77 3 ▶ croak "$class->new called but "
Branch: 1 branch
BranchTrueFalseTotal
unless $class->live_available 1 2 3
78 . $class->env_var('DSN') . ' is unset or the transport is missing'
79 . ' -- gate with skip_all_unless_live'
80 unless $class->live_available;
81  
82 my $self = bless {
83 dsn => $opts{dsn} // $ENV{ $class->env_var('DSN') },
84 user => $opts{user} // $ENV{ $class->env_var('USER') } // '',
85 pass => $opts{pass} // $ENV{ $class->env_var('PASS') } // '',
86 schema_class => $opts{schema_class}
87 // $class->_harness_declaration('harness_schema_class'),
88 storage_type => $opts{storage_type},
89 timeout => $opts{timeout},
90 2 ▶ }, $class;
Condition37%6/16
$opts{"dsn"} // $ENV{$class->env_var("DSN")}
l!l&&r!l&&!r
200
($opts{"user"} // $ENV{$class->env_var("USER")}) // ""
l!l
02
$opts{"user"} // $ENV{$class->env_var("USER")}
l!l&&r!l&&!r
002
($opts{"pass"} // $ENV{$class->env_var("PASS")}) // ""
l!l
02
$opts{"pass"} // $ENV{$class->env_var("PASS")}
l!l&&r!l&&!r
002
$opts{"schema_class"} // $class->_harness_declaration("harness_schema_class")
l!l&&r!l&&!r
200
MC/DC0%0/2
$opts{"dsn"} // $ENV{$class->env_var("DSN")}
$opts{"dsn"} $ENV{$class->env_var("DSN")}
MC/DC0%0/2
($opts{"user"} // $ENV{$class->env_var("USER")}) // ""
$opts{"user"} $ENV{$class->env_var("USER")}
MC/DC0%0/2
($opts{"pass"} // $ENV{$class->env_var("PASS")}) // ""
$opts{"pass"} $ENV{$class->env_var("PASS")}
MC/DC0%0/2
$opts{"schema_class"} // $class->_harness_declaration("harness_schema_class")
$opts{"schema_class"} $class->_harness_declaration("harness_schema_class")
Truth table33%1/3
A // B
A= $opts{"dsn"}B= $ENV{$class->env_var("DSN")}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $opts{"user"}B= $ENV{$class->env_var("USER")}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $opts{"pass"}B= $ENV{$class->env_var("PASS")}
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $opts{"schema_class"}B= $class->_harness_declaration("harness_schema_class")
ABresult
1X1
011
000
91  
92 # The driver, declared on a clone so the schema class stays undeclared
93 # (ADR 0067 ยง4); without one the schema class declares its own.
94 2 my $schema = $self->{schema_class};
95 2 ▶ if (defined $self->{storage_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"storage_type"}) 1 1 2
96 1 $schema = $schema->clone;
97 1 $schema->storage_type($self->{storage_type});
98 }
99  
100 # Connect an ordinary sync schema, opting THE CONNECTION into the async mode
101 # (ADR 0030: the mode is per connection, not per class). The connect actions
102 # live on the owning sync storage; the embedded backend's pool replays them
103 # per physical connection.
104 $self->{schema} = $schema->connect(
105 $self->{dsn}, $self->{user}, $self->{pass},
106 2 ▶ { %{ $opts{connect_attrs} // {} },
Condition50%1/2
$opts{"connect_attrs"} // {}
l!l
02
MC/DC0%0/1
$opts{"connect_attrs"} // {}
$opts{"connect_attrs"}
Truth table50%1/2
A
A= $opts{"connect_attrs"}
Aresult
11
00
107 async => $class->_harness_declaration('harness_async_mode') },
108 );
109  
110 1 return $self;
111 }
112  
113  
114 9 ▶sub schema { $_[0]->{schema} }
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
115  
116  
117 1 ▶sub sync_storage { $_[0]->{schema}->storage }
Subroutine sync_storage: called CC=1 SCAR=0.0
Pod documented
118  
119  
120 8 ▶sub async { $_[0]->{schema}->storage->async }
Subroutine async: called CC=1 SCAR=0.0
Pod documented
121  
122  
123 0 ▶sub pool { $_[0]->async->pool }
Subroutine pool: not called CC=1 SCAR=6.9
Pod documented
124  
125  
126 442 ▶sub timeout { $_[0]->{timeout} // $_[0]->harness_default_timeout }
Subroutine timeout: called CC=2 SCAR=7.2
Pod documented
Condition66%2/3
$_[0]{"timeout"} // $_[0]->harness_default_timeout
l!l&&r!l&&!r
43660
MC/DC0%0/2
$_[0]{"timeout"} // $_[0]->harness_default_timeout
$_[0]{"timeout"} $_[0]->harness_default_timeout
Truth table66%2/3
A // B
A= $_[0]{"timeout"}B= $_[0]->harness_default_timeout
ABresult
1X1
011
000
127  
128 # An outer alarm that is due before ours is waited for until it has FIRED, not
129 # for a guessed time. A timer is not delivered the instant it is due everywhere:
130 # on the BSDs and macOS the signal follows the expiry by up to a tick of the
131 # kernel clock or a timer-coalescing delay, and in that gap the timer is
132 # reported as 0 left -- and taking it off the clock then (alarm(0)) cancels the
133 # signal, which is never delivered. On Cygwin a timer that has fired reports a
134 # NEGATIVE time left. So "no time left" is not "fired"; await watches the
135 # handler run instead. This is only the ceiling for the case where no signal
136 # will ever come, because somebody cleared the alarm inside the loop: generous,
137 # since a signal that is merely late must never be mistaken for one that is not
138 # coming.
139 9 my $FALLBACK = 10;
140  
141 # The alarm of the timeout is set this much later than the timeout the clock
142 # keeps. Long enough that a loop that turns is out of the wait, by the clock,
143 # before the alarm is due; short enough that a loop hanging in a C call ends
144 # not noticeably later.
145 9 my $ALARM_GRACE = 0.25;
146  
147 # The longest await waits, after the wait, for the signal of an alarm that is
148 # reported as expired and whose handler has not run. Short: where the system
149 # discards the signal together with the alarm, none comes.
150 9 my $SIGNAL_WAIT = 0.25;
151  
152  
153 439 ▶sub await {
Subroutine await: called CC=39 SCAR=38.3
Pod documented
154 439 my ($self, $f) = @_;
155  
156 # The process has ONE alarm, and it may be somebody's already: a test file
157 # guarding itself against a hang, or an await further out whose callback
158 # called this one. So the alarm is borrowed: what is running is taken off
159 # the clock here, and is back on it on EVERY way out, due when it was due.
160 # Times are absolute, by Time::HiRes: perl's own alarm answers
161 # what is left rounded to a whole second, 9.8 as 10, and putting that back
162 # after each await pushes the alarm ahead of a row of quick awaits for good.
163 439 my $timeout = $self->timeout;
164 439 my $entered = Time::HiRes::time();
165 439 my $left = _alarm(0);
166 439 ▶ my $outer_at = $left > 0 ? $entered + $left : undef;
Branch: 1 branch
BranchTrueFalseTotal
$left > 0 ? : 332 107 439
167 439 ▶ my $ours_at = $timeout ? $entered + $timeout : undef;
Branch: 1 branch
BranchTrueFalseTotal
$timeout ? : 436 3 439
168  
169 439 my $err;
170 WAIT: {
171 # An outer alarm due before ours is not ours to answer. It goes back on
172 # the clock as it was and fires at its time under the handler it was set
173 # with -- or under none, and ends the process. No handler of ours here.
174 439 ▶ if (defined $outer_at and !defined $ours_at || $outer_at < $ours_at) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outer_at and !defined($ours_at) || $outer_at < $ours_at) 164 279 443
Condition100%6/6
defined $outer_at and !defined($ours_at) || $outer_at < $ours_at
!ll&&!rl&&r
111168164
not defined $ours_at or $outer_at < $ours_at
l!l&&r!l&&!r
2162168
MC/DC100%3/3
defined $outer_at and !defined($ours_at) || $outer_at < $ours_at
defined $outer_at not defined $ours_at $outer_at < $ours_at
Truth table100%4/4
A and not(B) or C
A= defined $outer_atB= not defined $ours_atC= $outer_at < $ours_at
ABCresult
0XX0
1000
11X1
1011
175 # The alarm is taken off the clock only once it has fired: a kernel may
176 # report a timer whose time is past as 0 left while its signal is still
177 # on the way, and taking it off the clock then would cancel it (see
178 # $FALLBACK). Whether it fired is seen at its handler.
179 164 my $handler = $SIG{ALRM};
180 164 ▶ my $mode = !defined $handler || $handler eq '' || $handler eq 'DEFAULT' ? 'default'
Branch: 2 branches
BranchTrueFalseTotal
!defined($handler) || $handler eq '' || $handler eq 'DEFAULT' ? : 0 164 164
$handler eq 'IGNORE' ? : 0 164 164
Condition33%2/6
not defined $handler or $handler eq "" or $handler eq "DEFAULT"
l!l&&r!l&&!r
00164
not defined $handler or $handler eq ""
l!l&&r!l&&!r
00164
MC/DC0%0/3
not defined $handler or $handler eq "" or $handler eq "DEFAULT"
not defined $handler $handler eq "" $handler eq "DEFAULT"
Truth table25%1/4
not(A) or B or C
A= not defined $handlerB= $handler eq ""C= $handler eq "DEFAULT"
ABCresult
1XX1
01X1
0011
0000
181 : $handler eq 'IGNORE' ? 'ignore' : 'code';
182 164 my $fired = 0;
183 164 local $SIG{ALRM} = $handler;
184 164 ▶ if ($mode eq 'code') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "code") 164 0 164
185 164 my $orig = $handler;
186 164 ▶ unless (ref $orig) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $orig) 0 164 164
187 0 ▶ my $name = $orig =~ /::/ ? $orig : "main::$orig";
Branch: 1 branch
BranchTrueFalseTotal
$orig =~ /::/ ? : 0 0 0
188 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 0 $orig = \&{$name};
190 }
191 164 ▶ $SIG{ALRM} = sub { $fired = 1; $orig->(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
192 }
193 # A delivered IGNORE is discarded, so after its time it counts as fired.
194 164 ▶ my $give_up = $mode eq 'ignore' ? $outer_at : $outer_at + $FALLBACK;
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'ignore' ? : 0 164 164
195 164 _alarm_at($outer_at);
196 164 my $ready;
197 164 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { while (1) { last if $ready = $f->is_ready or $fired; my $now = &Time::HiRes::time(); if (defined $ours_at and $now >= $ours_at) { $err = 'TIMEOUT awaiting Future in ' . ref($self) . "\n"; last; } ; last if $now >= $give_up; $self->_tick_loop; } ; 1 } } or $err = $@ 157 7 164
198 164 while (1) {
199 1000 ▶ last if ($ready = $f->is_ready) || $fired;
Branch: 1 branch
BranchTrueFalseTotal
if $ready = $f->is_ready or $fired 157 843 1000
Condition100%3/3
$ready = $f->is_ready or $fired
l!l&&r!l&&!r
1534843
MC/DC100%2/2
$ready = $f->is_ready or $fired
$ready = $f->is_ready $fired
Truth table100%3/3
A or B
A= $ready = $f->is_readyB= $fired
ABresult
1X1
011
000
200 843 my $now = Time::HiRes::time();
201 843 ▶ if (defined $ours_at and $now >= $ours_at) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ours_at and $now >= $ours_at) 0 843 843
Condition66%2/3
defined $ours_at and $now >= $ours_at
!ll&&!rl&&r
358080
MC/DC0%0/2
defined $ours_at and $now >= $ours_at
defined $ours_at $now >= $ours_at
Truth table66%2/3
A and B
A= defined $ours_atB= $now >= $ours_at
ABresult
0X0
100
111
202 0 $err = "TIMEOUT awaiting Future in " . ref($self) . "\n";
203 0 last;
204 }
205 843 ▶ last if $now >= $give_up;
Branch: 1 branch
BranchTrueFalseTotal
if $now >= $give_up 0 843 843
206 843 $self->_tick_loop;
207 }
208 157 1;
209 } or $err = $@;
210 164 ▶ last WAIT if defined $err || $ready;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err or $ready 160 4 164
Condition100%3/3
defined $err or $ready
l!l&&r!l&&!r
71534
MC/DC100%2/2
defined $err or $ready
defined $err $ready
Truth table100%3/3
A or B
A= defined $errB= $ready
ABresult
1X1
011
000
211  
212 # It was due and the wait goes on: its handler returned. What is left
213 # of the wait is under our timeout, counted from when we were entered.
214 4 $left = _alarm(0);
215 4 ▶ $outer_at = $left > 0 ? Time::HiRes::time() + $left : undef;
Branch: 1 branch
BranchTrueFalseTotal
$left > 0 ? : 0 4 4
216 4 redo WAIT;
217 }
218  
219 {
220 # Our handler dies only while the wait is going on ($in_wait); a signal
221 # at any other time, one that is late included, is only noted. It clears
222 # the flag as it dies, so a wait is cut short once and never twice.
223 279 my ($got, $in_wait) = (0, 0);
224 3 ▶ local $SIG{ALRM} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
225 3 $got = 1;
226 3 ▶ return unless $in_wait;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_wait 0 3 3
227 3 $in_wait = 0;
228 3 die "TIMEOUT awaiting Future in " . ref($self) . "\n";
229 279 };
230 # The clock ends the wait at the timeout; the alarm is due a grace later
231 # (see $ALARM_GRACE), the addition for a _tick_loop that blocks in a C
232 # call and for a platform that delivers a signal only now and then.
233 279 ▶ my $alarm_due = defined $ours_at ? _alarm_at($ours_at + $ALARM_GRACE) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $ours_at ? : 278 1 279
234 # The outer eval holds the gap between the end of the inner one and the
235 # clearing of the flag, where the handler may still die; what it catches
236 # there never replaces the error of the inner one, and is the error when
237 # the inner one has none (the signal came before it could be taken).
238 279 my $ok;
239 279 eval {
240 279 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $in_wait = 1; until ($f->is_ready) { die 'TIMEOUT awaiting Future in ' . ref($self) . "\n" if defined $ours_at and &Time::HiRes::time() >= $ours_at; $self->_tick_loop; } ; $ok = 1 } } or $err = $@ 257 22 279
241 279 $in_wait = 1;
242 279 ▶ until ($f->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f->is_ready) 525650 257 525907
243 525650 ▶ die "TIMEOUT awaiting Future in " . ref($self) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $ours_at and &Time::HiRes::time() >= $ours_at 16 525634 525650
Condition100%3/3
defined $ours_at and &Time::HiRes::time() >= $ours_at
!ll&&!rl&&r
552562916
MC/DC100%2/2
defined $ours_at and &Time::HiRes::time() >= $ours_at
defined $ours_at &Time::HiRes::time() >= $ours_at
Truth table100%3/3
A and B
A= defined $ours_atB= &Time::HiRes::time() >= $ours_at
ABresult
0X0
100
111
244 if defined $ours_at and Time::HiRes::time() >= $ours_at;
245 525634 $self->_tick_loop;
246 }
247 257 $ok = 1;
248 } or $err = $@;
249 279 $in_wait = 0;
250 279 1;
251 };
252 279 my $gap = $@;
253 279 ▶ $err = $gap if !$ok and !defined $err and length $gap;
Branch: 1 branch
BranchTrueFalseTotal
if not $ok || defined $err and length $gap 0 279 279
Condition50%3/6
not $ok || defined $err and length $gap
!ll&&!rl&&r
27900
$ok || defined $err
l!l&&r!l&&!r
257220
MC/DC0%0/3
not $ok || defined $err and length $gap
$ok defined $err length $gap
Truth table50%2/4
not(A || B) and C
A= $okB= defined $errC= length $gap
ABCresult
1XX0
01X0
0000
0011
254 # Our alarm is off the clock on every way out of the wait, a die inside
255 # _tick_loop included: left running it would fire later, under whatever
256 # handler is there then, far away from the test that set it.
257 279 ▶ if (defined $ours_at) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ours_at) 278 1 279
258 278 my $rest = _alarm(0);
259 # Expired by its report and by the clock, and its handler has not run:
260 # the signal may still be on the way. Wait for it, bounded; where it
261 # was discarded with the alarm, none comes.
262 278 ▶ if ($rest <= 0 and !$got and Time::HiRes::time() >= $alarm_due) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest <= 0 and not $got and &Time::HiRes::time() >= $alarm_due) 1 277 278
Condition83%5/6
$rest <= 0 and not $got and &Time::HiRes::time() >= $alarm_due
!ll&&!rl&&r
27701
$rest <= 0 and not $got
!ll&&!rl&&r
27431
MC/DC66%2/3
$rest <= 0 and not $got and &Time::HiRes::time() >= $alarm_due
$rest <= 0 not $got &Time::HiRes::time() >= $alarm_due
Truth table75%3/4
A and not(B) and C
A= $rest <= 0B= not $gotC= &Time::HiRes::time() >= $alarm_due
ABCresult
0XX0
10X0
1100
1111
263 1 my $until = Time::HiRes::time() + $SIGNAL_WAIT;
264 1 ▶ Time::HiRes::sleep(0.01) until $got or Time::HiRes::time() >= $until;
Branch: 1 branch
BranchTrueFalseTotal
($got or &Time::HiRes::time() >= $until) or do { &Time::HiRes::sleep("0.01") } 1 25 26
Condition66%2/3
$got or &Time::HiRes::time() >= $until
l!l&&r!l&&!r
0125
MC/DC50%1/2
$got or &Time::HiRes::time() >= $until
$got &Time::HiRes::time() >= $until
Truth table66%2/3
A or B
A= $gotB= &Time::HiRes::time() >= $until
ABresult
1X1
011
000
265 }
266 }
267 # $SIG{ALRM} is reset when this block ends, with nothing in flight.
268 }
269 # ... and the outer one back on it -- after our handler is gone, so that
270 # one whose time has come meanwhile fires under its own.
271 279 ▶ _alarm_at($outer_at) if defined $outer_at;
Branch: 1 branch
BranchTrueFalseTotal
if defined $outer_at 168 111 279
272 }
273 439 ▶ croak $err if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 29 410 439
274  
275 410 return $f->get;
276 }
277  
278 # Whether Time::HiRes::alarm works here: where the platform has no setitimer
279 # (Windows) it is there and croaks "unimplemented in this platform" when
280 # called, so only a call tells. Asked once, when this file is loaded, and an
281 # alarm that is running then is put back.
282 9 my $HIRES_ALARM = do {
283 9 local ($@, $SIG{__DIE__});
284 9 my $left;
285 9 ▶ eval { $left = Time::HiRes::alarm(0); Time::HiRes::alarm($left) if $left > 0; 1 } ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
eval { do { $left = &Time::HiRes::alarm(0); &Time::HiRes::alarm($left) if $left > 0; 1 } } ? : 8 1 9
if $left > 0 0 8 8
286 };
287 1941 ▶sub _hires_alarm { $HIRES_ALARM }
Subroutine _hires_alarm: called CC=1 SCAR=0.0
288  
289 # The process alarm in seconds, with Time::HiRes::alarm's contract: it returns
290 # what was left of the one running, 0 for none, and 0 clears. Without a timer
291 # of fractions the alarm of perl itself does it, in whole seconds rounded UP --
292 # never early, so a wait is never cut short -- and what it answers is whole
293 # seconds too.
294 1331 ▶sub _alarm {
Subroutine _alarm: called CC=5 SCAR=16.1
295 1331 my ($secs) = @_;
296 1331 ▶ if (_hires_alarm()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_hires_alarm()) 1312 19 1331
297 1312 my $left = Time::HiRes::alarm($secs);
298 1312 ▶ return $left if $secs or $left > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $secs or $left > 0 1202 110 1312
Condition100%3/3
$secs or $left > 0
l!l&&r!l&&!r
603599110
MC/DC100%2/2
$secs or $left > 0
$secs $left > 0
Truth table100%3/3
A or B
A= $secsB= $left > 0
ABresult
1X1
011
000
299 # Some systems keep the alarm of perl apart from the timer of
300 # Time::HiRes (Solaris): clearing one leaves the other running. Taken off
301 # too, and its time left, in whole seconds, is what there is.
302 110 return CORE::alarm(0);
303 }
304 19 my $whole = int($secs);
305 19 ▶ $whole++ if $whole < $secs;
Branch: 1 branch
BranchTrueFalseTotal
if $whole < $secs 7 12 19
306 19 return CORE::alarm($whole);
307 }
308  
309 # Set the process alarm to go off at $at, a Time::HiRes::time; at once when
310 # that is past -- the shortest alarm that still is one, since 0 clears it.
311 # Returns a time by which it has gone off.
312 610 ▶sub _alarm_at {
Subroutine _alarm_at: called CC=4 SCAR=13.9
313 610 my ($at) = @_;
314 610 my $in = $at - Time::HiRes::time();
315 610 ▶ $in = 0.001 if $in < 0.001;
Branch: 1 branch
BranchTrueFalseTotal
if $in < "0.001" 1 609 610
316 610 _alarm($in);
317 610 ▶ $in = int($in) + ($in > int($in) ? 1 : 0) unless _hires_alarm();
Branch: 2 branches
BranchTrueFalseTotal
unless _hires_alarm() 7 603 610
$in > int $in ? : 7 0 7
318 610 return Time::HiRes::time() + $in;
319 }
320  
321  
322 1 ▶sub _tick_loop {
Subroutine _tick_loop: called CC=1 SCAR=0.0
323 1 my $self = shift;
324 1 croak ref($self) . ' must override _tick_loop to drive its event loop'
325 . ' (see DBIO::Test::AsyncHarness)';
326 }
327  
328  
329 0 ▶sub query_on {
Subroutine query_on: not called CC=2 SCAR=17.9
Pod documented
330 0 my ($self, $conn, $sql, $bind) = @_;
331 0 ▶ return $self->async->_query_async_pinned($conn, $sql, $bind // []);
Condition0%0/2
$bind // []
l!l
00
MC/DC0%0/1
$bind // []
$bind
Truth table0%0/2
A
A= $bind
Aresult
11
00
332 }
333  
334  
335 0 ▶sub warm_pool {
Subroutine warm_pool: not called CC=3 SCAR=24.8
Pod documented
336 0 my $self = shift;
337 0 my $pool = $self->pool;
338 0 my @conns = map { $self->await($pool->acquire) } 1 .. $pool->max_size;
339 0 ▶ my @leases = map { $pool->can('_lease') ? $pool->_lease($_) : undef } @conns;
Branch: 1 branch
BranchTrueFalseTotal
$pool->can('_lease') ? : 0 0 0
340 0 $pool->release($conns[$_], $leases[$_]) for 0 .. $#conns;
341 0 return;
342 }
343  
344  
345 2 ▶sub run_on_each_pooled_connection {
Subroutine run_on_each_pooled_connection: called CC=11 SCAR=42.0
Pod documented
346 2 my ($self, $n, $code) = @_;
347 2 ▶ croak 'run_on_each_pooled_connection($n, $code): $code must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 1 1 2
348 unless ref $code eq 'CODE';
349  
350 1 my $pool = $self->pool;
351 1 ▶ croak "pool max_size (@{[ $pool->max_size ]}) is smaller than the requested "
Branch: 1 branch
BranchTrueFalseTotal
if $pool->max_size < $n 1 0 1
352 . "$n connections -- raise pool_size in connect_attrs"
353 if $pool->max_size < $n;
354  
355 # Acquire and HOLD $n connections, forcing $n fresh spawns (idle reuse would
356 # skip the spawn -- and thus the replay). acquire's readiness gating means
357 # each handle is already connected when it arrives.
358 0 my @conns;
359 my @leases;
360 0 for (1 .. $n) {
361 0 my $conn = $self->await($pool->acquire);
362 0 push @conns, $conn;
363 0 ▶ push @leases, $pool->can('_lease') ? $pool->_lease($conn) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$pool->can('_lease') ? : 0 0 0
364 }
365  
366 # Distinctness guard: $n held connections must be $n different objects.
367 0 my %seen;
368 0 $seen{ $_ }++ for @conns;
369 0 ▶ croak "expected $n distinct pooled connections, got @{[ scalar keys %seen ]} "
Branch: 1 branch
BranchTrueFalseTotal
unless keys %seen == $n 0 0 0
370 . '-- a spawn did not happen, so the replay was not exercised per connection'
371 unless keys %seen == $n;
372  
373 0 my @results;
374 my $err;
375 {
376 0 local $@;
377 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { push @results, [$self->await(&$code($_, $self))] foreach (@conns); 1 } } or $err = $@ 0 0 0
378 # Capture each connection's resolved list as an arrayref so multi-row
379 # results do not flatten together across connections.
380 0 push @results, [ $self->await($code->($_, $self)) ] for @conns;
381 0 1;
382 } or $err = $@;
383 }
384  
385 # Always release the held connections back to the pool, even on failure.
386 0 $pool->release($conns[$_], $leases[$_]) for 0 .. $#conns;
387  
388 0 ▶ croak $err if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 0 0
389 0 return @results;
390 }
391  
392  
393 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
394 0 my $self = shift;
395 0 eval { $self->async->disconnect };
396 0 ▶ eval { $self->{schema}->storage->disconnect } if $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"schema"} 0 0 0
397 0 return;
398 }
399  
400 9 1;
401  
402 __END__
« blib/lib/DBIO/Ordered.pm ↑ Summary blib/lib/DBIO/SQLMaker/ClassicExtensions.pm »