| 1 | package PAGI::Test::ConnectionState; | ||||||||||||
| 2 | 15 | $PAGI::Test::ConnectionState::VERSION = '0.002002'; | |||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | =head1 NAME | ||||||||||||
| 7 | |||||||||||||
| 8 | PAGI::Test::ConnectionState - the pagi.connection object provided by PAGI::Test | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 DESCRIPTION | ||||||||||||
| 11 | |||||||||||||
| 12 | PAGI::Test is a test server, so it provides the per-request C<pagi.connection> | ||||||||||||
| 13 | object. It implements the surface L<PAGI::Request>/L<PAGI::Context> delegate to | ||||||||||||
| 14 | (C<is_connected>, C<disconnect_reason>, C<disconnect_future>, C<on_disconnect>, | ||||||||||||
| 15 | C<on_complete>) plus C<response_started>, mirroring production | ||||||||||||
| 16 | C<PAGI::Server::ConnectionState>: a clean completion ends the request and fires | ||||||||||||
| 17 | C<on_complete> but is not a disconnect; exactly one of C<on_complete> / | ||||||||||||
| 18 | C<on_disconnect> fires. | ||||||||||||
| 19 | |||||||||||||
| 20 | =cut | ||||||||||||
| 21 | |||||||||||||
| 22 | 79 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 23 | 79 | my ($class) = @_; | |||||||||||
| 24 | 79 | return bless { | |||||||||||
| 25 | _connected => 1, | ||||||||||||
| 26 | _response_started => 0, | ||||||||||||
| 27 | _completed => 0, # explicit terminal-state flag, like production | ||||||||||||
| 28 | _reason => undef, | ||||||||||||
| 29 | _disc_cbs => [], | ||||||||||||
| 30 | _comp_cbs => [], | ||||||||||||
| 31 | }, $class; | ||||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | 4 | ▶ | sub is_connected { return $_[0]->{_connected} ? 1 : 0 } | ||||||||||
|
Subroutine
is_connected: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||
| 35 | 20 | ▶ | sub response_started { return $_[0]->{_response_started} ? 1 : 0 } | ||||||||||
|
Subroutine
response_started: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||
| 36 | 3 | ▶ | sub disconnect_reason { return $_[0]->{_reason} } | ||||||||||
|
Subroutine
disconnect_reason: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 37 | 0 | ▶ | sub disconnect_future { return undef } # not supported by the test double (spec: undef = unsupported) | ||||||||||
|
Subroutine
disconnect_future: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 38 | |||||||||||||
| 39 | # Late registration fires immediately for the terminal state that occurred — | ||||||||||||
| 40 | # distinguished by _completed (clean) vs a set _reason (abnormal), like production. | ||||||||||||
| 41 | # Invoke a callback the way production does: isolate failures so one bad | ||||||||||||
| 42 | # callback does not prevent the others from running. | ||||||||||||
| 43 | 6 | ▶ | sub _fire { | ||||||||||
|
Subroutine
_fire: called CC=2 SCAR=7.0
| |||||||||||||
| 44 | 6 | my ($cb, @args) = @_; | |||||||||||
| 45 | 6 | ▶ | eval { $cb->(@args); 1 } or warn "pagi.connection callback error: $@"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 6 | return; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 5 | ▶ | sub on_disconnect { | ||||||||||
|
Subroutine
on_disconnect: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 50 | 5 | my ($self, $cb) = @_; | |||||||||||
| 51 | 5 | ▶ | if (!$self->{_connected}) { # terminal: never store, fire only if abnormal | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 1 | ▶ | _fire($cb, $self->{_reason}) unless $self->{_completed}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | 1 | return; | |||||||||||
| 54 | } | ||||||||||||
| 55 | 4 | push @{$self->{_disc_cbs}}, $cb; # still in flight: register | |||||||||||
| 56 | 4 | return; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 7 | ▶ | sub on_complete { | ||||||||||
|
Subroutine
on_complete: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 60 | 7 | my ($self, $cb) = @_; | |||||||||||
| 61 | 7 | ▶ | if (!$self->{_connected}) { # terminal: never store, fire only if clean | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 2 | ▶ | _fire($cb) if $self->{_completed}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 2 | return; | |||||||||||
| 64 | } | ||||||||||||
| 65 | 5 | push @{$self->{_comp_cbs}}, $cb; | |||||||||||
| 66 | 5 | return; | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | # Server-internal (the test client, acting as server, calls these). | ||||||||||||
| 70 | 78 | ▶ | sub _mark_response_started { $_[0]->{_response_started} = 1; return } | ||||||||||
|
Subroutine
_mark_response_started: called CC=1 SCAR=0.0
| |||||||||||||
| 71 | |||||||||||||
| 72 | 60 | ▶ | sub _mark_complete { | ||||||||||
|
Subroutine
_mark_complete: called CC=3 SCAR=11.0
| |||||||||||||
| 73 | 60 | my ($self) = @_; | |||||||||||
| 74 | 60 | ▶ | return unless $self->{_connected}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 60 | $self->{_connected} = 0; | |||||||||||
| 76 | 60 | $self->{_completed} = 1; # clean completion (distinguishes from disconnect) | |||||||||||
| 77 | 60 | _fire($_) for @{$self->{_comp_cbs}}; | |||||||||||
| 78 | 60 | @{$self->{_comp_cbs}} = (); | |||||||||||
| 79 | 60 | @{$self->{_disc_cbs}} = (); | |||||||||||
| 80 | 60 | return; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 13 | ▶ | sub _mark_disconnected { | ||||||||||
|
Subroutine
_mark_disconnected: called CC=4 SCAR=14.0
| |||||||||||||
| 84 | 13 | my ($self, $reason) = @_; | |||||||||||
| 85 | 13 | ▶ | return unless $self->{_connected}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 13 | $self->{_connected} = 0; | |||||||||||
| 87 | 13 | ▶ | $self->{_reason} = $reason // 'unknown'; # coerce like production | ||||||||||
|
Condition50%1/2
$reason // "unknown"
MC/DC0%0/1
$reason // "unknown"
$reason
Truth table50%1/2
A
A= $reason
| |||||||||||||
| 88 | 13 | _fire($_, $self->{_reason}) for @{$self->{_disc_cbs}}; | |||||||||||
| 89 | 13 | @{$self->{_disc_cbs}} = (); | |||||||||||
| 90 | 13 | @{$self->{_comp_cbs}} = (); | |||||||||||
| 91 | 13 | return; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | 15 | 1; | |||||||||||