lib/PAGI/Test/SSE.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 61.5% 16 / 26 Conditioncond 72.7% 8 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 85.6% 113 / 132 CC 19 SCAR 12.1
CC
19
Cov
85%
receive_event
5.0
_pump_app
5.0
__ANON__
5.0
CRAP 3.4
« blib/lib/PAGI/Utils/SecureCompare.pm ↑ Summary lib/PAGI/App/Cascade.pm »
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
BranchTrueFalseTotal
unless $args{"app"} 0 12 12
14 12 ▶ croak "scope is required" unless $args{scope};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"scope"} 0 12 12
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
BranchTrueFalseTotal
if ($self->{"closed"}) 0 2 2
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"} //= []
l!l
02
MC/DC0%0/1
$self->{"_pending_receives"} //= []
$self->{"_pending_receives"}
Truth table50%1/2
A
A= $self->{"_pending_receives"}
Aresult
11
00
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
BranchTrueFalseTotal
if ($event->{'type'} eq 'sse.start') { } 12 19 31
elsif ($event->{'type'} eq 'sse.send') { } 18 1 19
47 12 $self->{started} = 1;
48 12 ▶ $self->{status} = $event->{status} // 200;
Condition50%1/2
$event->{"status"} // 200
l!l
120
MC/DC0%0/1
$event->{"status"} // 200
$event->{"status"}
Truth table50%1/2
A
A= $event->{"status"}
Aresult
11
00
49 12 ▶ $self->{headers} = $event->{headers} // [];
Condition100%2/2
$event->{"headers"} // []
l!l
66
MC/DC100%1/1
$event->{"headers"} // []
$event->{"headers"}
Truth table100%2/2
A
A= $event->{"headers"}
Aresult
11
00
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
BranchTrueFalseTotal
unless $self->{"started"} 0 12 12
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
BranchTrueFalseTotal
if ($self->{"closed"} and $self->{"_pending_receives"}) 2 22 24
Condition100%3/3
$self->{"closed"} and $self->{"_pending_receives"}
!ll&&!rl&&r
12102
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"}
ABresult
0X0
100
111
75 2 ▶ while (my $future = shift @{$self->{_pending_receives}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $future = shift @{$$self{"_pending_receives"};}) 2 2 4
76 2 ▶ $future->done({ type => 'sse.disconnect' }) unless $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $future->is_ready 2 0 2
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
l!l
014
MC/DC0%0/1
$opts{"timeout"} // 5
$opts{"timeout"}
Truth table50%1/2
A
A= $opts{"timeout"}
Aresult
11
00
84  
85 # Check if we have an event already waiting
86 14 ▶ if (@{$self->{recv_queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"recv_queue"};}) 14 0 14
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
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
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
BranchTrueFalseTotal
unless defined $event 0 1 1
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
BranchTrueFalseTotal
if $self->{"closed"} 0 12 12
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__
« blib/lib/PAGI/Utils/SecureCompare.pm ↑ Summary lib/PAGI/App/Cascade.pm »