blib/lib/Discord/Client/Gateway.pm

filter: Statementstmt 93.4% 470 / 503 Branchbran 68.7% 187 / 272 Conditioncond 57.8% 162 / 280 MC/DCmcdc 31.5% 48 / 152 Subroutinesub 93.5% 73 / 78 Podpod 100% 11 / 11 totaltotal 73.3% 951 / 1296 CC 246 SCAR 27.9
CC
246
Cov
78%
new
60.9
_open
26.7
__ANON__
24.8
CRAP 16.2
« blib/lib/Discord/Client/REST.pm ↑ Summary blib/lib/Discord/Client/Gateway/Session.pm »
1 package Discord::Client::Gateway;
2  
3 11 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Scalar::Util qw(blessed refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Discord::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Discord::Client::REST;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Discord::Client::Gateway::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Discord::Client::Gateway::IdentifyLimiter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Discord::Client::Gateway::WebSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 our $VERSION = '0.003';
20  
21 # Connection-local send limits; protocol traffic shares this window with application writes.
22 11 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 OUTGOING_WINDOW_SECONDS => 60,
24 MAX_SENDS_PER_WINDOW => 120,
25 MAX_PAYLOAD_BYTES => 4096,
26 11 };
27  
28 58 ▶sub new {
Subroutine new: called CC=33 SCAR=41.1
Pod documented
29 58 my ( $class, %args ) = @_;
30  
31 # Keep protocol state independent of the loop and the WebSocket transport.
32 58 ▶ my $loop = $args{loop} // IO::Async::Loop->new;
Condition33%1/3
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
5800
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
Truth table33%1/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
33 my $session = Discord::Client::Gateway::Session->new(
34 token => $args{token},
35 intents => $args{intents} // 0,
36 1 ▶ random => $args{random} // sub { rand() },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 shard => $args{shard},
38 58 ▶ );
Condition80%4/5
$args{"intents"} // 0
l!l
4612
$args{"random"} // sub { rand(); }
l!l&&r!l&&!r
5710
MC/DC100%1/1
$args{"intents"} // 0
$args{"intents"}
MC/DC0%0/2
$args{"random"} // sub { rand(); }
$args{"random"} sub { rand(); }
Truth table100%2/2
A
A= $args{"intents"}
Aresult
11
00
Truth table66%2/3
A // B
A= $args{"random"}B= sub { rand(); }
ABresult
1X1
011
000
39 my $self = bless {
40 loop => $loop,
41 session => $session,
42 state => 'idle',
43 9 ▶ clock => $args{clock} // sub { clock_gettime(CLOCK_MONOTONIC) },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 0 ▶ random => $args{random} // sub { rand() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 3 ▶ delay => $args{delay} // sub { $loop->delay_future( after => $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 4 ▶ defer => $args{defer} // sub { $loop->later( $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 websocket_factory => $args{websocket_factory},
48 websocket_options => $args{websocket_options} // {},
49 shard_id => $args{shard} ? $args{shard}[0] : 0,
50 connect_timeout => $args{connect_timeout} // 30,
51 stable_after => $args{stable_after} // 60,
52 backoff_base => $args{backoff_base} // 1,
53 backoff_max => $args{backoff_max} // 60,
54 max_outgoing => $args{max_outgoing} // 128,
55 max_events => $args{max_events} // 1024,
56 max_handlers => $args{max_handlers} // 64,
57 handlers => {},
58 dispatch_observers => [],
59 event_queue => [],
60 handler_futures => {},
61 outgoing => [],
62 timers => {},
63 counters => {},
64 generation => 0,
65 attempts => 0,
66 send_times => [],
67 on_error => $args{on_error},
68 on_diagnostic => $args{on_diagnostic},
69 57 ▶ }, $class;
Branch: 1 branch
BranchTrueFalseTotal
$args{'shard'} ? : 13 44 57
Condition85%24/28
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
l!l&&r!l&&!r
5610
$args{"random"} // sub { rand(); }
l!l&&r!l&&!r
5610
$args{"delay"} // sub { $loop->delay_future("after", $_[0]); }
l!l&&r!l&&!r
5610
$args{"defer"} // sub { $loop->later($_[0]); }
l!l&&r!l&&!r
5520
$args{"websocket_options"} // {}
l!l
255
$args{"connect_timeout"} // 30
l!l
255
$args{"stable_after"} // 60
l!l
156
$args{"backoff_base"} // 1
l!l
156
$args{"backoff_max"} // 60
l!l
156
$args{"max_outgoing"} // 128
l!l
156
$args{"max_events"} // 1024
l!l
750
$args{"max_handlers"} // 64
l!l
552
MC/DC0%0/2
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
$args{"clock"} sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
MC/DC0%0/2
$args{"random"} // sub { rand(); }
$args{"random"} sub { rand(); }
MC/DC0%0/2
$args{"delay"} // sub { $loop->delay_future("after", $_[0]); }
$args{"delay"} sub { $loop->delay_future("after", $_[0]); }
MC/DC0%0/2
$args{"defer"} // sub { $loop->later($_[0]); }
$args{"defer"} sub { $loop->later($_[0]); }
MC/DC100%1/1
$args{"websocket_options"} // {}
$args{"websocket_options"}
MC/DC100%1/1
$args{"connect_timeout"} // 30
$args{"connect_timeout"}
MC/DC100%1/1
$args{"stable_after"} // 60
$args{"stable_after"}
MC/DC100%1/1
$args{"backoff_base"} // 1
$args{"backoff_base"}
MC/DC100%1/1
$args{"backoff_max"} // 60
$args{"backoff_max"}
MC/DC100%1/1
$args{"max_outgoing"} // 128
$args{"max_outgoing"}
MC/DC100%1/1
$args{"max_events"} // 1024
$args{"max_events"}
MC/DC100%1/1
$args{"max_handlers"} // 64
$args{"max_handlers"}
Truth table66%2/3
A // B
A= $args{"clock"}B= sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"random"}B= sub { rand(); }
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"delay"}B= sub { $loop->delay_future("after", $_[0]); }
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"defer"}B= sub { $loop->later($_[0]); }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{"websocket_options"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"connect_timeout"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"stable_after"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"backoff_base"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"backoff_max"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_outgoing"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_events"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_handlers"}
Aresult
11
00
70  
71 # Validate resource bounds and hooks before attaching owned network resources.
72 57 for my $field (qw(max_outgoing max_events max_handlers)) {
73 croak "$field must be a positive integer"
74 unless defined( $self->{$field} )
75 && !ref( $self->{$field} )
76 && $self->{$field} =~ /\A\d+\z/
77 171 ▶ && $self->{$field} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+\z/u and $self->{$field} > 0 0 171 171
Condition33%3/9
defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+\z/u and $self->{$field} > 0
!ll&&!rl&&r
00171
defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+\z/u
!ll&&!rl&&r
00171
defined $self->{$field} and not ref $self->{$field}
!ll&&!rl&&r
00171
MC/DC0%0/4
defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+\z/u and $self->{$field} > 0
defined $self->{$field} not ref $self->{$field} $self->{$field} =~ /\A\d+\z/u $self->{$field} > 0
Truth table20%1/5
A and not(B) and C and D
A= defined $self->{$field}B= not ref $self->{$field}C= $self->{$field} =~ /\A\d+\z/uD= $self->{$field} > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
78 }
79 57 for my $field (qw(connect_timeout stable_after backoff_base backoff_max)) {
80 croak "$field must be positive"
81 unless defined( $self->{$field} )
82 && !ref( $self->{$field} )
83 && $self->{$field} =~ /\A\d+(?:\.\d+)?\z/
84 228 ▶ && $self->{$field} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+(?:\.\d+)?\z/u and $self->{$field} > 0 0 228 228
Condition33%3/9
defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+(?:\.\d+)?\z/u and $self->{$field} > 0
!ll&&!rl&&r
00228
defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+(?:\.\d+)?\z/u
!ll&&!rl&&r
00228
defined $self->{$field} and not ref $self->{$field}
!ll&&!rl&&r
00228
MC/DC0%0/4
defined $self->{$field} and not ref $self->{$field} and $self->{$field} =~ /\A\d+(?:\.\d+)?\z/u and $self->{$field} > 0
defined $self->{$field} not ref $self->{$field} $self->{$field} =~ /\A\d+(?:\.\d+)?\z/u $self->{$field} > 0
Truth table20%1/5
A and not(B) and C and D
A= defined $self->{$field}B= not ref $self->{$field}C= $self->{$field} =~ /\A\d+(?:\.\d+)?\z/uD= $self->{$field} > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
85 }
86 57 for my $field (qw(clock random delay defer websocket_factory on_error on_diagnostic)) {
87 399 ▶ croak "$field must be a code reference" if defined( $args{$field} ) && ref( $args{$field} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$field} and ref $args{$field} ne "CODE" 0 399 399
Condition66%2/3
defined $args{$field} and ref $args{$field} ne "CODE"
!ll&&!rl&&r
223770
MC/DC0%0/2
defined $args{$field} and ref $args{$field} ne "CODE"
defined $args{$field} ref $args{$field} ne "CODE"
Truth table66%2/3
A and B
A= defined $args{$field}B= ref $args{$field} ne "CODE"
ABresult
0X0
100
111
88 }
89 57 ▶ croak 'websocket_options must be a hash reference' unless ref( $self->{websocket_options} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"websocket_options"} eq "HASH" 0 57 57
90  
91 # Share discovery and Identify scheduling when a shard manager provides them.
92 57 ▶ $self->{rest} = $args{rest} // Discord::Client::REST->new( token => $args{token}, loop => $loop );
Condition33%1/3
$args{"rest"} // "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop)
l!l&&r!l&&!r
5700
MC/DC0%0/2
$args{"rest"} // "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop)
$args{"rest"} "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop)
Truth table33%1/3
A // B
A= $args{"rest"}B= "Discord::Client::REST"->new("token", $args{"token"}, "loop", $loop)
ABresult
1X1
011
000
93 57 $self->{own_rest} = !$args{rest};
94 $self->{identify_limiter} = $args{identify_limiter} // Discord::Client::Gateway::IdentifyLimiter->new(
95 loop => $loop,
96 rest => $self->{rest},
97 clock => $self->{clock},
98 delay => $self->{delay}
99 57 ▶ );
Condition66%2/3
$args{"identify_limiter"} // "Discord::Client::Gateway::IdentifyLimiter"->new("loop", $loop, "rest", $self->{"rest"}, "clock", $self->{"clock"}, "delay", $self->{"delay"})
l!l&&r!l&&!r
14430
MC/DC0%0/2
$args{"identify_limiter"} // "Discord::Client::Gateway::IdentifyLimiter"->new("loop", $loop, "rest", $self->{"rest"}, "clock", $self->{"clock"}, "delay", $self->{"delay"})
$args{"identify_limiter"} "Discord::Client::Gateway::IdentifyLimiter"->new("loop", $loop, "rest", $self->{"rest"}, "clock", $self->{"clock"}, "delay", $self->{"delay"})
Truth table66%2/3
A // B
A= $args{"identify_limiter"}B= "Discord::Client::Gateway::IdentifyLimiter"->new("loop", $loop, "rest", $self->{"rest"}, "clock", $self->{"clock"}, "delay", $self->{"delay"})
ABresult
1X1
011
000
100 57 $self->{own_limiter} = !$args{identify_limiter};
101 57 $self->{completion} = $loop->new_future;
102 57 return $self;
103 }
104  
105 0 ▶sub loop { $_[0]->{loop} }
Subroutine loop: not called CC=1 SCAR=6.9
Pod documented
106 58 ▶sub state { $_[0]->{state} }
Subroutine state: called CC=1 SCAR=0.0
Pod documented
107 0 ▶sub session { $_[0]->{session} }
Subroutine session: not called CC=1 SCAR=6.9
Pod documented
108 22 ▶sub completion { $_[0]->{completion} }
Subroutine completion: called CC=1 SCAR=0.0
Pod documented
109 1 ▶sub identify_limiter { $_[0]->{identify_limiter} }
Subroutine identify_limiter: called CC=1 SCAR=0.0
Pod documented
110  
111 19 ▶sub stats {
Subroutine stats: called CC=2 SCAR=7.0
Pod documented
112 19 my ($self) = @_;
113  
114 # Report queue sizes and timing without retaining or exposing payloads.
115 return {
116 19 %{ $self->{counters} },
117 state => $self->{state},
118 19 pending_events => scalar( @{ $self->{event_queue} } ),
119 19 active_handlers => scalar( keys %{ $self->{handler_futures} } ),
120 19 ▶ pending_writes => scalar( @{ $self->{outgoing} } ) + ( $self->{active_write} ? 1 : 0 ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'active_write'} ? : 0 19 19
121 heartbeat_latency => $self->{heartbeat_latency}
122 19 };
123 }
124  
125 51 ▶sub on {
Subroutine on: called CC=5 SCAR=19.6
Pod documented
126 51 my ( $self, $event, $callback ) = @_;
127  
128 # Validate the event before adding a callback to the delivery registry.
129 51 ▶ croak 'event name and callback are required'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $event and not ref $event and length $event and ref $callback eq "CODE" 0 51 51
Condition33%3/9
defined $event and not ref $event and length $event and ref $callback eq "CODE"
!ll&&!rl&&r
0051
defined $event and not ref $event and length $event
!ll&&!rl&&r
0051
defined $event and not ref $event
!ll&&!rl&&r
0051
MC/DC0%0/4
defined $event and not ref $event and length $event and ref $callback eq "CODE"
defined $event not ref $event length $event ref $callback eq "CODE"
Truth table20%1/5
A and not(B) and C and D
A= defined $eventB= not ref $eventC= length $eventD= ref $callback eq "CODE"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
130 unless defined($event)
131 && !ref($event)
132 && length($event)
133 && ref($callback) eq 'CODE';
134 51 push @{ $self->{handlers}{$event} }, $callback;
135 51 return $self;
136 }
137  
138 # SDK state consumers run before the bounded application callback queue.
139 30 ▶sub _on_dispatch {
Subroutine _on_dispatch: called CC=2 SCAR=7.0
140 30 my ( $self, $callback ) = @_;
141 30 ▶ croak 'dispatch observer must be a code reference' unless ref($callback) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 30 30
142  
143 30 push @{ $self->{dispatch_observers} }, $callback;
144 30 return $self;
145 }
146  
147 60 ▶sub connect {
Subroutine connect: called CC=6 SCAR=19.6
Pod documented
148 60 my ($self) = @_;
149  
150 # Reuse an outstanding startup wait; a reconnect needs a fresh ready notification.
151 60 ▶ return $self->_failed( 'Gateway is closed', 'shutdown' ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 60 60
152 60 ▶ return $self->{loop}->new_future->done($self) if $self->{state} eq 'ready';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} eq "ready" 2 58 60
153 58 ▶ return $self->{ready_future} if $self->{ready_future} && !$self->{ready_future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ready_future"} and not $self->{"ready_future"}->is_ready 0 58 58
Condition33%1/3
$self->{"ready_future"} and not $self->{"ready_future"}->is_ready
!ll&&!rl&&r
5800
MC/DC0%0/2
$self->{"ready_future"} and not $self->{"ready_future"}->is_ready
$self->{"ready_future"} not $self->{"ready_future"}->is_ready
Truth table33%1/3
A and not(B)
A= $self->{"ready_future"}B= not $self->{"ready_future"}->is_ready
ABresult
0X0
100
111
154 58 my $ready = $self->{loop}->new_future;
155 58 $self->{ready_future} = $ready;
156 58 ▶ $ready->on_cancel( sub { $self->close } );
Subroutine __ANON__: called CC=1 SCAR=0.0
157 58 ▶ return $ready if $self->{running};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"running"} 3 55 58
158  
159 # Discover the endpoint once before opening the first connection.
160 55 $self->{running} = 1;
161 55 $self->{state} = 'discovering';
162 55 my $info = $self->{identify_limiter}->info;
163 55 $self->{discovery} = $info;
164  
165 # Discovery completion may arrive after the caller has closed the Gateway.
166 $info->on_ready(
167 55 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=22.5
168 55 delete $self->{discovery};
169 55 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 55 55
170 55 ▶ if ( $info->is_done ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($info->is_done) { } 55 0 55
elsif ($info->is_failed) { } 0 0 0
171 55 ( $self->{gateway_info} ) = $info->result;
172 55 $self->_open;
173 }
174 elsif ( $info->is_failed ) {
175 0 my ( undef, $category ) = $info->failure;
176 0 ▶ $self->_fatal( 'Gateway discovery failed', $category // 'transport' );
Condition0%0/2
$category // "transport"
l!l
00
MC/DC0%0/1
$category // "transport"
$category
Truth table0%0/2
A
A= $category
Aresult
11
00
177 }
178 else {
179 0 $self->_fatal( 'Gateway discovery cancelled', 'transport' );
180 }
181 }
182 55 );
183 55 return $ready;
184 }
185  
186 67 ▶sub _open {
Subroutine _open: called CC=17 SCAR=32.8
187 67 my ($self) = @_;
188 67 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 67 67
189  
190 # Tag callbacks so an old socket cannot mutate a newer connection.
191 67 my $generation = ++$self->{generation};
192 67 ▶ my $url = $self->{session}->can_resume ? $self->{session}->resume_gateway_url : $self->{gateway_info}{url};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'session'}->can_resume ? : 11 56 67
193 67 my $uri = URI->new($url);
194 67 ▶ unless ( ( $uri->scheme // '' ) eq 'wss'
Branch: 1 branch
BranchTrueFalseTotal
unless (($uri->scheme // "") eq "wss" and $uri->host and not defined $uri->userinfo and not defined $uri->fragment) 0 67 67
Condition36%4/11
($uri->scheme // "") eq "wss" and $uri->host and not defined $uri->userinfo and not defined $uri->fragment
!ll&&!rl&&r
0067
($uri->scheme // "") eq "wss" and $uri->host and not defined $uri->userinfo
!ll&&!rl&&r
0067
($uri->scheme // "") eq "wss" and $uri->host
!ll&&!rl&&r
0067
$uri->scheme // ""
l!l
670
MC/DC0%0/4
($uri->scheme // "") eq "wss" and $uri->host and not defined $uri->userinfo and not defined $uri->fragment
$uri->scheme $uri->host not defined $uri->userinfo not defined $uri->fragment
Truth table20%1/5
A and B and not(C) and not(D)
A= $uri->schemeB= $uri->hostC= not defined $uri->userinfoD= not defined $uri->fragment
ABCDresult
0XXX0
10XX0
110X0
11100
11111
195 && $uri->host
196 && !defined( $uri->userinfo )
197 && !defined( $uri->fragment ) )
198 {
199 0 $self->_fatal( 'Invalid Gateway URL', 'protocol' );
200 0 return;
201 }
202  
203 # Metadata and Resume URLs may omit a path; HTTP upgrades still need '/'.
204 67 ▶ $uri->path('/') unless length( $uri->path );
Branch: 1 branch
BranchTrueFalseTotal
unless length $uri->path 2 65 67
205 67 $uri->query_form( v => 10, encoding => 'json' );
206  
207 # Transport callbacks do not own the Gateway; stale callbacks are ignored.
208 67 my $weak = $self;
209 67 weaken($weak);
210 my %options = (
211 67 %{ $self->{websocket_options} },
212 loop => $self->{loop},
213 208 ▶ on_text => sub { $weak->_receive( $_[0], $generation ) if $weak },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $weak 208 0 208
214 7 ▶ on_close => sub { $weak->_closed( $_[0], $generation ) if $weak }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $weak 7 0 7
215 67 );
216  
217 # Validate injected adapters before using their asynchronous transport methods.
218 67 my $websocket = eval {
219 $self->{websocket_factory}
220 67 ▶ ? $self->{websocket_factory}->(%options)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'websocket_factory'} ? : 64 3 67
221 : Discord::Client::Gateway::WebSocket->new(%options);
222 };
223 67 ▶ unless ( blessed($websocket)
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($websocket) and $websocket->can("connect") and $websocket->can("send") and $websocket->can("abort") and $websocket->can("close")) 0 67 67
Condition33%4/12
builtin::blessed($websocket) and $websocket->can("connect") and $websocket->can("send") and $websocket->can("abort") and $websocket->can("close")
!ll&&!rl&&r
0067
builtin::blessed($websocket) and $websocket->can("connect") and $websocket->can("send") and $websocket->can("abort")
!ll&&!rl&&r
0067
builtin::blessed($websocket) and $websocket->can("connect") and $websocket->can("send")
!ll&&!rl&&r
0067
builtin::blessed($websocket) and $websocket->can("connect")
!ll&&!rl&&r
0067
MC/DC0%0/5
builtin::blessed($websocket) and $websocket->can("connect") and $websocket->can("send") and $websocket->can("abort") and $websocket->can("close")
builtin::blessed($websocket) $websocket->can("connect") $websocket->can("send") $websocket->can("abort") $websocket->can("close")
Truth table16%1/6
A and B and C and D and E
A= builtin::blessed($websocket)B= $websocket->can("connect")C= $websocket->can("send")D= $websocket->can("abort")E= $websocket->can("close")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
224 && $websocket->can('connect')
225 && $websocket->can('send')
226 && $websocket->can('abort')
227 && $websocket->can('close') )
228 {
229 0 $self->_fatal( 'Invalid Gateway transport', 'transport' );
230 0 return;
231 }
232  
233 # Bound connection establishment and HELLO receipt with one handshake deadline.
234 67 $self->{websocket} = $websocket;
235 67 $self->{state} = 'connecting';
236 $self->_timer(
237 handshake => $self->{connect_timeout},
238 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
239 1 $self->_reconnect( 0, 'Gateway handshake timed out', 'timeout' );
240 }
241 67 );
242 67 my $connect = eval { $websocket->connect( $uri->as_string ) };
243 67 ▶ unless ( blessed($connect) && $connect->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($connect) and $connect->isa("Future")) 0 67 67
Condition33%1/3
builtin::blessed($connect) and $connect->isa("Future")
!ll&&!rl&&r
0067
MC/DC0%0/2
builtin::blessed($connect) and $connect->isa("Future")
builtin::blessed($connect) $connect->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($connect)B= $connect->isa("Future")
ABresult
0X0
100
111
244 0 $self->_reconnect( 0, 'Gateway connection failed', 'transport' );
245 0 return;
246 }
247 67 $self->{connection_future} = $connect;
248  
249 # Enter the protocol handshake only after WebSocket connection succeeds.
250 $connect->on_ready(
251 67 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
252 67 ▶ return if $self->{closed} || $generation != $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $generation != $self->{"generation"} 4 63 67
Condition100%3/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
3163
MC/DC100%2/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table100%3/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
253 63 delete $self->{connection_future};
254 63 ▶ if ( $connect->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connect->is_done) { } 62 1 63
255 62 $self->{session}->opened;
256 62 $self->{state} = 'awaiting_hello';
257 62 $self->_diagnostic('connected');
258 }
259 else {
260 1 $self->_reconnect( 0, 'Gateway connection failed', 'transport' );
261 }
262 }
263 67 );
264 }
265  
266 208 ▶sub _receive {
Subroutine _receive: called CC=9 SCAR=22.0
267 208 my ( $self, $text, $generation ) = @_;
268 208 ▶ return if $self->{closed} || $generation != $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $generation != $self->{"generation"} 1 207 208
Condition66%2/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
01207
MC/DC50%1/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table66%2/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
269  
270 # Decode and validate the packet before applying any transport actions.
271 207 my $packet = eval { JSON::PP->new->decode($text) };
272 207 ▶ my $actions = !$@ ? eval { $self->{session}->receive($packet) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
!$@ ? : 206 1 207
273 207 ▶ if ( !$actions || $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $actions or $@) 1 206 207
Condition66%2/3
not $actions or $@
l!l&&r!l&&!r
10206
MC/DC50%1/2
not $actions or $@
not $actions $@
Truth table66%2/3
not(A) or B
A= not $actionsB= $@
ABresult
1X1
011
000
274 1 $self->_reconnect( 0, 'Invalid Gateway packet', 'protocol' );
275 1 return;
276 }
277  
278 # HELLO ends the connection deadline; Identify may wait on a shared permit.
279 206 ▶ if ( $packet->{op} == 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->{"op"} == 10) 62 144 206
280 62 $self->{state} = 'authenticating';
281 62 $self->_cancel_timer('handshake');
282 }
283  
284 # Measure heartbeat acknowledgement latency independently of event callbacks.
285 206 ▶ if ( $packet->{op} == 11 && defined( $self->{heartbeat_sent_at} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->{"op"} == 11 and defined $self->{"heartbeat_sent_at"}) 20 186 206
Condition66%2/3
$packet->{"op"} == 11 and defined $self->{"heartbeat_sent_at"}
!ll&&!rl&&r
186020
MC/DC50%1/2
$packet->{"op"} == 11 and defined $self->{"heartbeat_sent_at"}
$packet->{"op"} == 11 defined $self->{"heartbeat_sent_at"}
Truth table66%2/3
A and B
A= $packet->{"op"} == 11B= defined $self->{"heartbeat_sent_at"}
ABresult
0X0
100
111
286 20 $self->{heartbeat_latency} = $self->{clock}->() - $self->{heartbeat_sent_at};
287 20 $self->_diagnostic('heartbeat_ack');
288 }
289 206 $self->_actions( $actions, $packet );
290 }
291  
292 229 ▶sub _actions {
Subroutine _actions: called CC=18 SCAR=29.2
293 229 my ( $self, $actions, $packet ) = @_;
294  
295 # Execute protocol decisions in order, stopping if a callback closes the Gateway.
296 229 for my $action (@$actions) {
297 271 ▶ last if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 271 271
298 271 ▶ if ( $action->{action} eq 'heartbeat_schedule' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($action->{'action'} eq 'heartbeat_schedule') { } 62 209 271
elsif ($action->{'action'} eq 'send') { } 84 125 209
elsif ($action->{'action'} eq 'event') { } 120 5 125
elsif ($action->{'action'} eq 'reconnect') { } 5 0 5
elsif ($action->{'action'} eq 'opcode') { } 0 0 0
299 62 $self->{heartbeat_interval} = $action->{interval_ms} / 1000;
300 62 ▶ $self->_timer( heartbeat => $action->{first_delay_ms} / 1000, sub { $self->_heartbeat } );
Subroutine __ANON__: called CC=1 SCAR=0.0
301 }
302 elsif ( $action->{action} eq 'send' ) {
303 84 my $payload = $action->{payload};
304 84 ▶ if ( $payload->{op} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{'op'} == 2) { } 53 31 84
305 53 $self->_identify($payload);
306 }
307 else {
308 31 $self->_protocol_send($payload);
309 }
310 }
311 elsif ( $action->{action} eq 'event' ) {
312  
313 # A ready session clears authentication timeout and begins the stability window.
314 120 ▶ if ( $action->{name} eq 'READY' || $action->{name} eq 'RESUMED' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->{"name"} eq "READY" or $action->{"name"} eq "RESUMED") 59 61 120
Condition100%3/3
$action->{"name"} eq "READY" or $action->{"name"} eq "RESUMED"
l!l&&r!l&&!r
52761
MC/DC100%2/2
$action->{"name"} eq "READY" or $action->{"name"} eq "RESUMED"
$action->{"name"} eq "READY" $action->{"name"} eq "RESUMED"
Truth table100%3/3
A or B
A= $action->{"name"} eq "READY"B= $action->{"name"} eq "RESUMED"
ABresult
1X1
011
000
315 59 $self->{state} = 'ready';
316 1 ▶ $self->_timer( stable => $self->{stable_after}, sub { $self->{attempts} = 0 } )
Subroutine __ANON__: called CC=1 SCAR=0.0
317 59 ▶ if $self->{attempts};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attempts"} 7 52 59
318 59 $self->_cancel_timer('handshake');
319 59 $self->_diagnostic( lc $action->{name} );
320 }
321 120 $self->_dispatch( $action->{name}, $action->{data}, $packet );
322 120 ▶ if ( $action->{name} eq 'READY' || $action->{name} eq 'RESUMED' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->{"name"} eq "READY" or $action->{"name"} eq "RESUMED") 59 61 120
Condition100%3/3
$action->{"name"} eq "READY" or $action->{"name"} eq "RESUMED"
l!l&&r!l&&!r
52761
MC/DC100%2/2
$action->{"name"} eq "READY" or $action->{"name"} eq "RESUMED"
$action->{"name"} eq "READY" $action->{"name"} eq "RESUMED"
Truth table100%3/3
A or B
A= $action->{"name"} eq "READY"B= $action->{"name"} eq "RESUMED"
ABresult
1X1
011
000
323  
324 # Startup observers see Client state updated from the same packet.
325 59 my $ready = delete $self->{ready_future};
326 59 ▶ $ready->done($self) if $ready && !$ready->is_ready && !$self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $ready and not $ready->is_ready and not $self->{"closed"} 55 4 59
Condition66%4/6
$ready and not $ready->is_ready and not $self->{"closed"}
!ll&&!rl&&r
4055
$ready and not $ready->is_ready
!ll&&!rl&&r
4055
MC/DC33%1/3
$ready and not $ready->is_ready and not $self->{"closed"}
$ready not $ready->is_ready not $self->{"closed"}
Truth table50%2/4
A and not(B) and not(C)
A= $readyB= not $ready->is_readyC= not $self->{"closed"}
ABCresult
0XX0
10X0
1100
1111
327 }
328 }
329 elsif ( $action->{action} eq 'reconnect' ) {
330 $self->_reconnect(
331 $action->{delay_ms} / 1000,
332 'Gateway session reconnect requested',
333 5 ▶ 'reconnect', $packet && $packet->{op} == 7
Condition100%3/3
$packet && $packet->{'op'} == 7
!ll&&!rl&&r
212
MC/DC100%2/2
$packet && $packet->{'op'} == 7
$packet $packet->{'op'} == 7
Truth table100%3/3
A && B
A= $packetB= $packet->{'op'} == 7
ABresult
0X0
100
111
334 );
335 5 last;
336 }
337 elsif ( $action->{action} eq 'opcode' ) {
338 0 $self->_dispatch( 'OPCODE', { op => $action->{op}, d => $action->{data} }, $packet );
339 }
340 }
341 }
342  
343 23 ▶sub _heartbeat {
Subroutine _heartbeat: called CC=5 SCAR=16.7
344 23 my ($self) = @_;
345 23 ▶ return if $self->{closed} || !$self->{websocket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or not $self->{"websocket"} 0 23 23
Condition33%1/3
$self->{"closed"} or not $self->{"websocket"}
l!l&&r!l&&!r
0023
MC/DC0%0/2
$self->{"closed"} or not $self->{"websocket"}
$self->{"closed"} not $self->{"websocket"}
Truth table33%1/3
A or not(B)
A= $self->{"closed"}B= not $self->{"websocket"}
ABresult
1X1
011
000
346  
347 # A missed acknowledgement can replace the connection instead of sending a beat.
348 23 my $generation = $self->{generation};
349 23 $self->_actions( $self->{session}->heartbeat_due );
350 23 ▶ return if $generation != $self->{generation} || $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $generation != $self->{"generation"} or $self->{"closed"} 2 21 23
Condition66%2/3
$generation != $self->{"generation"} or $self->{"closed"}
l!l&&r!l&&!r
2021
MC/DC50%1/2
$generation != $self->{"generation"} or $self->{"closed"}
$generation != $self->{"generation"} $self->{"closed"}
Truth table66%2/3
A or B
A= $generation != $self->{"generation"}B= $self->{"closed"}
ABresult
1X1
011
000
351 21 ▶ $self->_timer( heartbeat => $self->{heartbeat_interval}, sub { $self->_heartbeat } );
Subroutine __ANON__: called CC=1 SCAR=0.0
352 }
353  
354 53 ▶sub _identify {
Subroutine _identify: called CC=1 SCAR=1.4
355 53 my ( $self, $payload ) = @_;
356  
357 # Acquire a shared Identify permit without blocking heartbeat processing.
358 53 my $generation = $self->{generation};
359 53 my $grant = $self->{identify_limiter}->acquire( $self->{shard_id} );
360 53 $self->{identify_future} = $grant;
361 $grant->on_ready(
362 53 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=32.1
363 53 ▶ delete $self->{identify_future} if $generation == $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
if $generation == $self->{"generation"} 53 0 53
364 53 ▶ if ( $grant->is_done ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($grant->is_done) { } 53 0 53
elsif (not $grant->is_cancelled || $self->{'closed'} and $generation == $self->{'generation'}) { } 0 0 0
365 53 my ($permit) = $grant->result;
366 53 ▶ if ( $self->{closed} || $generation != $self->{generation} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"} or $generation != $self->{"generation"}) 0 53 53
Condition33%1/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
0053
MC/DC0%0/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
367 0 $permit->cancel;
368 0 return;
369 }
370 53 $self->_protocol_send( $payload, $permit );
371 }
372 ▶ elsif ( !$grant->is_cancelled && !$self->{closed} && $generation == $self->{generation} ) {
Condition0%0/6
not $grant->is_cancelled || $self->{"closed"} and $generation == $self->{"generation"}
!ll&&!rl&&r
000
$grant->is_cancelled || $self->{'closed'}
l!l&&r!l&&!r
000
MC/DC0%0/3
not $grant->is_cancelled || $self->{"closed"} and $generation == $self->{"generation"}
$grant->is_cancelled $self->{'closed'} $generation == $self->{"generation"}
Truth table0%0/4
not(A || B) and C
A= $grant->is_cancelledB= $self->{'closed'}C= $generation == $self->{"generation"}
ABCresult
1XX0
01X0
0000
0011
373 0 $self->_fatal( 'Gateway Identify scheduling failed', 'identify' );
374 }
375 }
376 53 );
377 }
378  
379 84 ▶sub _protocol_send {
Subroutine _protocol_send: called CC=1 SCAR=0.4
380 84 my ( $self, $payload, $permit ) = @_;
381  
382 # Protocol writes have priority and reconnect if their delivery fails.
383 84 my $generation = $self->{generation};
384 84 my $future = $self->_queue_send( $payload, protocol => 1, permit => $permit );
385 $future->on_ready(
386 84 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=17.9
387 84 ▶ return if $self->{closed} || $generation != $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $generation != $self->{"generation"} 0 84 84
Condition33%1/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
0084
MC/DC0%0/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
388 84 ▶ if ( !$future->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $future->is_done) 0 84 84
389 0 $self->_reconnect( 0, 'Gateway protocol write failed', 'transport' );
390 }
391 }
392 84 );
393 }
394  
395 131 ▶sub send {
Subroutine send: called CC=9 SCAR=25.1
Pod documented
396 131 my ( $self, $op, $data ) = @_;
397  
398 # Application sends cannot bypass the internal authentication or heartbeat paths.
399 131 ▶ return $self->_failed( 'Gateway is closed', 'shutdown' ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 131 131
400 131 ▶ return $self->_failed( 'Gateway is not ready', 'state' ) unless $self->{state} eq 'ready';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"state"} eq "ready" 0 131 131
401 131 ▶ return $self->_failed( 'Invalid Gateway opcode', 'request' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op and not ref $op and $op =~ /\A\d+\z/u and $op != 1 and $op != 2 and $op != 6 1 130 131
Condition53%8/15
defined $op and not ref $op and $op =~ /\A\d+\z/u and $op != 1 and $op != 2 and $op != 6
!ll&&!rl&&r
10130
defined $op and not ref $op and $op =~ /\A\d+\z/u and $op != 1 and $op != 2
!ll&&!rl&&r
10130
defined $op and not ref $op and $op =~ /\A\d+\z/u and $op != 1
!ll&&!rl&&r
01130
defined $op and not ref $op and $op =~ /\A\d+\z/u
!ll&&!rl&&r
00131
defined $op and not ref $op
!ll&&!rl&&r
00131
MC/DC16%1/6
defined $op and not ref $op and $op =~ /\A\d+\z/u and $op != 1 and $op != 2 and $op != 6
defined $op not ref $op $op =~ /\A\d+\z/u $op != 1 $op != 2 $op != 6
Truth table28%2/7
A and not(B) and C and D and E and F
A= defined $opB= not ref $opC= $op =~ /\A\d+\z/uD= $op != 1E= $op != 2F= $op != 6
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
402 unless defined($op) && !ref($op) && $op =~ /\A\d+\z/ && $op != 1 && $op != 2 && $op != 6;
403 130 return $self->_queue_send( { op => 0 + $op, d => $data } );
404 }
405  
406 214 ▶sub _queue_send {
Subroutine _queue_send: called CC=11 SCAR=24.5
407 214 my ( $self, $payload, %options ) = @_;
408 214 my $permit = $options{permit};
409 214 ▶ my $is_protocol = $options{protocol} // 0;
Condition100%2/2
$options{"protocol"} // 0
l!l
84130
MC/DC100%1/1
$options{"protocol"} // 0
$options{"protocol"}
Truth table100%2/2
A
A= $options{"protocol"}
Aresult
11
00
410  
411 # Reject disconnected, oversized or excess writes before consuming a permit.
412 214 my $future = $self->{loop}->new_future;
413 214 ▶ unless ( $self->{websocket} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"websocket"}) 0 214 214
414 0 ▶ $permit->cancel if $permit;
Branch: 1 branch
BranchTrueFalseTotal
if $permit 0 0 0
415 0 return $self->_fail( $future, 'Gateway is disconnected', 'state' );
416 }
417 214 my $text = eval { JSON::PP->new->encode($payload) };
418 214 ▶ if ( $@ || length( encode( 'UTF-8', $text ) ) > MAX_PAYLOAD_BYTES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or length &encode("UTF-8", $text) > 4096) 1 213 214
Condition66%2/3
$@ or length &encode("UTF-8", $text) > 4096
l!l&&r!l&&!r
01213
MC/DC50%1/2
$@ or length &encode("UTF-8", $text) > 4096
$@ length &encode("UTF-8", $text) > 4096
Truth table66%2/3
A or B
A= $@B= length &encode("UTF-8", $text) > 4096
ABresult
1X1
011
000
419 1 ▶ $permit->cancel if $permit;
Branch: 1 branch
BranchTrueFalseTotal
if $permit 0 1 1
420 1 return $self->_fail( $future, 'Gateway payload is invalid or exceeds 4096 bytes', 'request' );
421 }
422 213 ▶ if ( @{ $self->{outgoing} } + ( $self->{active_write} ? 1 : 0 ) >= $self->{max_outgoing} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{"outgoing"};} + ($self->{"active_write"} ? 1 : 0) >= $self->{"max_outgoing"}) 1 212 213
$self->{'active_write'} ? : 2 211 213
423 1 ▶ $permit->cancel if $permit;
Branch: 1 branch
BranchTrueFalseTotal
if $permit 0 1 1
424 1 return $self->_fail( $future, 'Gateway write queue capacity exceeded', 'queue_full' );
425 }
426  
427 # Keep protocol traffic ahead of application writes within one serialized queue.
428 212 my $job = { future => $future, text => $text, permit => $permit, op => $payload->{op}, protocol => $is_protocol };
429 212 ▶ if ($is_protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_protocol) { } 84 128 212
430 84 unshift @{ $self->{outgoing} }, $job;
431 }
432 else {
433 128 push @{ $self->{outgoing} }, $job;
434 }
435  
436 # Queued writes can be removed; cancelling an active write makes delivery uncertain.
437 $future->on_cancel(
438 2 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
439 2 @{ $self->{outgoing} } = grep { $_ != $job } @{ $self->{outgoing} };
440 2 ▶ $permit->cancel if $permit;
Branch: 1 branch
BranchTrueFalseTotal
if $permit 0 2 2
441 2 ▶ if ( $self->{active_write} && $self->{active_write} == $job ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"active_write"} and $self->{"active_write"} == $job) 1 1 2
Condition66%2/3
$self->{"active_write"} and $self->{"active_write"} == $job
!ll&&!rl&&r
011
MC/DC50%1/2
$self->{"active_write"} and $self->{"active_write"} == $job
$self->{"active_write"} $self->{"active_write"} == $job
Truth table66%2/3
A and B
A= $self->{"active_write"}B= $self->{"active_write"} == $job
ABresult
0X0
100
111
442 1 $self->_reconnect( 0, 'Active Gateway write cancelled', 'transport' );
443 }
444 }
445 212 );
446 212 $self->_drain_writes;
447 212 return $future;
448 }
449  
450 423 ▶sub _drain_writes {
Subroutine _drain_writes: called CC=12 SCAR=25.1
451 423 my ($self) = @_;
452 423 ▶ return if $self->{active_write} || $self->{draining_writes} || !$self->{websocket} || $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_write"} or $self->{"draining_writes"} or not $self->{"websocket"} or $self->{"closed"} 206 217 423
Condition77%7/9
$self->{"active_write"} or $self->{"draining_writes"} or not $self->{"websocket"} or $self->{"closed"}
l!l&&r!l&&!r
2060217
$self->{"active_write"} or $self->{"draining_writes"} or not $self->{"websocket"}
l!l&&r!l&&!r
2060217
$self->{"active_write"} or $self->{"draining_writes"}
l!l&&r!l&&!r
1205217
MC/DC50%2/4
$self->{"active_write"} or $self->{"draining_writes"} or not $self->{"websocket"} or $self->{"closed"}
$self->{"active_write"} $self->{"draining_writes"} not $self->{"websocket"} $self->{"closed"}
Truth table60%3/5
A or B or not(C) or D
A= $self->{"active_write"}B= $self->{"draining_writes"}C= not $self->{"websocket"}D= $self->{"closed"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
453  
454 # Synchronous transport completion must not recursively drain the same queue.
455 217 local $self->{draining_writes} = 1;
456 217 ▶ while ( @{ $self->{outgoing} } && $self->{websocket} && !$self->{active_write} && !$self->{closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"outgoing"};} and $self->{"websocket"} and not $self->{"active_write"} and not $self->{"closed"}) 219 209 428
Condition66%6/9
@{$self->{"outgoing"};} and $self->{"websocket"} and not $self->{"active_write"} and not $self->{"closed"}
!ll&&!rl&&r
2090219
@{$self->{"outgoing"};} and $self->{"websocket"} and not $self->{"active_write"}
!ll&&!rl&&r
2090219
@{$self->{"outgoing"};} and $self->{"websocket"}
!ll&&!rl&&r
2090219
MC/DC25%1/4
@{$self->{"outgoing"};} and $self->{"websocket"} and not $self->{"active_write"} and not $self->{"closed"}
@{$self->{"outgoing"};} $self->{"websocket"} not $self->{"active_write"} not $self->{"closed"}
Truth table40%2/5
A and B and not(C) and not(D)
A= @{$self->{"outgoing"};}B= $self->{"websocket"}C= not $self->{"active_write"}D= not $self->{"closed"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
457 219 my $now = $self->{clock}->();
458 219 @{ $self->{send_times} } = grep { $_ > $now - OUTGOING_WINDOW_SECONDS } @{ $self->{send_times} };
459 219 my $limit = $self->_outgoing_limit( $self->{outgoing}[0]{protocol} );
460 219 ▶ if ( @{ $self->{send_times} } >= $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"send_times"};} >= $limit) 8 211 219
461 $self->_timer(
462 outbound => $self->{send_times}[0] + OUTGOING_WINDOW_SECONDS - $now,
463 1 ▶ sub { $self->_drain_writes }
Subroutine __ANON__: called CC=1 SCAR=0.0
464 8 );
465 8 last;
466 }
467 211 my $job = shift @{ $self->{outgoing} };
468 211 ▶ last unless $self->_start_write($job);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_start_write($job) 0 211 211
469 }
470 217 ▶ $self->_cancel_timer('outbound') unless @{ $self->{outgoing} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"outgoing"};} 209 8 217
471 }
472  
473 219 ▶sub _outgoing_limit {
Subroutine _outgoing_limit: called CC=4 SCAR=14.0
474 219 my ( $self, $is_protocol ) = @_;
475 219 ▶ return MAX_SENDS_PER_WINDOW if $is_protocol;
Branch: 1 branch
BranchTrueFalseTotal
if $is_protocol 84 135 219
476  
477 # Application traffic leaves room for scheduled heartbeats and additional protocol writes.
478 # The three extra slots are local safety headroom; they do not increase the server quota.
479 135 my $reserved_sends = 4;
480 135 ▶ if ( $self->{heartbeat_interval} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"heartbeat_interval"}) 135 0 135
481 135 my $expected_heartbeats = int( OUTGOING_WINDOW_SECONDS / $self->{heartbeat_interval} );
482 135 $reserved_sends = $expected_heartbeats + 3;
483 }
484  
485 # Even very short heartbeat intervals leave one slot for application traffic.
486 135 ▶ $reserved_sends = MAX_SENDS_PER_WINDOW - 1 if $reserved_sends >= MAX_SENDS_PER_WINDOW;
Branch: 1 branch
BranchTrueFalseTotal
if $reserved_sends >= 120 0 135 135
487 135 return MAX_SENDS_PER_WINDOW - $reserved_sends;
488 }
489  
490 211 ▶sub _start_write {
Subroutine _start_write: called CC=12 SCAR=27.8
491 211 my ( $self, $job ) = @_;
492  
493 # Commit Identify immediately before sending, then account for connection quota.
494 211 $self->{active_write} = $job;
495 211 my $generation = $self->{generation};
496 211 ▶ $job->{permit}->commit if $job->{permit};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"permit"} 53 158 211
497 211 ▶ return 0 if $self->{closed} || $generation != $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $generation != $self->{"generation"} 0 211 211
Condition33%1/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
00211
MC/DC0%0/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
498 211 ▶ if ( $job->{op} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"op"} == 1) 22 189 211
499 22 $self->{heartbeat_sent_at} = $self->{clock}->();
500 22 $self->_diagnostic('heartbeat_sent');
501 }
502 211 ▶ return 0 if $self->{closed} || $generation != $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $generation != $self->{"generation"} 0 211 211
Condition33%1/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
00211
MC/DC0%0/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
503  
504 211 push @{ $self->{send_times} }, $self->{clock}->();
505  
506 # Translate adapter exceptions into a failed write and a connection reset.
507 211 my $write_future = eval { $self->{websocket}->send( $job->{text} ) };
508 211 ▶ unless ( blessed($write_future) && $write_future->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($write_future) and $write_future->isa("Future")) 0 211 211
Condition33%1/3
builtin::blessed($write_future) and $write_future->isa("Future")
!ll&&!rl&&r
00211
MC/DC0%0/2
builtin::blessed($write_future) and $write_future->isa("Future")
builtin::blessed($write_future) $write_future->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($write_future)B= $write_future->isa("Future")
ABresult
0X0
100
111
509 0 delete $self->{active_write};
510 0 $self->_fail( $job->{future}, 'Gateway write failed', 'transport' );
511 0 $self->_reconnect( 0, 'Gateway write failed', 'transport' );
512 0 return 0;
513 }
514  
515 # Authentication timeout starts at send time, after any Identify scheduling delay.
516 211 ▶ if ( $job->{op} =~ /\A(?:2|6)\z/ && $self->{state} ne 'ready' && !$self->{closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($job->{"op"} =~ /\A(?:2|6)\z/u and $self->{"state"} ne "ready" and not $self->{"closed"}) 62 149 211
Condition66%4/6
$job->{"op"} =~ /\A(?:2|6)\z/u and $self->{"state"} ne "ready" and not $self->{"closed"}
!ll&&!rl&&r
149062
$job->{"op"} =~ /\A(?:2|6)\z/u and $self->{"state"} ne "ready"
!ll&&!rl&&r
149062
MC/DC33%1/3
$job->{"op"} =~ /\A(?:2|6)\z/u and $self->{"state"} ne "ready" and not $self->{"closed"}
$job->{"op"} =~ /\A(?:2|6)\z/u $self->{"state"} ne "ready" not $self->{"closed"}
Truth table50%2/4
A and B and not(C)
A= $job->{"op"} =~ /\A(?:2|6)\z/uB= $self->{"state"} ne "ready"C= not $self->{"closed"}
ABCresult
0XX0
10X0
1100
1111
517 $self->_timer(
518 handshake => $self->{connect_timeout},
519 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
520 0 $self->_reconnect( 0, 'Gateway authentication timed out', 'timeout' );
521 }
522 62 );
523 }
524 211 $job->{write_future} = $write_future;
525 211 ▶ $write_future->on_ready( sub { $self->_complete_write( $job, $write_future, $generation ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
526 211 return 1;
527 }
528  
529 211 ▶sub _complete_write {
Subroutine _complete_write: called CC=6 SCAR=18.8
530 211 my ( $self, $job, $write_future, $generation ) = @_;
531 211 ▶ return if $generation != $self->{generation} || $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $generation != $self->{"generation"} or $self->{"closed"} 1 210 211
Condition66%2/3
$generation != $self->{"generation"} or $self->{"closed"}
l!l&&r!l&&!r
10210
MC/DC50%1/2
$generation != $self->{"generation"} or $self->{"closed"}
$generation != $self->{"generation"} $self->{"closed"}
Truth table66%2/3
A or B
A= $generation != $self->{"generation"}B= $self->{"closed"}
ABresult
1X1
011
000
532  
533 # Release the active slot before notifying callers that can enqueue more writes.
534 210 delete $self->{active_write};
535 210 ▶ if ( !$job->{future}->is_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($job->{"future"}->is_ready) 210 0 210
536 210 ▶ if ( $write_future->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($write_future->is_done) { } 210 0 210
537 210 $job->{future}->done;
538 }
539 else {
540 0 $self->_fail( $job->{future}, 'Gateway write failed', 'transport' );
541 }
542 }
543 210 ▶ $self->_reconnect( 0, 'Gateway write failed', 'transport' ) unless $write_future->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $write_future->is_done 0 210 210
544 210 $self->_drain_writes;
545 }
546  
547 7 ▶sub _closed {
Subroutine _closed: called CC=5 SCAR=16.8
548 7 my ( $self, $code, $generation ) = @_;
549 7 ▶ return if $self->{closed} || $generation != $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $generation != $self->{"generation"} 0 7 7
Condition33%1/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
550  
551 # Fatal Discord close codes terminate the lifetime; recoverable closes reconnect.
552 7 my $actions = $self->{session}->closed($code);
553 7 ▶ if ( @$actions && $actions->[0]{action} eq 'fatal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$actions and $actions->[0]{'action'} eq 'fatal') { } 2 5 7
Condition66%2/3
@$actions and $actions->[0]{"action"} eq "fatal"
!ll&&!rl&&r
052
MC/DC50%1/2
@$actions and $actions->[0]{"action"} eq "fatal"
@$actions $actions->[0]{"action"} eq "fatal"
Truth table66%2/3
A and B
A= @$actionsB= $actions->[0]{"action"} eq "fatal"
ABresult
0X0
100
111
554 2 $self->_fatal( 'Discord rejected the Gateway session', 'gateway_close', close_code => $code );
555 }
556 else {
557 5 $self->_reconnect( 0, 'Gateway connection closed', 'transport' );
558 }
559 }
560  
561 14 ▶sub _reconnect {
Subroutine _reconnect: called CC=14 SCAR=30.6
562 14 my ( $self, $minimum, $message, $category, $immediate ) = @_;
563 14 ▶ return if $self->{closed} || $self->{state} eq 'reconnecting';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"state"} eq "reconnecting" 0 14 14
Condition33%1/3
$self->{"closed"} or $self->{"state"} eq "reconnecting"
l!l&&r!l&&!r
0014
MC/DC0%0/2
$self->{"closed"} or $self->{"state"} eq "reconnecting"
$self->{"closed"} $self->{"state"} eq "reconnecting"
Truth table33%1/3
A or B
A= $self->{"closed"}B= $self->{"state"} eq "reconnecting"
ABresult
1X1
011
000
564  
565 # Notify the caller before teardown, allowing the error hook to request shutdown.
566 14 $self->_report( $message, $category );
567 14 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 14 14
568 14 $self->_teardown;
569 14 $self->{session}->closed(1006);
570 14 $self->{state} = 'reconnecting';
571  
572 # Bound exponential backoff and jitter; explicit server reconnects may be immediate.
573 14 my $attempt = $self->{attempts}++;
574 14 ▶ $attempt = 16 if $attempt > 16;
Branch: 1 branch
BranchTrueFalseTotal
if $attempt > 16 0 14 14
575 14 my $base = $self->{backoff_base} * ( 2**$attempt );
576 14 ▶ $base = $self->{backoff_max} if $base > $self->{backoff_max};
Branch: 1 branch
BranchTrueFalseTotal
if $base > $self->{"backoff_max"} 0 14 14
577 14 my $random = $self->{random}->();
578 14 ▶ $random = 0.5
Branch: 1 branch
BranchTrueFalseTotal
unless defined $random and not ref $random and $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u and $random >= 0 and $random < 1 0 14 14
Condition33%4/12
defined $random and not ref $random and $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u and $random >= 0 and $random < 1
!ll&&!rl&&r
0014
defined $random and not ref $random and $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u and $random >= 0
!ll&&!rl&&r
0014
defined $random and not ref $random and $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u
!ll&&!rl&&r
0014
defined $random and not ref $random
!ll&&!rl&&r
0014
MC/DC0%0/5
defined $random and not ref $random and $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u and $random >= 0 and $random < 1
defined $random not ref $random $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/u $random >= 0 $random < 1
Truth table16%1/6
A and not(B) and C and D and E
A= defined $randomB= not ref $randomC= $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/uD= $random >= 0E= $random < 1
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
579 unless defined($random)
580 && !ref($random)
581 && $random =~ /\A(?:\d+(?:\.\d*)?|\.\d+)\z/
582 && $random >= 0
583 && $random < 1;
584 14 my $after = $base * ( 0.5 + 0.5 * $random );
585 14 ▶ $after = 0 if $immediate && $attempt == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $immediate and $attempt == 0 2 12 14
Condition66%2/3
$immediate and $attempt == 0
!ll&&!rl&&r
1202
MC/DC50%1/2
$immediate and $attempt == 0
$immediate $attempt == 0
Truth table66%2/3
A and B
A= $immediateB= $attempt == 0
ABresult
0X0
100
111
586 14 ▶ $after = $minimum if $minimum > $after;
Branch: 1 branch
BranchTrueFalseTotal
if $minimum > $after 0 14 14
587 14 $self->_diagnostic( 'reconnect_scheduled', after => $after );
588 14 ▶ $self->_timer( reconnect => $after, sub { $self->_open } );
Subroutine __ANON__: called CC=1 SCAR=0.0
589 }
590  
591 120 ▶sub _dispatch {
Subroutine _dispatch: called CC=11 SCAR=24.0
592 120 my ( $self, $event, $data, $packet ) = @_;
593 120 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 120 120
594 120 my $received_at = $self->{clock}->();
595  
596 # Cache updates and interaction admission must survive application overload.
597 120 for my $observer ( @{ $self->{dispatch_observers} } ) {
598 70 my $ok = eval {
599 70 $observer->( $event, $data, $self, $packet, $received_at );
600 70 1;
601 };
602 70 ▶ $self->_report( 'Gateway dispatch observer failed', 'handler', event => $event ) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 70 70
603 70 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 70 70
604 }
605  
606 # Queue each application listener separately after SDK observers have run.
607 120 my @jobs;
608 120 ▶ for my $callback ( @{ $self->{handlers}{$event} // [] } ) {
Condition100%2/2
$self->{"handlers"}{$event} // []
l!l
2397
MC/DC100%1/1
$self->{"handlers"}{$event} // []
$self->{"handlers"}{$event}
Truth table100%2/2
A
A= $self->{"handlers"}{$event}
Aresult
11
00
609 28 push @jobs,
610 {
611 callback => $callback,
612 args => [ $data, $self, $packet, $received_at ],
613 event => $event,
614 };
615 }
616 120 ▶ for my $callback ( @{ $self->{handlers}{'*'} // [] } ) {
Condition100%2/2
$self->{"handlers"}{"*"} // []
l!l
7446
MC/DC100%1/1
$self->{"handlers"}{"*"} // []
$self->{"handlers"}{"*"}
Truth table100%2/2
A
A= $self->{"handlers"}{"*"}
Aresult
11
00
617 74 push @jobs,
618 {
619 callback => $callback,
620 args => [ $event, $data, $self, $packet, $received_at ],
621 event => $event,
622 };
623 }
624  
625 # Drop the whole application event on overflow and report once per saturated queue.
626 120 ▶ if ( @{ $self->{event_queue} } + @jobs > $self->{max_events} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"event_queue"};} + @jobs > $self->{"max_events"}) 11 109 120
627 11 ++$self->{counters}{dropped_events};
628 11 $self->{counters}{dropped_callbacks} += @jobs;
629 11 ▶ unless ( $self->{event_overflow_reported} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"event_overflow_reported"}) 7 4 11
630 7 $self->{event_overflow_reported} = 1;
631 7 $self->_report( 'Gateway event queue capacity exceeded; event dropped', 'queue_full' );
632 }
633 11 return;
634 }
635 109 push @{ $self->{event_queue} }, @jobs;
636 109 $self->_schedule_handlers;
637 }
638  
639 120 ▶sub _schedule_handlers {
Subroutine _schedule_handlers: called CC=4 SCAR=13.9
640 120 my ($self) = @_;
641 120 ▶ return if $self->{handler_scheduled} || $self->{closed} || !@{ $self->{event_queue} };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handler_scheduled"} or $self->{"closed"} or not @{$self->{"event_queue"};} 42 78 120
Condition100%6/6
$self->{"handler_scheduled"} or $self->{"closed"} or not @{$self->{"event_queue"};}
l!l&&r!l&&!r
152778
$self->{"handler_scheduled"} or $self->{"closed"}
l!l&&r!l&&!r
141105
MC/DC100%3/3
$self->{"handler_scheduled"} or $self->{"closed"} or not @{$self->{"event_queue"};}
$self->{"handler_scheduled"} $self->{"closed"} not @{$self->{"event_queue"};}
Truth table100%4/4
A or B or not(C)
A= $self->{"handler_scheduled"}B= $self->{"closed"}C= not @{$self->{"event_queue"};}
ABCresult
1XX1
01X1
0011
0000
642  
643 # Defer application callbacks so packet processing can finish first.
644 78 $self->{handler_scheduled} = 1;
645 $self->{defer}->(
646 74 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
647 74 delete $self->{handler_scheduled};
648 74 $self->_drain_handlers;
649 }
650 78 );
651 }
652  
653 74 ▶sub _drain_handlers {
Subroutine _drain_handlers: called CC=15 SCAR=27.3
654 74 my ($self) = @_;
655 74 ▶ return if $self->{closed} || $self->{draining_handlers};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} or $self->{"draining_handlers"} 0 74 74
Condition33%1/3
$self->{"closed"} or $self->{"draining_handlers"}
l!l&&r!l&&!r
0074
MC/DC0%0/2
$self->{"closed"} or $self->{"draining_handlers"}
$self->{"closed"} $self->{"draining_handlers"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $self->{"draining_handlers"}
ABresult
1X1
011
000
656  
657 # Admit callbacks up to the pending-Future limit without recursive draining.
658 74 local $self->{draining_handlers} = 1;
659 74 ▶ while ( @{ $self->{event_queue} } && keys( %{ $self->{handler_futures} } ) < $self->{max_handlers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"event_queue"};} and keys %{$$self{"handler_futures"};} < $self->{"max_handlers"}) 84 69 153
Condition100%3/3
@{$self->{"event_queue"};} and keys %{$$self{"handler_futures"};} < $self->{"max_handlers"}
!ll&&!rl&&r
65484
MC/DC100%2/2
@{$self->{"event_queue"};} and keys %{$$self{"handler_futures"};} < $self->{"max_handlers"}
@{$self->{"event_queue"};} keys %{$$self{"handler_futures"};} < $self->{"max_handlers"}
Truth table100%3/3
A and B
A= @{$self->{"event_queue"};}B= keys %{$$self{"handler_futures"};} < $self->{"max_handlers"}
ABresult
0X0
100
111
660 84 my $job = shift @{ $self->{event_queue} };
661 84 my $result = eval { $job->{callback}->( @{ $job->{args} } ) };
662 84 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 82 84
663 2 $self->_report( 'Gateway event handler failed', 'handler', event => $job->{event} );
664 2 next;
665 }
666 82 ▶ if ( $self->{closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"closed"}) 5 77 82
667  
668 # Work returned by a closing handler has not yet entered our registry.
669 # Preserve close itself so its transport handshake can finish.
670 $result->cancel
671 if blessed($result)
672 && $result->isa('Future')
673 && !$result->is_ready
674 5 ▶ && ( !$self->{close_future} || $result != $self->{close_future} );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Future") and not $result->is_ready and !$self->{"close_future"} || $result != $self->{"close_future"} 1 4 5
Condition75%9/12
builtin::blessed($result) and $result->isa("Future") and not $result->is_ready and !$self->{"close_future"} || $result != $self->{"close_future"}
!ll&&!rl&&r
311
builtin::blessed($result) and $result->isa("Future") and not $result->is_ready
!ll&&!rl&&r
302
builtin::blessed($result) and $result->isa("Future")
!ll&&!rl&&r
302
not $self->{"close_future"} or $result != $self->{"close_future"}
l!l&&r!l&&!r
011
MC/DC40%2/5
builtin::blessed($result) and $result->isa("Future") and not $result->is_ready and !$self->{"close_future"} || $result != $self->{"close_future"}
builtin::blessed($result) $result->isa("Future") not $result->is_ready not $self->{"close_future"} $result != $self->{"close_future"}
Truth table50%3/6
A and B and not(C) and not(D) or E
A= builtin::blessed($result)B= $result->isa("Future")C= not $result->is_readyD= not $self->{"close_future"}E= $result != $self->{"close_future"}
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
675 5 last;
676 }
677  
678 # Keep handler work alive until settled and resume queued callbacks afterward.
679 77 ▶ if ( blessed($result) && $result->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->isa("Future")) 11 66 77
Condition66%2/3
builtin::blessed($result) and $result->isa("Future")
!ll&&!rl&&r
66011
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Future")
builtin::blessed($result) $result->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Future")
ABresult
0X0
100
111
680 11 my $id = refaddr($job);
681 11 $self->{handler_futures}{$id} = $result;
682 $result->on_ready(
683 11 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
684 11 delete $self->{handler_futures}{$id};
685 11 ▶ if ( !$result->is_done && !$self->{closed} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result->is_done or $self->{"closed"}) 1 10 11
Condition100%3/3
$result->is_done or $self->{"closed"}
l!l&&r!l&&!r
821
MC/DC100%2/2
$result->is_done or $self->{"closed"}
$result->is_done $self->{"closed"}
Truth table100%3/3
A or B
A= $result->is_doneB= $self->{"closed"}
ABresult
1X1
011
000
686 1 $self->_report( 'Gateway event handler failed', 'handler', event => $job->{event} );
687 }
688 11 $self->_schedule_handlers;
689 }
690 11 );
691 }
692 }
693 74 ▶ delete $self->{event_overflow_reported} unless @{ $self->{event_queue} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"event_queue"};} 70 4 74
694 }
695  
696 241 ▶sub _timer {
Subroutine _timer: called CC=1 SCAR=0.1
697 241 my ( $self, $name, $after, $callback ) = @_;
698  
699 # Only one timer per purpose may act on the current connection generation.
700 241 $self->_cancel_timer($name);
701 241 my $generation = $self->{generation};
702 241 my $timer = $self->{delay}->($after);
703 241 $self->{timers}{$name} = $timer;
704 $timer->on_done(
705 38 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
706 38 delete $self->{timers}{$name};
707 38 ▶ $callback->() unless $self->{closed} || $generation != $self->{generation};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} or $generation != $self->{"generation"} 38 0 38
Condition33%1/3
$self->{"closed"} or $generation != $self->{"generation"}
l!l&&r!l&&!r
0038
MC/DC0%0/2
$self->{"closed"} or $generation != $self->{"generation"}
$self->{"closed"} $generation != $self->{"generation"}
Truth table33%1/3
A or B
A= $self->{"closed"}B= $generation != $self->{"generation"}
ABresult
1X1
011
000
708 }
709 241 );
710 }
711  
712 646 ▶sub _cancel_timer {
Subroutine _cancel_timer: called CC=3 SCAR=11.1
713 646 my ( $self, $name ) = @_;
714  
715 # Detach the timer before cancellation can invoke callbacks that re-enter the driver.
716 646 ▶ if ( my $timer = delete $self->{timers}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timer = delete $self->{"timers"}{$name}) 203 443 646
717 203 ▶ $timer->cancel unless $timer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $timer->is_ready 203 0 203
718 }
719 }
720  
721 71 ▶sub _teardown {
Subroutine _teardown: called CC=12 SCAR=25.3
722 71 my ($self) = @_;
723  
724 # Invalidate old callbacks before cancelling timers and in-flight operations.
725 71 ++$self->{generation};
726 71 $self->_cancel_timer($_) for keys %{ $self->{timers} };
727 71 for my $name (qw(connection_future identify_future discovery)) {
728 213 ▶ if ( my $future = delete $self->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $future = delete $self->{$name}) 4 209 213
729 4 ▶ $future->cancel unless $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $future->is_ready 4 0 4
730 }
731 }
732  
733 # Detach queued writes before failing them; completion hooks may re-enter the driver.
734 71 my @writes = @{ $self->{outgoing} };
735 71 $self->{outgoing} = [];
736 71 ▶ push @writes, delete( $self->{active_write} ) if $self->{active_write};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_write"} 1 70 71
737 71 for my $job (@writes) {
738 1 ▶ $job->{permit}->cancel if $job->{permit};
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"permit"} 0 1 1
739 1 ▶ $job->{write_future}->cancel if $job->{write_future} && !$job->{write_future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"write_future"} and not $job->{"write_future"}->is_ready 1 0 1
Condition33%1/3
$job->{"write_future"} and not $job->{"write_future"}->is_ready
!ll&&!rl&&r
001
MC/DC0%0/2
$job->{"write_future"} and not $job->{"write_future"}->is_ready
$job->{"write_future"} not $job->{"write_future"}->is_ready
Truth table33%1/3
A and not(B)
A= $job->{"write_future"}B= not $job->{"write_future"}->is_ready
ABresult
0X0
100
111
740 $self->_fail( $job->{future}, 'Gateway disconnected before write completed', 'transport' )
741 1 ▶ unless $job->{future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"future"}->is_ready 1 0 1
742 }
743  
744 # Connection-local timing and quota must not leak into the replacement socket.
745 71 delete @{$self}{qw(heartbeat_interval heartbeat_sent_at)};
746 71 $self->{send_times} = [];
747 71 my $websocket = delete $self->{websocket};
748 71 ▶ $websocket->abort if $websocket;
Branch: 1 branch
BranchTrueFalseTotal
if $websocket 14 57 71
749 }
750  
751 9 ▶sub _fail {
Subroutine _fail: called CC=1 SCAR=0.0
752 9 my ( $self, $future, $message, $category, %fields ) = @_;
753 9 my $error = Discord::Client::Error->new( message => $message, category => $category, %fields );
754 9 return $future->fail( $message, $category, $error );
755 }
756  
757 1 ▶sub _failed {
Subroutine _failed: called CC=1 SCAR=0.0
758 1 my ( $self, @args ) = @_;
759 1 return $self->_fail( $self->{loop}->new_future, @args );
760 }
761  
762 26 ▶sub _report {
Subroutine _report: called CC=3 SCAR=11.3
763 26 my ( $self, $message, $category, %fields ) = @_;
764  
765 # Surface structured errors without forwarding raw exceptions or protocol payloads.
766 26 my $error = Discord::Client::Error->new( message => $message, category => $category, %fields );
767 26 ++$self->{counters}{errors};
768 26 ▶ if ( $self->{on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'on_error'}) { } 26 0 26
769 26 my $ok = eval {
770 26 $self->{on_error}->( $error, $self );
771 26 1;
772 };
773 26 ▶ warn "Gateway error handler failed\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 26 26
774 }
775 else {
776 0 warn "$message\n";
777 }
778 26 return $error;
779 }
780  
781 177 ▶sub _diagnostic {
Subroutine _diagnostic: called CC=3 SCAR=11.0
782 177 my ( $self, $type, %fields ) = @_;
783  
784 # Diagnostics are optional observations and cannot interrupt protocol processing.
785 177 ++$self->{counters}{$type};
786 177 ▶ return unless $self->{on_diagnostic};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"on_diagnostic"} 42 135 177
787 135 my $ok = eval {
788 135 $self->{on_diagnostic}->( { type => $type, %fields, state => $self->{state} } );
789 135 1;
790 };
791 135 ▶ warn "Gateway diagnostic handler failed\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 135 135
792 }
793  
794 2 ▶sub _fatal {
Subroutine _fatal: called CC=5 SCAR=16.4
795 2 my ( $self, $message, $category, %fields ) = @_;
796 2 ▶ return if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 2 2
797  
798 # Fail startup and lifetime before closing so fatal failures remain observable.
799 2 my $error = $self->_report( $message, $category, %fields );
800 2 $self->{fatal_error} = $error;
801 $self->_fail( $self->{ready_future}, $message, $category, %fields )
802 2 ▶ if $self->{ready_future} && !$self->{ready_future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ready_future"} and not $self->{"ready_future"}->is_ready 1 1 2
Condition66%2/3
$self->{"ready_future"} and not $self->{"ready_future"}->is_ready
!ll&&!rl&&r
101
MC/DC50%1/2
$self->{"ready_future"} and not $self->{"ready_future"}->is_ready
$self->{"ready_future"} not $self->{"ready_future"}->is_ready
Truth table66%2/3
A and not(B)
A= $self->{"ready_future"}B= not $self->{"ready_future"}->is_ready
ABresult
0X0
100
111
803 2 ▶ $self->_fail( $self->{completion}, $message, $category, %fields ) unless $self->{completion}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"completion"}->is_ready 2 0 2
804 2 $self->close;
805 }
806  
807 59 ▶sub close {
Subroutine close: called CC=13 SCAR=29.7
Pod documented
808 59 my ($self) = @_;
809 59 ▶ return $self->{close_future} if $self->{close_future};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"close_future"} 2 57 59
810  
811 # Publish the shutdown Future before cancelling anything that can re-enter close.
812 57 my $result = $self->{loop}->new_future;
813 57 $self->{close_future} = $result;
814 57 $self->{closed} = 1;
815 57 $self->{state} = 'closed';
816  
817 # Detach the live socket so teardown cannot abort the graceful close handshake.
818 57 my $websocket = delete $self->{websocket};
819 57 $self->_teardown;
820 57 $self->{session}->stop;
821 $self->_fail( $self->{ready_future}, 'Gateway closed before READY', 'shutdown' )
822 57 ▶ if $self->{ready_future} && !$self->{ready_future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ready_future"} and not $self->{"ready_future"}->is_ready 2 55 57
Condition100%3/3
$self->{"ready_future"} and not $self->{"ready_future"}->is_ready
!ll&&!rl&&r
5412
MC/DC100%2/2
$self->{"ready_future"} and not $self->{"ready_future"}->is_ready
$self->{"ready_future"} not $self->{"ready_future"}->is_ready
Truth table100%3/3
A and not(B)
A= $self->{"ready_future"}B= not $self->{"ready_future"}->is_ready
ABresult
0X0
100
111
823  
824 # Drop queued notifications and cancel pending handlers before releasing owned resources.
825 57 $self->{event_queue} = [];
826 57 $self->{dispatch_observers} = [];
827 57 my @handlers = values %{ $self->{handler_futures} };
828 57 $self->{handler_futures} = {};
829 57 for my $future (@handlers) {
830 2 ▶ $future->cancel unless $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $future->is_ready 2 0 2
831 }
832 57 ▶ $self->{identify_limiter}->close if $self->{own_limiter};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"own_limiter"} 43 14 57
833 57 ▶ $self->{rest}->close if $self->{own_rest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"own_rest"} 0 57 57
834  
835 # Await transport shutdown, converting adapter exceptions into a sanitized failure.
836 57 ▶ my $closing = $websocket ? eval { $websocket->close(1000) } : $self->{loop}->new_future->done;
Branch: 1 branch
BranchTrueFalseTotal
$websocket ? : 53 4 57
837  
838 57 ▶ if ( !blessed($closing) || !$closing->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($closing) and $closing->isa("Future")) 0 57 57
Condition33%1/3
builtin::blessed($closing) and $closing->isa("Future")
!ll&&!rl&&r
0057
MC/DC0%0/2
builtin::blessed($closing) and $closing->isa("Future")
builtin::blessed($closing) $closing->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($closing)B= $closing->isa("Future")
ABresult
0X0
100
111
839 0 ▶ $websocket->abort if $websocket;
Branch: 1 branch
BranchTrueFalseTotal
if $websocket 0 0 0
840 0 $self->_fail( $result, 'Gateway shutdown failed', 'transport' );
841 0 ▶ $self->{completion}->done unless $self->{completion}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"completion"}->is_ready 0 0 0
842 0 return $result;
843 }
844 $closing->on_ready(
845 57 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
846 57 ▶ $self->{completion}->done unless $self->{completion}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"completion"}->is_ready 55 2 57
847 57 ▶ return if $result->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_ready 0 57 57
848 57 ▶ $closing->is_done ? $result->done : $self->_fail( $result, 'Gateway shutdown failed', 'transport' );
Branch: 1 branch
BranchTrueFalseTotal
$closing->is_done ? : 57 0 57
849 }
850 57 );
851  
852 # Explicit shutdown cancellation aborts the socket and ends the lifetime wait.
853 $result->on_cancel(
854 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
855 0 ▶ $closing->cancel unless $closing->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $closing->is_ready 0 0 0
856 0 ▶ $websocket->abort if $websocket;
Branch: 1 branch
BranchTrueFalseTotal
if $websocket 0 0 0
857 0 ▶ $self->{completion}->done unless $self->{completion}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"completion"}->is_ready 0 0 0
858 }
859 57 );
860 57 return $result;
861 }
862  
863 11 1;
864  
865 __END__
« blib/lib/Discord/Client/REST.pm ↑ Summary blib/lib/Discord/Client/Gateway/Session.pm »