lib/PAGI/Test/WebSocket.pm

filter: Statementstmt 93.6% 118 / 126 Branchbran 60.0% 24 / 40 Conditioncond 57.1% 16 / 28 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 20 / 20 Podpod 100% 11 / 11 totaltotal 80.5% 194 / 241 CC 35 SCAR 15.9
CC
35
Cov
81%
_pump_app
8.6
receive_bytes
8.3
receive_text
6.3
CRAP 4.9
« blib/lib/PAGI/Middleware/Cookie.pm ↑ Summary lib/PAGI/Middleware/SSE/Retry.pm »
1 package PAGI::Test::WebSocket;
2 5 $PAGI::Test::WebSocket::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 14 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
11 14 my ($class, %args) = @_;
12  
13 14 ▶ croak "app is required" unless $args{app};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"app"} 0 14 14
14 14 ▶ croak "scope is required" unless $args{scope};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"scope"} 0 14 14
15  
16 return bless {
17 app => $args{app},
18 scope => $args{scope},
19 14 send_queue => [], # Messages from test -> app
20 recv_queue => [], # Messages from app -> test
21 closed => 0,
22 accepted => 0,
23 close_code => undef,
24 close_reason => '',
25 _pending_receives => [], # Pending receive futures
26 }, $class;
27 }
28  
29 14 ▶sub _start {
Subroutine _start: called CC=2 SCAR=7.1
30 14 my ($self) = @_;
31  
32 # Create receive coderef for the app
33 31 ▶ my $receive = async sub {
Subroutine __ANON__: called CC=5 SCAR=17.6
34 # First call returns websocket.connect
35 31 ▶ if (!$self->{_connect_sent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_connect_sent"}) 12 19 31
36 12 $self->{_connect_sent} = 1;
37 12 return { type => 'websocket.connect' };
38 }
39  
40 # Return queued message if available
41 19 ▶ if (@{$self->{send_queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"send_queue"};}) 0 19 19
42 0 return shift @{$self->{send_queue}};
43 }
44  
45 # Return disconnect if closed
46 19 ▶ if ($self->{closed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"}) 0 19 19
47 0 ▶ return { type => 'websocket.disconnect', code => $self->{close_code} // 1000 };
Condition0%0/2
$self->{"close_code"} // 1000
l!l
00
MC/DC0%0/1
$self->{"close_code"} // 1000
$self->{"close_code"}
Truth table0%0/2
A
A= $self->{"close_code"}
Aresult
11
00
48 }
49  
50 # Create a future that will be resolved when data arrives
51 19 my $future = Future->new;
52 19 push @{$self->{_pending_receives}}, $future;
53 19 return await $future;
54 14 };
55  
56 # Create send coderef for the app
57 28 ▶ my $send = async sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
58 28 my ($event) = @_;
59  
60 28 ▶ if ($event->{type} eq 'websocket.accept') {
Branch: 3 branches
BranchTrueFalseTotal
if ($event->{'type'} eq 'websocket.accept') { } 14 14 28
elsif ($event->{'type'} eq 'websocket.send') { } 11 3 14
elsif ($event->{'type'} eq 'websocket.close') { } 2 1 3
61 14 $self->{accepted} = 1;
62 }
63 elsif ($event->{type} eq 'websocket.send') {
64 11 push @{$self->{recv_queue}}, $event;
65 }
66 elsif ($event->{type} eq 'websocket.close') {
67 2 $self->{closed} = 1;
68 2 ▶ $self->{close_code} = $event->{code} // 1000;
Condition50%1/2
$event->{"code"} // 1000
l!l
20
MC/DC0%0/1
$event->{"code"} // 1000
$event->{"code"}
Truth table50%1/2
A
A= $event->{"code"}
Aresult
11
00
69 2 ▶ $self->{close_reason} = $event->{reason} // '';
Condition50%1/2
$event->{"reason"} // ""
l!l
02
MC/DC0%0/1
$event->{"reason"} // ""
$event->{"reason"}
Truth table50%1/2
A
A= $event->{"reason"}
Aresult
11
00
70 }
71  
72 28 return;
73 14 };
74  
75 # Start the app future but don't block on it
76 14 $self->{app_future} = $self->{app}->($self->{scope}, $receive, $send);
77  
78 # Wait for acceptance (the first two awaits in the app should complete immediately)
79 # This is a bit hacky but works: we need to let the app run until it accepts
80 14 $self->_pump_app;
81  
82 14 ▶ croak "WebSocket connection not accepted" unless $self->{accepted};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"accepted"} 0 14 14
83  
84 14 return $self;
85 }
86  
87 34 ▶sub _pump_app {
Subroutine _pump_app: called CC=7 SCAR=21.5
88 34 my ($self) = @_;
89  
90 # This pumps the app future by checking if it's waiting on a receive
91 # If there are pending receives and we have data, resolve them
92 34 ▶ while (@{$self->{_pending_receives}} && @{$self->{send_queue}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"_pending_receives"};} and @{$self->{"send_queue"};}) 19 34 53
Condition100%3/3
@{$self->{"_pending_receives"};} and @{$self->{"send_queue"};}
!ll&&!rl&&r
151919
MC/DC100%2/2
@{$self->{"_pending_receives"};} and @{$self->{"send_queue"};}
@{$self->{"_pending_receives"};} @{$self->{"send_queue"};}
Truth table100%3/3
A and B
A= @{$self->{"_pending_receives"};}B= @{$self->{"send_queue"};}
ABresult
0X0
100
111
93 19 my $future = shift @{$self->{_pending_receives}};
94 19 my $event = shift @{$self->{send_queue}};
95 19 $future->done($event);
96 }
97  
98 # If closed and there are pending receives, resolve them with disconnect
99 34 ▶ if ($self->{closed} && @{$self->{_pending_receives}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"} and @{$self->{"_pending_receives"};}) 0 34 34
Condition66%2/3
$self->{"closed"} and @{$self->{"_pending_receives"};}
!ll&&!rl&&r
20140
MC/DC0%0/2
$self->{"closed"} and @{$self->{"_pending_receives"};}
$self->{"closed"} @{$self->{"_pending_receives"};}
Truth table66%2/3
A and B
A= $self->{"closed"}B= @{$self->{"_pending_receives"};}
ABresult
0X0
100
111
100 0 ▶ while (my $future = shift @{$self->{_pending_receives}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $future = shift @{$$self{"_pending_receives"};}) 0 0 0
101 0 ▶ $future->done({ type => 'websocket.disconnect', code => $self->{close_code} // 1000 });
Condition0%0/2
$self->{"close_code"} // 1000
l!l
00
MC/DC0%0/1
$self->{"close_code"} // 1000
$self->{"close_code"}
Truth table0%0/2
A
A= $self->{"close_code"}
Aresult
11
00
102 }
103 }
104 }
105  
106 8 ▶sub send_text {
Subroutine send_text: called CC=2 SCAR=6.9
Pod documented
107 8 my ($self, $text) = @_;
108  
109 8 ▶ croak "Cannot send on closed WebSocket" if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 1 7 8
110  
111 7 push @{$self->{send_queue}}, {
112 type => 'websocket.receive',
113 text => $text,
114 };
115  
116 # Pump the app to process this message
117 7 $self->_pump_app;
118  
119 7 return $self;
120 }
121  
122 1 ▶sub send_bytes {
Subroutine send_bytes: called CC=2 SCAR=7.0
Pod documented
123 1 my ($self, $bytes) = @_;
124  
125 1 ▶ croak "Cannot send on closed WebSocket" if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 1 1
126  
127 1 push @{$self->{send_queue}}, {
128 type => 'websocket.receive',
129 bytes => $bytes,
130 };
131  
132 # Pump the app to process this message
133 1 $self->_pump_app;
134  
135 1 return $self;
136 }
137  
138 1 ▶sub send_json {
Subroutine send_json: called CC=1 SCAR=0.0
Pod documented
139 1 my ($self, $data) = @_;
140  
141 1 require JSON::MaybeXS;
142 1 my $text = JSON::MaybeXS::encode_json($data);
143  
144 1 return $self->send_text($text);
145 }
146  
147 11 ▶sub receive_text {
Subroutine receive_text: called CC=6 SCAR=18.4
Pod documented
148 11 my ($self, $timeout) = @_;
149 11 ▶ $timeout //= 5;
Condition100%2/2
$timeout //= 5
l!l
110
MC/DC100%1/1
$timeout //= 5
$timeout
Truth table100%2/2
A
A= $timeout
Aresult
11
00
150  
151 # Check if we have a text message already waiting
152 11 for my $i (0 .. $#{$self->{recv_queue}}) {
153 10 my $event = $self->{recv_queue}[$i];
154 10 ▶ if ($event->{type} eq 'websocket.send' && exists $event->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "websocket.send" and exists $event->{"text"}) 10 0 10
Condition33%1/3
$event->{"type"} eq "websocket.send" and exists $event->{"text"}
!ll&&!rl&&r
0010
MC/DC0%0/2
$event->{"type"} eq "websocket.send" and exists $event->{"text"}
$event->{"type"} eq "websocket.send" exists $event->{"text"}
Truth table33%1/3
A and B
A= $event->{"type"} eq "websocket.send"B= exists $event->{"text"}
ABresult
0X0
100
111
155 10 splice @{$self->{recv_queue}}, $i, 1;
156 10 return $event->{text};
157 }
158 }
159  
160 # Check if connection closed
161 1 ▶ return undef if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 1 1
162  
163 # No message available yet
164 1 croak "Timeout waiting for WebSocket text message";
165 }
166  
167 1 ▶sub receive_bytes {
Subroutine receive_bytes: called CC=6 SCAR=21.2
Pod documented
168 1 my ($self, $timeout) = @_;
169 1 ▶ $timeout //= 5;
Condition50%1/2
$timeout //= 5
l!l
01
MC/DC0%0/1
$timeout //= 5
$timeout
Truth table50%1/2
A
A= $timeout
Aresult
11
00
170  
171 # Check if we have a bytes message waiting
172 1 for my $i (0 .. $#{$self->{recv_queue}}) {
173 1 my $event = $self->{recv_queue}[$i];
174 1 ▶ if ($event->{type} eq 'websocket.send' && exists $event->{bytes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "websocket.send" and exists $event->{"bytes"}) 1 0 1
Condition33%1/3
$event->{"type"} eq "websocket.send" and exists $event->{"bytes"}
!ll&&!rl&&r
001
MC/DC0%0/2
$event->{"type"} eq "websocket.send" and exists $event->{"bytes"}
$event->{"type"} eq "websocket.send" exists $event->{"bytes"}
Truth table33%1/3
A and B
A= $event->{"type"} eq "websocket.send"B= exists $event->{"bytes"}
ABresult
0X0
100
111
175 1 splice @{$self->{recv_queue}}, $i, 1;
176 1 return $event->{bytes};
177 }
178 }
179  
180 # Check if connection closed
181 0 ▶ return undef if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 0 0
182  
183 # No message available yet
184 0 croak "Timeout waiting for WebSocket bytes message";
185 }
186  
187 2 ▶sub receive_json {
Subroutine receive_json: called CC=2 SCAR=7.0
Pod documented
188 2 my ($self, $timeout) = @_;
189  
190 2 my $text = $self->receive_text($timeout);
191 2 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 2 2
192  
193 2 require JSON::MaybeXS;
194 2 return JSON::MaybeXS::decode_json($text);
195 }
196  
197 12 ▶sub close {
Subroutine close: called CC=4 SCAR=13.9
Pod documented
198 12 my ($self, $code, $reason) = @_;
199  
200 12 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 12 12
201  
202 12 ▶ $code //= 1000;
Condition100%2/2
$code //= 1000
l!l
111
MC/DC100%1/1
$code //= 1000
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
203 12 ▶ $reason //= '';
Condition100%2/2
$reason //= ""
l!l
111
MC/DC100%1/1
$reason //= ""
$reason
Truth table100%2/2
A
A= $reason
Aresult
11
00
204  
205 12 $self->{closed} = 1;
206 12 $self->{close_code} = $code;
207 12 $self->{close_reason} = $reason;
208  
209 # Push disconnect event
210 12 push @{$self->{send_queue}}, {
211 type => 'websocket.disconnect',
212 code => $code,
213 reason => $reason,
214 };
215  
216 # Pump the app to let it process the disconnect
217 12 $self->_pump_app;
218  
219 12 return $self;
220 }
221  
222 2 ▶sub close_code {
Subroutine close_code: called CC=1 SCAR=0.0
Pod documented
223 2 my ($self) = @_;
224 2 return $self->{close_code};
225 }
226  
227 1 ▶sub close_reason {
Subroutine close_reason: called CC=1 SCAR=0.0
Pod documented
228 1 my ($self) = @_;
229 1 return $self->{close_reason};
230 }
231  
232 14 ▶sub is_closed {
Subroutine is_closed: called CC=1 SCAR=0.0
Pod documented
233 14 my ($self) = @_;
234 14 return $self->{closed};
235 }
236  
237 5 1;
238  
239 __END__
« blib/lib/PAGI/Middleware/Cookie.pm ↑ Summary lib/PAGI/Middleware/SSE/Retry.pm »