| 1 | package DBIO::MySQL::EV::TestHarness; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Reusable live-MySQL harness for EV async on_connect-replay tests | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 26 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 26 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 26 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 26 | ▶ | use base 'DBIO::Test::AsyncHarness'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 26 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 26 | __PACKAGE__->harness_env_prefix('DBIO_TEST_MYSQL'); | |||||||||||||||||||||||||||
| 15 | 26 | __PACKAGE__->harness_async_mode('ev'); | |||||||||||||||||||||||||||
| 16 | 26 | __PACKAGE__->harness_required_modules([qw( EV EV::MariaDB )]); | |||||||||||||||||||||||||||
| 17 | 26 | __PACKAGE__->harness_schema_class('DBIO::MySQL::EV::TestHarness::_Schema'); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # The default schema class used when the caller supplies none: an ordinary sync | ||||||||||||||||||||||||||||
| 20 | # MySQL schema. Its MySQL component declares DBIO::MySQL::Storage at connect, | ||||||||||||||||||||||||||||
| 21 | # and that storage registers the 'ev' async mode; no result sources are needed | ||||||||||||||||||||||||||||
| 22 | # because the harness exercises connection-level SQL, not the ORM. | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | # 'MySQL::EV' is loaded ALONGSIDE the driver component deliberately, and is not | ||||||||||||||||||||||||||||
| 25 | # what makes the connection async -- { async => 'ev' } at connect is (ADR 0030). | ||||||||||||||||||||||||||||
| 26 | # DBIO::MySQL::EV is an inert marker (ADR 0032 section 7, karr k14): it defines | ||||||||||||||||||||||||||||
| 27 | # no methods and no connection() hook, so loading it must change nothing. Doing | ||||||||||||||||||||||||||||
| 28 | # it here is the one place that claim is exercised against a REAL server -- a | ||||||||||||||||||||||||||||
| 29 | # marker that ever regained a storage_type pin would break these live tests, not | ||||||||||||||||||||||||||||
| 30 | # just the offline guard in t/00-load.t. Core's DBIO::Test::AsyncHarness has no | ||||||||||||||||||||||||||||
| 31 | # default schema of its own precisely because this choice is per driver. | ||||||||||||||||||||||||||||
| 32 | { | ||||||||||||||||||||||||||||
| 33 | 26 | package DBIO::MySQL::EV::TestHarness::_Schema; | |||||||||||||||||||||||||||
| 34 | 26 | ▶ | use base 'DBIO::Schema'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 26 | __PACKAGE__->load_components('MySQL', 'MySQL::EV'); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 10 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 10 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 41 | my $schema_class = $opts{schema_class} | ||||||||||||||||||||||||||||
| 42 | 10 | ▶ | // $class->_harness_declaration('harness_schema_class'); | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts{"schema_class"} // $class->_harness_declaration("harness_schema_class")
MC/DC0%0/2
$opts{"schema_class"} // $class->_harness_declaration("harness_schema_class")
$opts{"schema_class"}
$class->_harness_declaration("harness_schema_class")
Truth table66%2/3
A // B
A= $opts{"schema_class"}B= $class->_harness_declaration("harness_schema_class")
| |||||||||||||||||||||||||||||
| 43 | 10 | ▶ | $opts{storage_type} //= 'DBIO::MySQL::Storage' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$opts{"storage_type"} //= "DBIO::MySQL::Storage"
MC/DC100%1/1
$opts{"storage_type"} //= "DBIO::MySQL::Storage"
$opts{"storage_type"}
Truth table100%2/2
A
A= $opts{"storage_type"}
| |||||||||||||||||||||||||||||
| 44 | if _declares_no_driver($schema_class); | ||||||||||||||||||||||||||||
| 45 | 10 | return $class->SUPER::new(%opts); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Core's reading of storage_type (DBIO::Schema::connection): a schema class | ||||||||||||||||||||||||||||
| 49 | # whose storage class resolves to the generic DBIO::Storage::DBI declares no | ||||||||||||||||||||||||||||
| 50 | # driver (ADR 0067). Inlined because a core before ADR 0067 has no helper for | ||||||||||||||||||||||||||||
| 51 | # it. A class that is no schema is left to core to refuse. | ||||||||||||||||||||||||||||
| 52 | 10 | ▶ | sub _declares_no_driver { | ||||||||||||||||||||||||||
|
Subroutine
_declares_no_driver: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 53 | 10 | my ($schema_class) = @_; | |||||||||||||||||||||||||||
| 54 | 10 | ▶ | return 0 unless defined $schema_class && $schema_class->can('storage_type'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $schema_class and $schema_class->can("storage_type")
MC/DC0%0/2
defined $schema_class and $schema_class->can("storage_type")
defined $schema_class
$schema_class->can("storage_type")
Truth table33%1/3
A and B
A= defined $schema_classB= $schema_class->can("storage_type")
| |||||||||||||||||||||||||||||
| 55 | 10 | my $storage_type = $schema_class->storage_type; | |||||||||||||||||||||||||||
| 56 | 10 | ▶ | my ($storage_class) = ref $storage_type | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | ? $schema_class->_normalize_storage_type($storage_type) | ||||||||||||||||||||||||||||
| 58 | : ($storage_type); | ||||||||||||||||||||||||||||
| 59 | 10 | $storage_class =~ s/^\+//; | |||||||||||||||||||||||||||
| 60 | 10 | $storage_class =~ s/^::/DBIO::Storage::/; | |||||||||||||||||||||||||||
| 61 | 10 | return $storage_class eq 'DBIO::Storage::DBI'; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub _tick_loop { | ||||||||||||||||||||||||||
|
Subroutine
_tick_loop: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 0 | require EV; | |||||||||||||||||||||||||||
| 67 | 0 | EV::run(EV::RUN_ONCE()); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 26 | my %SESSION_SUM; | |||||||||||||||||||||||||||
| 71 | my $COUNTING; | ||||||||||||||||||||||||||||
| 72 | 26 | my @SESSION_COUNTERS = qw( Com_begin Com_commit Com_rollback Com_stmt_prepare Com_stmt_close ); | |||||||||||||||||||||||||||
| 73 | my $SESSION_SQL = 'SHOW SESSION STATUS WHERE Variable_name IN (' | ||||||||||||||||||||||||||||
| 74 | 26 | . join(', ', map { "'$_'" } @SESSION_COUNTERS) . ')'; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub count_session_status { | ||||||||||||||||||||||||||
|
Subroutine
count_session_status: not called CC=2 SCAR=16.8
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return if $COUNTING++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | require DBIO::MySQL::EV::Storage; | |||||||||||||||||||||||||||
| 79 | 26 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 0 | my $orig = \&DBIO::MySQL::EV::Storage::_reset_pool_connection; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | *DBIO::MySQL::EV::Storage::_reset_pool_connection = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 82 | 0 | my ($self, $conn) = @_; | |||||||||||||||||||||||||||
| 83 | 0 | my $tid = eval { $conn->thread_id }; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # EV::MariaDB takes no reset behind a query queued on the wire, so the | ||||||||||||||||||||||||||||
| 86 | # reading is answered first and the reset follows from an idle watcher | ||||||||||||||||||||||||||||
| 87 | # (outside the callback, like every settling of the transport). A handle | ||||||||||||||||||||||||||||
| 88 | # with a statement in flight is reset as it is. | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return $self->$orig($conn) unless defined $tid && !eval { $conn->pending_count }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $tid and not eval {
do {
$conn->pending_count
}
}
MC/DC0%0/2
defined $tid and not eval {
do {
$conn->pending_count
}
}
defined $tid
not eval {
do {
$conn->pending_count
}
}
Truth table0%0/3
A and not(B)
A= defined $tidB= not eval {
do {
$conn->pending_count
}
}
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $outer = $self->future_class->new; | |||||||||||||||||||||||||||
| 92 | 0 | my ($ok, $idle, $held); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $reset = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $idle = EV::idle(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 95 | 0 | undef $idle; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | my $f = $held = eval { $self->$orig($conn) } || $self->future_class->fail($@); | ||||||||||||||||||||||||||
|
Condition0%0/3
eval {
do {
$self->$orig($conn)
}
} || $self->future_class->fail($@)
MC/DC0%0/2
eval {
do {
$self->$orig($conn)
}
} || $self->future_class->fail($@)
eval {
do {
$self->$orig($conn)
}
}
$self->future_class->fail($@)
Truth table0%0/3
A || B
A= eval {
do {
$self->$orig($conn)
}
}B= $self->future_class->fail($@)
| |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | $f->on_ready(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 98 | 0 | my ($inner) = @_; | |||||||||||||||||||||||||||
| 99 | 0 | undef $held; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | $inner->is_cancelled ? $outer->cancel | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 101 | : $inner->is_failed ? $outer->fail($inner->failure) | ||||||||||||||||||||||||||||
| 102 | : $outer->done($inner->get); | ||||||||||||||||||||||||||||
| 103 | 0 | }); | |||||||||||||||||||||||||||
| 104 | 0 | }); | |||||||||||||||||||||||||||
| 105 | 0 | }; | |||||||||||||||||||||||||||
| 106 | 0 | $ok = eval { | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | $conn->query($SESSION_SQL, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 108 | 0 | my ($rows, $err) = @_; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | $SESSION_SUM{$tid}{ $_->[0] } += $_->[1] for $err ? () : @{ $rows || [] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$rows || []
MC/DC0%0/1
$rows || []
$rows
Truth table0%0/2
A
A= $rows
| |||||||||||||||||||||||||||||
| 110 | 0 | $reset->(); | |||||||||||||||||||||||||||
| 111 | 0 | }); | |||||||||||||||||||||||||||
| 112 | 0 | 1; | |||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $reset->() unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | return $outer; | |||||||||||||||||||||||||||
| 116 | 0 | }; | |||||||||||||||||||||||||||
| 117 | 0 | return; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub session_status { | ||||||||||||||||||||||||||
|
Subroutine
session_status: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my ($self, $conn) = @_; | |||||||||||||||||||||||||||
| 122 | 0 | require Time::HiRes; | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | my $tick = EV::timer(0.02, 0.02, sub {}); # RUN_ONCE never blocks for long | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | 0 | my $deadline = Time::HiRes::time() + 10; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $busy = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return $self->pool->_connection_is_resetting($conn) || eval { $conn->pending_count }; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->pool->_connection_is_resetting($conn) || eval {
do {
$conn->pending_count
}
}
MC/DC0%0/2
$self->pool->_connection_is_resetting($conn) || eval {
do {
$conn->pending_count
}
}
$self->pool->_connection_is_resetting($conn)
eval {
do {
$conn->pending_count
}
}
Truth table0%0/3
A || B
A= $self->pool->_connection_is_resetting($conn)B= eval {
do {
$conn->pending_count
}
}
| |||||||||||||||||||||||||||||
| 127 | 0 | }; | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | while ($busy->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return if Time::HiRes::time() > $deadline; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | $self->_tick_loop; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $tid = eval { $conn->thread_id } // return; | ||||||||||||||||||||||||||
|
Condition0%0/2
eval {
do {
$conn->thread_id
}
} // (return)
MC/DC0%0/1
eval {
do {
$conn->thread_id
}
} // (return)
eval {
do {
$conn->thread_id
}
}
Truth table0%0/2
A
A= eval {
do {
$conn->thread_id
}
}
| |||||||||||||||||||||||||||||
| 134 | 0 | my ($rows, $err, $done); | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | eval { $conn->query($SESSION_SQL, sub { ($rows, $err) = @_; $done = 1 }); 1 } or return; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | until ($done) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | return if Time::HiRes::time() > $deadline; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | $self->_tick_loop; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | return if $err || !$rows; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$err or not $rows
MC/DC0%0/2
$err or not $rows
$err
not $rows
Truth table0%0/3
A or not(B)
A= $errB= not $rows
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my %status = map { ($_ => $SESSION_SUM{$tid}{$_} // 0) } @SESSION_COUNTERS; | ||||||||||||||||||||||||||
|
Condition0%0/2
$SESSION_SUM{$tid}{$_} // 0
MC/DC0%0/1
$SESSION_SUM{$tid}{$_} // 0
$SESSION_SUM{$tid}{$_}
Truth table0%0/2
A
A= $SESSION_SUM{$tid}{$_}
| |||||||||||||||||||||||||||||
| 143 | 0 | $status{ $_->[0] } += $_->[1] for @$rows; | |||||||||||||||||||||||||||
| 144 | 0 | return \%status; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 26 | 1; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | __END__ | ||||||||||||||||||||||||||||