EV/TestHarness.pm

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