| 1 | package PAGI::Test::SSE; | ||||||||||||||||||||||||||||
| 2 | 5 | $PAGI::Test::SSE::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 12 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 12 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 12 | ▶ | croak "app is required" unless $args{app}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 12 | ▶ | croak "scope is required" unless $args{scope}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | return bless { | ||||||||||||||||||||||||||||
| 17 | app => $args{app}, | ||||||||||||||||||||||||||||
| 18 | scope => $args{scope}, | ||||||||||||||||||||||||||||
| 19 | 12 | recv_queue => [], # Events from app -> test | |||||||||||||||||||||||||||
| 20 | closed => 0, | ||||||||||||||||||||||||||||
| 21 | started => 0, | ||||||||||||||||||||||||||||
| 22 | }, $class; | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 12 | ▶ | sub _start { | ||||||||||||||||||||||||||
|
Subroutine
_start: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 26 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Create receive coderef for the app (always returns disconnect when closed) | ||||||||||||||||||||||||||||
| 29 | 2 | ▶ | my $receive = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | if ($self->{closed}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | return { type => 'sse.disconnect' }; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # SSE only receives disconnects from client, so we wait indefinitely | ||||||||||||||||||||||||||||
| 35 | # until the connection is closed | ||||||||||||||||||||||||||||
| 36 | 2 | my $future = Future->new; | |||||||||||||||||||||||||||
| 37 | # This future will be resolved when close() is called | ||||||||||||||||||||||||||||
| 38 | 2 | ▶ | push @{$self->{_pending_receives} //= []}, $future; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"_pending_receives"} //= []
MC/DC0%0/1
$self->{"_pending_receives"} //= []
$self->{"_pending_receives"}
Truth table50%1/2
A
A= $self->{"_pending_receives"}
| |||||||||||||||||||||||||||||
| 39 | 2 | return await $future; | |||||||||||||||||||||||||||
| 40 | 12 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Create send coderef for the app | ||||||||||||||||||||||||||||
| 43 | 31 | ▶ | my $send = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 44 | 31 | my ($event) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 31 | ▶ | if ($event->{type} eq 'sse.start') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 47 | 12 | $self->{started} = 1; | |||||||||||||||||||||||||||
| 48 | 12 | ▶ | $self->{status} = $event->{status} // 200; | ||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"status"} // 200
MC/DC0%0/1
$event->{"status"} // 200
$event->{"status"}
Truth table50%1/2
A
A= $event->{"status"}
| |||||||||||||||||||||||||||||
| 49 | 12 | ▶ | $self->{headers} = $event->{headers} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$event->{"headers"} // []
MC/DC100%1/1
$event->{"headers"} // []
$event->{"headers"}
Truth table100%2/2
A
A= $event->{"headers"}
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | elsif ($event->{type} eq 'sse.send') { | ||||||||||||||||||||||||||||
| 52 | 18 | push @{$self->{recv_queue}}, $event; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 31 | return; | |||||||||||||||||||||||||||
| 56 | 12 | }; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Start the app future but don't block on it | ||||||||||||||||||||||||||||
| 59 | 12 | $self->{app_future} = $self->{app}->($self->{scope}, $receive, $send); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Wait for sse.start (this should complete immediately) | ||||||||||||||||||||||||||||
| 62 | # We need to let the app run until it starts | ||||||||||||||||||||||||||||
| 63 | 12 | $self->_pump_app; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | ▶ | croak "SSE connection not started" unless $self->{started}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 12 | return $self; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 24 | ▶ | sub _pump_app { | ||||||||||||||||||||||||||
|
Subroutine
_pump_app: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 71 | 24 | my ($self) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # If closed and there are pending receives, resolve them with disconnect | ||||||||||||||||||||||||||||
| 74 | 24 | ▶ | if ($self->{closed} && $self->{_pending_receives}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"closed"} and $self->{"_pending_receives"}
MC/DC100%2/2
$self->{"closed"} and $self->{"_pending_receives"}
$self->{"closed"}
$self->{"_pending_receives"}
Truth table100%3/3
A and B
A= $self->{"closed"}B= $self->{"_pending_receives"}
| |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | while (my $future = shift @{$self->{_pending_receives}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | $future->done({ type => 'sse.disconnect' }) unless $future->is_ready; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 14 | ▶ | sub receive_event { | ||||||||||||||||||||||||||
|
Subroutine
receive_event: called CC=4 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 14 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||
| 83 | 14 | ▶ | my $timeout = $opts{timeout} // 5; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{"timeout"} // 5
MC/DC0%0/1
$opts{"timeout"} // 5
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Check if we have an event already waiting | ||||||||||||||||||||||||||||
| 86 | 14 | ▶ | if (@{$self->{recv_queue}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 14 | my $event = shift @{$self->{recv_queue}}; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Extract SSE event fields | ||||||||||||||||||||||||||||
| 90 | return { | ||||||||||||||||||||||||||||
| 91 | event => $event->{event}, | ||||||||||||||||||||||||||||
| 92 | data => $event->{data}, | ||||||||||||||||||||||||||||
| 93 | id => $event->{id}, | ||||||||||||||||||||||||||||
| 94 | retry => $event->{retry}, | ||||||||||||||||||||||||||||
| 95 | 14 | }; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Check if connection closed | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return undef if $self->{closed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # No event available yet | ||||||||||||||||||||||||||||
| 102 | 0 | croak "Timeout waiting for SSE event"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub receive_json { | ||||||||||||||||||||||||||
|
Subroutine
receive_json: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 106 | 1 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | my $event = $self->receive_event(%opts); | |||||||||||||||||||||||||||
| 109 | 1 | ▶ | return undef unless defined $event; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | require JSON::MaybeXS; | |||||||||||||||||||||||||||
| 112 | 1 | return JSON::MaybeXS::decode_json($event->{data}); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 12 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 12 | ▶ | return if $self->{closed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 12 | $self->{closed} = 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Pump the app to let it process the disconnect | ||||||||||||||||||||||||||||
| 123 | 12 | $self->_pump_app; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 12 | return $self; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 11 | ▶ | sub is_closed { | ||||||||||||||||||||||||||
|
Subroutine
is_closed: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||
| 130 | 11 | return $self->{closed}; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||