EV/TestHarness.pm

filter: Statementstmt 23.9% 23 / 96 Branchbran 8.3% 2 / 24 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 35.0% 7 / 20 Podpod 100% 12 / 12 totaltotal 23.6% 44 / 186 CC 29 SCAR 37.7
CC
29
Cov
17%
run_on_each_pooled_connection
110.0
new
90.0
await
12.0
CRAP 43.5
« EV/ConnectInfo.pm ↑ Summary EV/Storage.pm »
1 package DBIO::MySQL::EV::TestHarness;
2 # ABSTRACT: Reusable live-MySQL harness for EV async on_connect-replay tests
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 # The default schema class used when the caller supplies none: an ordinary sync
12 # MySQL schema. The 'dbi:mysql:'/'dbi:MariaDB:' DSN auto-detects
13 # DBIO::MySQL::Storage, which registers the 'ev' async mode; no result sources
14 # are needed because the harness exercises connection-level SQL, not the ORM.
15 {
16 3 package DBIO::MySQL::EV::TestHarness::_Schema;
17 3 ▶ use base 'DBIO::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 __PACKAGE__->load_components('MySQL', 'MySQL::EV');
19 }
20  
21  
22 3 ▶sub live_available {
Subroutine live_available: called CC=3 SCAR=20.3
Pod documented
23 3 ▶ return 0 unless $ENV{DBIO_TEST_MYSQL_DSN};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DBIO_TEST_MYSQL_DSN"} 3 0 3
24 0 ▶ return 0 unless eval { require EV; require EV::MariaDB; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { require EV; require EV::MariaDB; 1 } } 0 0 0
25 0 return 1;
26 }
27  
28  
29 3 ▶sub skip_all_unless_live {
Subroutine skip_all_unless_live: called CC=2 SCAR=7.0
Pod documented
30 3 my $class = shift;
31 3 ▶ return if $class->live_available;
Branch: 1 branch
BranchTrueFalseTotal
if $class->live_available 0 3 3
32 3 require Test::More;
33 3 Test::More::plan(skip_all =>
34 'Set DBIO_TEST_MYSQL_DSN (and install EV::MariaDB) to run the live EV harness tests');
35 }
36  
37  
38 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
39 0 my ($class, %opts) = @_;
40  
41 0 ▶ croak 'DBIO::MySQL::EV::TestHarness->new called but no live MySQL DSN / EV::MariaDB '
Branch: 1 branch
BranchTrueFalseTotal
unless $class->live_available 0 0 0
42 . '-- gate with skip_all_unless_live'
43 unless $class->live_available;
44  
45 0 ▶ my $dsn = $opts{dsn} // $ENV{DBIO_TEST_MYSQL_DSN};
Condition0%0/3
$opts{"dsn"} // $ENV{"DBIO_TEST_MYSQL_DSN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"dsn"} // $ENV{"DBIO_TEST_MYSQL_DSN"}
$opts{"dsn"} $ENV{"DBIO_TEST_MYSQL_DSN"}
Truth table0%0/3
A // B
A= $opts{"dsn"}B= $ENV{"DBIO_TEST_MYSQL_DSN"}
ABresult
1X1
011
000
46 0 ▶ my $user = $opts{user} // $ENV{DBIO_TEST_MYSQL_USER} // '';
Condition0%0/5
($opts{"user"} // $ENV{"DBIO_TEST_MYSQL_USER"}) // ""
l!l
00
$opts{"user"} // $ENV{"DBIO_TEST_MYSQL_USER"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($opts{"user"} // $ENV{"DBIO_TEST_MYSQL_USER"}) // ""
$opts{"user"} $ENV{"DBIO_TEST_MYSQL_USER"}
Truth table0%0/3
A // B
A= $opts{"user"}B= $ENV{"DBIO_TEST_MYSQL_USER"}
ABresult
1X1
011
000
47 0 ▶ my $pass = $opts{pass} // $ENV{DBIO_TEST_MYSQL_PASS} // '';
Condition0%0/5
($opts{"pass"} // $ENV{"DBIO_TEST_MYSQL_PASS"}) // ""
l!l
00
$opts{"pass"} // $ENV{"DBIO_TEST_MYSQL_PASS"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($opts{"pass"} // $ENV{"DBIO_TEST_MYSQL_PASS"}) // ""
$opts{"pass"} $ENV{"DBIO_TEST_MYSQL_PASS"}
Truth table0%0/3
A // B
A= $opts{"pass"}B= $ENV{"DBIO_TEST_MYSQL_PASS"}
ABresult
1X1
011
000
48  
49 0 ▶ my $schema_class = $opts{schema_class} // 'DBIO::MySQL::EV::TestHarness::_Schema';
Condition0%0/2
$opts{"schema_class"} // "DBIO::MySQL::EV::TestHarness::_Schema"
l!l
00
MC/DC0%0/1
$opts{"schema_class"} // "DBIO::MySQL::EV::TestHarness::_Schema"
$opts{"schema_class"}
Truth table0%0/2
A
A= $opts{"schema_class"}
Aresult
11
00
50 0 ▶ my $connect_attrs = $opts{connect_attrs} // {};
Condition0%0/2
$opts{"connect_attrs"} // {}
l!l
00
MC/DC0%0/1
$opts{"connect_attrs"} // {}
$opts{"connect_attrs"}
Truth table0%0/2
A
A= $opts{"connect_attrs"}
Aresult
11
00
51  
52 my $self = bless {
53 dsn => $dsn,
54 user => $user,
55 pass => $pass,
56 schema_class => $schema_class,
57 timeout => $opts{timeout},
58 0 }, $class;
59  
60 # Connect an ordinary sync schema, opting the connection into the native 'ev'
61 # async mode. The connect actions live on the (owning) sync storage; the
62 # embedded EV backend's pool replays them per connection (karr #18).
63 0 $self->{schema} = $schema_class->connect(
64 $dsn, $user, $pass,
65 { %$connect_attrs, async => 'ev' },
66 );
67  
68 0 return $self;
69 }
70  
71  
72 0 ▶sub schema { $_[0]->{schema} }
Subroutine schema: not called CC=1 SCAR=6.9
Pod documented
73  
74  
75 0 ▶sub sync_storage { $_[0]->{schema}->storage }
Subroutine sync_storage: not called CC=1 SCAR=6.9
Pod documented
76  
77  
78 0 ▶sub async { $_[0]->{schema}->storage->async }
Subroutine async: not called CC=1 SCAR=6.9
Pod documented
79  
80  
81 0 ▶sub pool { $_[0]->async->pool }
Subroutine pool: not called CC=1 SCAR=6.9
Pod documented
82  
83  
84 0 ▶sub await {
Subroutine await: not called CC=3 SCAR=24.8
Pod documented
85 0 my ($self, $f) = @_;
86 0 require EV;
87 0 ▶ local $SIG{ALRM} = sub { croak 'TIMEOUT awaiting Future in EV test harness' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ▶ alarm($self->{timeout} || 30);
Condition0%0/2
$self->{'timeout'} || 30
l!l
00
MC/DC0%0/1
$self->{'timeout'} || 30
$self->{'timeout'}
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
89 0 ▶ EV::run(EV::RUN_ONCE()) until $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
$f->is_ready or do { EV::run(EV::RUN_ONCE()) } 0 0 0
90 0 alarm 0;
91 0 return $f->get;
92 }
93  
94  
95 0 ▶sub warm_pool {
Subroutine warm_pool: not called CC=3 SCAR=24.8
Pod documented
96 0 my $self = shift;
97 0 my $pool = $self->pool;
98 0 my @conns = map { $self->await($pool->acquire) } 1 .. $pool->max_size;
99 0 $self->_await_connected($_) for @conns;
100 0 $pool->release($_) for @conns;
101 0 return;
102 }
103  
104 # Drive the EV loop until $conn reports connected. is_connected is the canonical
105 # readiness gate for an EV::MariaDB handle -- the same one the pool-connect
106 # replay uses in DBIO::MySQL::EV::Storage::_run_pool_connect_statement. Guarded
107 # by the same wall-clock alarm as await so a connection that never comes up
108 # fails loudly instead of spinning forever.
109 0 ▶sub _await_connected {
Subroutine _await_connected: not called CC=3 SCAR=24.8
110 0 my ($self, $conn) = @_;
111 0 require EV;
112 0 ▶ local $SIG{ALRM} = sub { croak 'TIMEOUT awaiting EV::MariaDB connect in EV test harness' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 ▶ alarm($self->{timeout} || 30);
Condition0%0/2
$self->{'timeout'} || 30
l!l
00
MC/DC0%0/1
$self->{'timeout'} || 30
$self->{'timeout'}
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
114 0 ▶ EV::run(EV::RUN_ONCE()) until $conn->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
$conn->is_connected or do { EV::run(EV::RUN_ONCE()) } 0 0 0
115 0 alarm 0;
116 0 return $conn;
117 }
118  
119  
120 0 ▶sub query_on {
Subroutine query_on: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($self, $conn, $sql, $bind) = @_;
122 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
123 }
124  
125  
126 0 ▶sub run_on_each_pooled_connection {
Subroutine run_on_each_pooled_connection: not called CC=10 SCAR=47.0
Pod documented
127 0 my ($self, $n, $code) = @_;
128 0 ▶ croak 'run_on_each_pooled_connection($n, $code): $code must be a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 0 0
129 unless ref $code eq 'CODE';
130  
131 0 my $pool = $self->pool;
132 0 ▶ croak "pool max_size (@{[ $pool->max_size ]}) is smaller than the requested "
Branch: 1 branch
BranchTrueFalseTotal
if $pool->max_size < $n 0 0 0
133 . "$n connections -- raise pool_size in connect_attrs"
134 if $pool->max_size < $n;
135  
136 # Acquire and HOLD $n connections, forcing $n fresh spawns (idle reuse would
137 # skip the spawn -- and thus the replay). Each acquire's spawn drives the
138 # blocking connect-action replay to completion before the Future resolves.
139 # The pool hands off an EV::MariaDB handle before its async connect handshake
140 # has completed (acquire's Future is done immediately), so drive each held
141 # connection to ready before running an op on it -- a bound query's
142 # prepare/execute needs a connected handle (a bindless query would queue, but
143 # not all ops are bindless). See L</_await_connected>.
144 0 my @conns;
145 0 for (1 .. $n) {
146 0 my $conn = $self->await($pool->acquire);
147 0 $self->_await_connected($conn);
148 0 push @conns, $conn;
149 }
150  
151 # Distinctness guard: $n held connections must be $n different objects.
152 0 my %seen;
153 0 $seen{ $_ }++ for @conns;
154 0 ▶ croak "expected $n distinct pooled connections, got @{[ scalar keys %seen ]} "
Branch: 1 branch
BranchTrueFalseTotal
unless keys %seen == $n 0 0 0
155 . '-- a spawn did not happen, so the replay was not exercised per connection'
156 unless keys %seen == $n;
157  
158 0 my @results;
159 my $err;
160 {
161 0 local $@;
162 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { push @results, [$self->await(&$code($_, $self))] foreach (@conns); 1 } } or $err = $@ 0 0 0
163 # Capture each connection's resolved list as an arrayref so multi-row
164 # results do not flatten together across connections.
165 0 push @results, [ $self->await($code->($_, $self)) ] for @conns;
166 0 1;
167 } or $err = $@;
168 }
169  
170 # Always release the held connections back to the pool, even on failure.
171 0 $pool->release($_) for @conns;
172  
173 0 ▶ croak $err if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 0 0
174 0 return @results;
175 }
176  
177  
178 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
179 0 my $self = shift;
180 0 eval { $self->async->disconnect };
181 0 ▶ eval { $self->{schema}->storage->disconnect } if $self->{schema};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"schema"} 0 0 0
182 0 return;
183 }
184  
185 3 1;
186  
187 __END__
188  
189 =pod
190  
191 =encoding UTF-8
192  
193 =head1 NAME
194  
195 DBIO::MySQL::EV::TestHarness - Reusable live-MySQL harness for EV async on_connect-replay tests
196  
197 =head1 VERSION
198  
199 version 0.900001
200  
201 =head1 SYNOPSIS
202  
203 use Test::More;
204 use DBIO::MySQL::EV::TestHarness;
205  
206 # Skip the whole file cleanly unless a live MySQL DSN + EV::MariaDB are available.
207 DBIO::MySQL::EV::TestHarness->skip_all_unless_live;
208  
209 my $h = DBIO::MySQL::EV::TestHarness->new(
210 # each fresh pool connection replays these against the owning sync storage
211 connect_attrs => {
212 on_connect_do => [ q{SET @dbio_replay_tag = 'live'} ],
213 pool_size => 4,
214 },
215 );
216  
217 # Force 3 DISTINCT pool connections to spawn (each replays the connect
218 # actions at spawn) and run an async op on each -- the replay-verification seam.
219 my @tags = $h->run_on_each_pooled_connection(3, sub {
220 my ($conn, $harness) = @_;
221 return $harness->query_on($conn, q{SELECT @dbio_replay_tag});
222 });
223 is $_->[0][0], 'live', 'connect action replayed on this pooled connection'
224 for @tags;
225  
226 $h->disconnect;
227 done_testing;
228  
229 =head1 DESCRIPTION
230  
231 An B<installable> test harness for the native C<ev> async transport
232 (L<DBIO::MySQL::EV::Storage>). It removes the boilerplate every live EV test
233 repeats -- parse C<DBIO_TEST_MYSQL_DSN>, connect an C<< { async => 'ev' } >>
234 schema, reach the embedded async backend + its pool, drive the EV loop -- and,
235 crucially, provides the one thing an C<on_connect>-replay test needs and cannot
236 get from a directly-constructed storage: an EV async backend B<wired to an
237 owning sync storage> whose C<on_connect_do> / C<on_connect_call> actually replay
238 on every freshly-spawned pool connection (karr #18 / core #68).
239  
240 It ships under C<lib/> (not C<t/>) on purpose: it is consumed both by this
241 distribution's own live tests B<and> by downstream extension distributions,
242 which cannot see another dist's C<t/lib>. An extension that carries a
243 develop/recommends dependency on L<DBIO::MySQL::EV> reaches this class from its
244 own C<t/>.
245  
246 =head1 METHODS
247  
248 =head2 live_available
249  
250 DBIO::MySQL::EV::TestHarness->live_available or ...;
251  
252 Class method. True when C<DBIO_TEST_MYSQL_DSN> is set B<and> L<EV::MariaDB> (and
253 L<EV>) can be loaded -- i.e. the live harness can run.
254  
255 =head2 skip_all_unless_live
256  
257 DBIO::MySQL::EV::TestHarness->skip_all_unless_live;
258  
259 Class method. When the live harness is unavailable (see L</live_available>),
260 calls C<< Test::More->plan(skip_all => ...) >>, which skips and exits the test
261 file cleanly. A no-op when live testing is available.
262  
263 =head2 new
264  
265 my $h = DBIO::MySQL::EV::TestHarness->new(%opts);
266  
267 Connect an C<< { async => 'ev' } >> schema and build its embedded async backend.
268 Options:
269  
270 =over 4
271  
272 =item * C<dsn> / C<user> / C<pass> -- connect info; default to
273 C<DBIO_TEST_MYSQL_DSN> / C<DBIO_TEST_MYSQL_USER> / C<DBIO_TEST_MYSQL_PASS>.
274  
275 =item * C<schema_class> -- the schema class to connect (default a generic sync
276 MySQL schema). Supply your own to register storage layers / components (this is
277 how an extension injects its behaviour).
278  
279 =item * C<connect_attrs> -- extra connect attributes merged into the
280 C<< { async => 'ev', ... } >> attrs hash, e.g. C<on_connect_do>,
281 C<on_connect_call>, C<pool_size>. These are exactly the actions replayed on each
282 pooled connection.
283  
284 =item * C<timeout> -- wall-clock seconds L</await> waits before failing loudly
285 (default 30).
286  
287 =back
288  
289 Croaks unless L</live_available> (call L</skip_all_unless_live> first).
290  
291 =head2 schema
292  
293 The connected schema instance.
294  
295 =head2 sync_storage
296  
297 The sync L<DBIO::MySQL::Storage> the schema connected to (the owner of the async
298 backend). Forces driver determination on first call.
299  
300 =head2 async
301  
302 The embedded async backend (a L<DBIO::MySQL::EV::Storage>, possibly with composed
303 extension async layers). Built lazily on first access via the sync storage's
304 C<async> resolver, which also wires the owner back-reference.
305  
306 =head2 pool
307  
308 The L<DBIO::MySQL::EV::Pool> of the async backend.
309  
310 =head2 await
311  
312 my @result = $h->await($future);
313  
314 Drive the EV loop until C<$future> is ready, then return its result (dies on a
315 failed Future). Guarded by a wall-clock C<alarm> so a hang fails loudly instead
316 of spinning forever.
317  
318 =head2 warm_pool
319  
320 $h->warm_pool;
321  
322 Drive B<every> pool connection through its EV::MariaDB connect handshake to
323 ready, then release them all back to the idle pool. The pool hands off a
324 freshly-spawned connection before its async connect has completed
325 (L<DBIO::Storage::PoolBase/acquire> resolves immediately), and a bound query's
326 C<prepare>/C<execute> needs an already-connected handle. A pooled C<*_async>
327 op (e.g. C<< $h->async->select_async(...) >>, or an extension async method that
328 rides the inherited C<_query_async>) acquires a connection B<internally>, so the
329 harness cannot warm it at the call site -- call C<warm_pool> first to guarantee
330 every idle connection an C<acquire> can hand back is already connected. Mirrors
331 the manual pool pre-warm the raw live tests do by hand. Returns nothing.
332  
333 =head2 query_on
334  
335 my @rows = $h->await( $h->query_on($conn, $sql, \@bind) );
336  
337 Run C<$sql> on the specific pooled connection C<$conn> (pinned, not released),
338 returning a L<Future> of the raw result rows -- the natural way to assert that a
339 connect action replayed on B<that> connection. C<$sql> uses C<?> placeholders
340 (MySQL native). Returns a Future; wrap with L</await>.
341  
342 =head2 run_on_each_pooled_connection
343  
344 my @results = $h->run_on_each_pooled_connection($n, sub {
345 my ($conn, $harness) = @_;
346 return $harness->query_on($conn, $sql); # a Future
347 });
348  
349 The replay-verification seam. Forces C<$n> B<distinct> pool connections to spawn
350 by acquiring (and holding) them -- each spawn replays the owner's C<on_connect>
351 actions via L<DBIO::MySQL::EV::Storage/_run_pool_connect_statement>. Then runs
352 C<$code-E<gt>($conn, $self)> on each held connection, awaits the returned Future,
353 releases every connection, and returns one entry per connection (in spawn
354 order). Each entry is an B<arrayref> of whatever that connection's Future
355 resolved to -- e.g. for a L</query_on> op it is the arrayref of raw result rows,
356 so C<< $results[$i][0] >> is connection C<$i>'s first row. Capturing per
357 connection as an arrayref keeps multi-row results from flattening across
358 connections.
359  
360 C<$code> must return a L<Future>. Croaks if the pool's C<max_size> is smaller
361 than C<$n> (raise C<pool_size> in C<connect_attrs>), or if the pool ever hands
362 back a non-distinct connection (which would mean a spawn did not happen and the
363 replay was not exercised on a fresh connection).
364  
365 =head2 disconnect
366  
367 Tear down the async backend (shutting the pool, which runs the C<on_disconnect>
368 actions on each live connection) and the schema storage.
369  
370 =head1 REUSE BY AN EXTENSION
371  
372 An async storage extension composes onto this transport via core's storage-layer
373 composition (core karr #70); it does not ship its own transport. To live-test
374 that its C<on_connect_call> replays on pooled EV connections, an extension reuses
375 this harness by supplying its own schema class (carrying the extension's storage
376 layer + component) and its own per-connection assertion:
377  
378 my $h = DBIO::MySQL::EV::TestHarness->new(
379 schema_class => 'MyApp::Ext::Schema',
380 connect_attrs => { on_connect_call => 'load_ext', pool_size => 3 },
381 );
382  
383 $h->run_on_each_pooled_connection(3, sub {
384 my ($conn, $harness) = @_;
385 return $harness->query_on($conn, q{SELECT @@session.some_var});
386 });
387  
388 The harness stays generic: it never mentions any extension, exposes the schema /
389 sync storage / async backend / pool, and lets the caller add the layer (via
390 C<schema_class>) and assert whatever it needs on each pooled connection.
391  
392 =head1 CAVEAT
393  
394 This is a B<live> harness: it opens real connections against
395 C<DBIO_TEST_MYSQL_DSN>. It is for driver/extension test suites, not production
396 code. It is excluded from the mock-only offline discipline by design -- gate
397 every consumer with L</skip_all_unless_live>.
398  
399 =head1 AUTHOR
400  
401 DBIO Authors
402  
403 =head1 COPYRIGHT AND LICENSE
404  
405 Copyright (C) 2026 DBIO Authors
406  
407 This is free software; you can redistribute it and/or modify it under
408 the same terms as the Perl 5 programming language system itself.
409  
410 =cut
« EV/ConnectInfo.pm ↑ Summary EV/Storage.pm »