blib/lib/Discord/Client/Interactions.pm

filter: Statementstmt 97.0% 167 / 172 Branchbran 79.5% 70 / 88 Conditioncond 74.0% 60 / 81 MC/DCmcdc 53.1% 25 / 47 Subroutinesub 100% 26 / 26 Podpod 100% 4 / 4 totaltotal 84.2% 352 / 418 CC 78 SCAR 25.2
CC
78
Cov
87%
handle
21.8
new
20.5
_check_job
16.0
CRAP 12.4
« blib/lib/Discord/Client/Interaction/Verifier.pm ↑ Summary blib/lib/Discord/Client/REST/RateLimiter.pm »
1 package Discord::Client::Interactions;
2  
3 9 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Discord::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Discord::Client::Interaction;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 our $VERSION = '0.003';
14  
15 52 ▶sub new {
Subroutine new: called CC=15 SCAR=30.2
Pod documented
16 52 my ( $class, %args ) = @_;
17  
18 # Validate bounded work and replay settings before accepting any payloads.
19 52 ▶ croak 'A command registry is required' unless $args{commands};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"commands"} 0 52 52
20 52 for my $key (qw(max_pending max_seen seen_ttl)) {
21 croak "$key must be a positive integer"
22 if exists( $args{$key} )
23 156 ▶ && ( !defined( $args{$key} ) || ref( $args{$key} ) || $args{$key} !~ /\A[1-9]\d*\z/ );
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} and !defined($args{$key}) || ref $args{$key} || !($args{$key} =~ /\A[1-9]\d*\z/u) 0 156 156
Condition44%4/9
exists $args{$key} and !defined($args{$key}) || ref $args{$key} || !($args{$key} =~ /\A[1-9]\d*\z/u)
!ll&&!rl&&r
96600
not defined $args{$key} or ref $args{$key} or not $args{$key} =~ /\A[1-9]\d*\z/u
l!l&&r!l&&!r
0060
not defined $args{$key} or ref $args{$key}
l!l&&r!l&&!r
0060
MC/DC0%0/4
exists $args{$key} and !defined($args{$key}) || ref $args{$key} || !($args{$key} =~ /\A[1-9]\d*\z/u)
exists $args{$key} not defined $args{$key} ref $args{$key} not $args{$key} =~ /\A[1-9]\d*\z/u
Truth table40%2/5
A and not(B) or C or not(D)
A= exists $args{$key}B= not defined $args{$key}C= ref $args{$key}D= not $args{$key} =~ /\A[1-9]\d*\z/u
ABCDresult
0XXX0
10000
11XX1
101X1
10011
24 }
25 52 for my $key (qw(clock delay on_error)) {
26 156 ▶ croak "$key must be a code reference" if exists( $args{$key} ) && ref( $args{$key} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$key} and ref $args{$key} ne "CODE" 0 156 156
Condition66%2/3
exists $args{$key} and ref $args{$key} ne "CODE"
!ll&&!rl&&r
311250
MC/DC0%0/2
exists $args{$key} and ref $args{$key} ne "CODE"
exists $args{$key} ref $args{$key} ne "CODE"
Truth table66%2/3
A and B
A= exists $args{$key}B= ref $args{$key} ne "CODE"
ABresult
0X0
100
111
27 }
28  
29 # Keep active work separate from retained IDs used to suppress duplicate effects.
30 return bless {
31 loop => $args{loop} // IO::Async::Loop->new,
32 commands => $args{commands},
33 rest => $args{rest},
34 6 ▶ clock => $args{clock} // sub { clock_gettime(CLOCK_MONOTONIC) },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 delay => $args{delay},
36 max_pending => $args{max_pending} // 512,
37 max_seen => $args{max_seen} // 10000,
38 seen_ttl => $args{seen_ttl} // 900,
39 on_error => $args{on_error},
40 52 ▶ seen => {},
Condition83%10/12
$args{"loop"} // "IO::Async::Loop"->new
l!l&&r!l&&!r
4930
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
l!l&&r!l&&!r
37150
$args{"max_pending"} // 512
l!l
2032
$args{"max_seen"} // 10000
l!l
2131
$args{"seen_ttl"} // 900
l!l
1933
MC/DC0%0/2
$args{"loop"} // "IO::Async::Loop"->new
$args{"loop"} "IO::Async::Loop"->new
MC/DC0%0/2
$args{"clock"} // sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
$args{"clock"} sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
MC/DC100%1/1
$args{"max_pending"} // 512
$args{"max_pending"}
MC/DC100%1/1
$args{"max_seen"} // 10000
$args{"max_seen"}
MC/DC100%1/1
$args{"seen_ttl"} // 900
$args{"seen_ttl"}
Truth table66%2/3
A // B
A= $args{"loop"}B= "IO::Async::Loop"->new
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $args{"clock"}B= sub { &clock_gettime(scalar CLOCK_MONOTONIC()); }
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{"max_pending"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_seen"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"seen_ttl"}
Aresult
11
00
41 jobs => {},
42 counters => {}
43 }, $class;
44 }
45  
46 11 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
47 11 my ( $self, $message, $category ) = @_;
48 11 return Discord::Client::Error->new( message => $message, category => $category );
49 }
50  
51 4 ▶sub _failed {
Subroutine _failed: called CC=1 SCAR=0.0
52 4 my ( $self, $message, $category ) = @_;
53 4 my $error = $self->_error( $message, $category );
54 4 return $self->{loop}->new_future->fail( $message, $category, $error );
55 }
56  
57 9 ▶sub _report {
Subroutine _report: called CC=3 SCAR=11.4
58 9 my ( $self, $error, $context ) = @_;
59  
60 # Application error hooks receive sanitized failures and cannot break dispatch.
61 9 ++$self->{counters}{errors};
62 9 ▶ if ( $self->{on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'on_error'}) { } 9 0 9
63 9 my $ok = eval {
64 9 $self->{on_error}->( $error, $context );
65 9 1;
66 };
67 9 ▶ warn "Interaction error handler failed\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 9 9
68 }
69 else {
70 0 warn $error->message . "\n";
71 }
72 }
73  
74 1085 ▶sub handle {
Subroutine handle: called CC=21 SCAR=30.8
Pod documented
75 1085 my ( $self, $payload, %args ) = @_;
76 1085 ▶ return $self->_failed( 'Interaction dispatcher is closed', 'shutdown' ) if $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"closed"} 0 1085 1085
77 1085 my $now = $self->{clock}->();
78  
79 # Reclaim completed replay records only when a retained deadline is due.
80 1085 $self->_expire_seen($now);
81 1085 ▶ my $id = ref($payload) eq 'HASH' ? $payload->{id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $payload eq 'HASH' ? : 1085 0 1085
82 1085 ▶ if ( defined($id) && !ref($id) && exists $self->{seen}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and not ref $id and exists $self->{"seen"}{$id}) 2 1083 1085
Condition50%3/6
defined $id and not ref $id and exists $self->{"seen"}{$id}
!ll&&!rl&&r
010832
defined $id and not ref $id
!ll&&!rl&&r
001085
MC/DC33%1/3
defined $id and not ref $id and exists $self->{"seen"}{$id}
defined $id not ref $id exists $self->{"seen"}{$id}
Truth table50%2/4
A and not(B) and C
A= defined $idB= not ref $idC= exists $self->{"seen"}{$id}
ABCresult
0XX0
10X0
1100
1111
83 2 ++$self->{counters}{duplicates};
84 2 ▶ return $args{acknowledgement_only} ? $self->{seen}{$id}{ack} : $self->{seen}{$id}{result};
Branch: 1 branch
BranchTrueFalseTotal
$args{'acknowledgement_only'} ? : 1 1 2
85 }
86  
87 # Reject overload before constructing a context or invoking application code.
88 return $self->_failed( 'Interaction work capacity exceeded', 'queue_full' )
89 1083 ▶ if keys( %{ $self->{jobs} } ) >= $self->{max_pending} || keys( %{ $self->{seen} } ) >= $self->{max_seen};
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"jobs"};} >= $self->{"max_pending"} or keys %{$$self{"seen"};} >= $self->{"max_seen"} 4 1079 1083
Condition100%3/3
keys %{$$self{"jobs"};} >= $self->{"max_pending"} or keys %{$$self{"seen"};} >= $self->{"max_seen"}
l!l&&r!l&&!r
221079
MC/DC100%2/2
keys %{$$self{"jobs"};} >= $self->{"max_pending"} or keys %{$$self{"seen"};} >= $self->{"max_seen"}
keys %{$$self{"jobs"};} >= $self->{"max_pending"} keys %{$$self{"seen"};} >= $self->{"max_seen"}
Truth table100%3/3
A or B
A= keys %{$$self{"jobs"};} >= $self->{"max_pending"}B= keys %{$$self{"seen"};} >= $self->{"max_seen"}
ABresult
1X1
011
000
90  
91 # Use the same monotonic clock for receipt time, deadlines and replay retention.
92 my %context_options = (
93 payload => $payload,
94 loop => $self->{loop},
95 rest => $self->{rest},
96 clock => $self->{clock},
97 1079 );
98 1079 ▶ $context_options{delay} = $self->{delay} if $self->{delay};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"delay"} 1076 3 1079
99 1079 for my $name (qw(received_at initial_sender)) {
100 2158 ▶ $context_options{$name} = $args{$name} if exists $args{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$name} 2127 31 2158
101 }
102 1079 my $context = eval { Discord::Client::Interaction->new(%context_options) };
103 1079 ▶ return $self->_failed( 'Invalid interaction payload', 'protocol' ) if !$context || $@;
Branch: 1 branch
BranchTrueFalseTotal
if not $context or $@ 0 1079 1079
Condition33%1/3
not $context or $@
l!l&&r!l&&!r
001079
MC/DC0%0/2
not $context or $@
not $context $@
Truth table33%1/3
not(A) or B
A= not $contextB= $@
ABresult
1X1
011
000
104 1079 ▶ if ( $context->acknowledgement->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->acknowledgement->is_failed) 2 1077 1079
105 2 $self->_report( ( $context->acknowledgement->failure )[2], $context );
106 2 return $self->{loop}->new_future->fail( $context->acknowledgement->failure );
107 }
108  
109 # Reserve the ID and active slot before invoking handlers that can re-enter dispatch.
110 1077 my $result = $self->{loop}->new_future;
111 1077 my $job = { context => $context, result => $result };
112 1077 my $key = refaddr($job);
113 1077 $self->{jobs}{$key} = $job;
114 $self->{seen}{ $context->id } =
115 1077 { expires => $now + $self->{seen_ttl}, ack => $context->acknowledgement, result => $result };
116 1077 my $expires = $self->{seen}{ $context->id }{expires};
117 $self->{next_seen_expiry} = $expires
118 1077 ▶ if !defined( $self->{next_seen_expiry} ) || $expires < $self->{next_seen_expiry};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"next_seen_expiry"} or $expires < $self->{"next_seen_expiry"} 37 1040 1077
Condition66%2/3
not defined $self->{"next_seen_expiry"} or $expires < $self->{"next_seen_expiry"}
l!l&&r!l&&!r
3701040
MC/DC50%1/2
not defined $self->{"next_seen_expiry"} or $expires < $self->{"next_seen_expiry"}
not defined $self->{"next_seen_expiry"} $expires < $self->{"next_seen_expiry"}
Truth table66%2/3
not(A) or B
A= not defined $self->{"next_seen_expiry"}B= $expires < $self->{"next_seen_expiry"}
ABresult
1X1
011
000
119 1077 ++$self->{counters}{received};
120  
121 # Release work capacity on every terminal result; unanswered contexts also close.
122 $result->on_ready(
123 1077 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
124 1077 delete $self->{jobs}{$key};
125 1077 ▶ $context->close unless $context->acknowledgement->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $context->acknowledgement->is_done 6 1071 1077
126 }
127 1077 );
128 $result->on_cancel(
129 7 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
130 7 $context->close;
131 7 ▶ $job->{handler}->cancel if $job->{handler} && !$job->{handler}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $job->{"handler"} and not $job->{"handler"}->is_ready 3 4 7
Condition100%3/3
$job->{"handler"} and not $job->{"handler"}->is_ready
!ll&&!rl&&r
133
MC/DC100%2/2
$job->{"handler"} and not $job->{"handler"}->is_ready
$job->{"handler"} not $job->{"handler"}->is_ready
Truth table100%3/3
A and not(B)
A= $job->{"handler"}B= not $job->{"handler"}->is_ready
ABresult
0X0
100
111
132 }
133 1077 );
134 1077 $job->{handler} = $self->_invoke_handler($job);
135  
136 # Shutdown can run inside the handler before its Future is registered.
137 # Cancel that returned work even though the result has already settled.
138 1077 ▶ if ( $result->is_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_ready) 1 1076 1077
139 1 ▶ $job->{handler}->cancel unless $job->{handler}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"handler"}->is_ready 1 0 1
140 1 ▶ return $args{acknowledgement_only} ? $context->acknowledgement : $result;
Branch: 1 branch
BranchTrueFalseTotal
$args{'acknowledgement_only'} ? : 0 1 1
141 }
142  
143 1076 $self->_watch_job($job);
144 1076 ▶ return $args{acknowledgement_only} ? $context->acknowledgement : $result;
Branch: 1 branch
BranchTrueFalseTotal
$args{'acknowledgement_only'} ? : 24 1052 1076
145 }
146  
147 1085 ▶sub _expire_seen {
Subroutine _expire_seen: called CC=8 SCAR=20.9
148 1085 my ( $self, $now ) = @_;
149  
150 # Skip full-table scans until at least one retained ID can expire.
151 1085 ▶ return if defined( $self->{next_seen_expiry} ) && $now < $self->{next_seen_expiry};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"next_seen_expiry"} and $now < $self->{"next_seen_expiry"} 1043 42 1085
Condition100%3/3
defined $self->{"next_seen_expiry"} and $now < $self->{"next_seen_expiry"}
!ll&&!rl&&r
3391043
MC/DC100%2/2
defined $self->{"next_seen_expiry"} and $now < $self->{"next_seen_expiry"}
defined $self->{"next_seen_expiry"} $now < $self->{"next_seen_expiry"}
Truth table100%3/3
A and B
A= defined $self->{"next_seen_expiry"}B= $now < $self->{"next_seen_expiry"}
ABresult
0X0
100
111
152 42 delete $self->{next_seen_expiry};
153  
154 # A replay window must not expire an ID while its handler can still act.
155 42 for my $id ( keys %{ $self->{seen} } ) {
156 13 my $record = $self->{seen}{$id};
157 13 ▶ if ( $record->{expires} <= $now && $record->{result}->is_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"expires"} <= $now and $record->{"result"}->is_ready) 9 4 13
Condition100%3/3
$record->{"expires"} <= $now and $record->{"result"}->is_ready
!ll&&!rl&&r
229
MC/DC100%2/2
$record->{"expires"} <= $now and $record->{"result"}->is_ready
$record->{"expires"} <= $now $record->{"result"}->is_ready
Truth table100%3/3
A and B
A= $record->{"expires"} <= $nowB= $record->{"result"}->is_ready
ABresult
0X0
100
111
158 9 delete $self->{seen}{$id};
159 9 next;
160 }
161  
162 # Recheck expired running work on the next request, even before later deadlines.
163 $self->{next_seen_expiry} = $record->{expires}
164 4 ▶ if !defined( $self->{next_seen_expiry} ) || $record->{expires} < $self->{next_seen_expiry};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"next_seen_expiry"} or $record->{"expires"} < $self->{"next_seen_expiry"} 4 0 4
Condition33%1/3
not defined $self->{"next_seen_expiry"} or $record->{"expires"} < $self->{"next_seen_expiry"}
l!l&&r!l&&!r
400
MC/DC0%0/2
not defined $self->{"next_seen_expiry"} or $record->{"expires"} < $self->{"next_seen_expiry"}
not defined $self->{"next_seen_expiry"} $record->{"expires"} < $self->{"next_seen_expiry"}
Truth table33%1/3
not(A) or B
A= not defined $self->{"next_seen_expiry"}B= $record->{"expires"} < $self->{"next_seen_expiry"}
ABresult
1X1
011
000
165 }
166 }
167  
168 1077 ▶sub _invoke_handler {
Subroutine _invoke_handler: called CC=10 SCAR=24.6
169 1077 my ( $self, $job ) = @_;
170 1077 my $context = $job->{context};
171  
172 # Resolve the local declaration before calling application code.
173 1077 my $handler = eval { $self->{commands}->handler_for($context) };
174 1077 ▶ if ( !$handler || $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $handler or $@) 0 1077 1077
Condition33%1/3
not $handler or $@
l!l&&r!l&&!r
001077
MC/DC0%0/2
not $handler or $@
not $handler $@
Truth table33%1/3
not(A) or B
A= not $handlerB= $@
ABresult
1X1
011
000
175 0 ▶ my $message = $@ ? 'Invalid interaction command options' : 'No handler for this interaction';
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
176 0 $self->_record_job_error( $job, $message, 'command' );
177 0 return $self->{loop}->new_future->done;
178 }
179  
180 # Capture synchronous failures; asynchronous work is observed by the job watcher.
181 1077 my $work = eval { $handler->($context) };
182 1077 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 1076 1077
183 1 $self->_record_job_error( $job, 'Interaction handler failed', 'handler' );
184 1 return $self->{loop}->new_future->done;
185 }
186  
187 # Autocomplete handlers may return choices directly instead of sending a response.
188 1076 ▶ if ( $context->type == 4 && ref($work) eq 'ARRAY' && !$context->responded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->type == 4 and ref $work eq "ARRAY" and not $context->responded) 1 1075 1076
Condition83%5/6
$context->type == 4 and ref $work eq "ARRAY" and not $context->responded
!ll&&!rl&&r
107501
$context->type == 4 and ref $work eq "ARRAY"
!ll&&!rl&&r
107411
MC/DC66%2/3
$context->type == 4 and ref $work eq "ARRAY" and not $context->responded
$context->type == 4 ref $work eq "ARRAY" not $context->responded
Truth table75%3/4
A and B and not(C)
A= $context->type == 4B= ref $work eq "ARRAY"C= not $context->responded
ABCresult
0XX0
10X0
1100
1111
189 1 return $context->autocomplete($work);
190 }
191 1075 ▶ return $work if blessed($work) && $work->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($work) and $work->isa("Future") 1073 2 1075
Condition66%2/3
builtin::blessed($work) and $work->isa("Future")
!ll&&!rl&&r
201073
MC/DC50%1/2
builtin::blessed($work) and $work->isa("Future")
builtin::blessed($work) $work->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($work)B= $work->isa("Future")
ABresult
0X0
100
111
192 2 return $self->{loop}->new_future->done;
193 }
194  
195 7 ▶sub _record_job_error {
Subroutine _record_job_error: called CC=2 SCAR=6.9
196 7 my ( $self, $job, $message, $category ) = @_;
197 7 $job->{error} = $self->_error( $message, $category );
198 7 ▶ $self->_report( $job->{error}, $job->{context} ) unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 5 2 7
199 }
200  
201 1076 ▶sub _watch_job {
Subroutine _watch_job: called CC=1 SCAR=0.0
202 1076 my ( $self, $job ) = @_;
203 1076 my $context = $job->{context};
204 1076 my $work = $job->{handler};
205  
206 # Recheck both completion paths because acknowledgement and handler work can settle in either order.
207 1076 ▶ my $check = sub { $self->_check_job($job) };
Subroutine __ANON__: called CC=1 SCAR=0.0
208 $work->on_ready(
209 1076 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
210 # Resolve asynchronously produced suggestions only if the handler has not already replied.
211 1076 ▶ if ( $context->type == 4 && $work->is_done && !$context->responded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->type == 4 and $work->is_done and not $context->responded) 1 1075 1076
Condition83%5/6
$context->type == 4 and $work->is_done and not $context->responded
!ll&&!rl&&r
107411
$context->type == 4 and $work->is_done
!ll&&!rl&&r
107402
MC/DC66%2/3
$context->type == 4 and $work->is_done and not $context->responded
$context->type == 4 $work->is_done not $context->responded
Truth table75%3/4
A and B and not(C)
A= $context->type == 4B= $work->is_doneC= not $context->responded
ABCresult
0XX0
10X0
1100
1111
212 1 my ($choices) = $work->result;
213 1 ▶ if ( ref($choices) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $choices eq "ARRAY") 1 0 1
214 1 my $response = $context->autocomplete($choices);
215 1 $response->on_ready($check);
216 }
217 }
218 1076 $check->();
219 }
220 1076 );
221 1076 $context->acknowledgement->on_ready($check);
222 1076 $check->();
223 }
224  
225 3229 ▶sub _check_job {
Subroutine _check_job: called CC=16 SCAR=27.7
226 3229 my ( $self, $job ) = @_;
227 3229 my $result = $job->{result};
228 3229 ▶ return if $result->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_ready 2100 1129 3229
229  
230 1129 my $context = $job->{context};
231 1129 my $work = $job->{handler};
232 1129 my $acknowledgement = $context->acknowledgement;
233  
234 # Record a handler failure once, allowing an error hook to close the dispatcher.
235 1129 ▶ if ( $work->is_ready && !$work->is_done && !$job->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($work->is_ready and not $work->is_done and not $job->{"error"}) 6 1123 1129
Condition100%6/6
$work->is_ready and not $work->is_done and not $job->{"error"}
!ll&&!rl&&r
112216
$work->is_ready and not $work->is_done
!ll&&!rl&&r
4410787
MC/DC100%3/3
$work->is_ready and not $work->is_done and not $job->{"error"}
$work->is_ready not $work->is_done not $job->{"error"}
Truth table100%4/4
A and not(B) and not(C)
A= $work->is_readyB= not $work->is_doneC= not $job->{"error"}
ABCresult
0XX0
10X0
1100
1111
236 6 $self->_record_job_error( $job, 'Interaction handler failed', 'handler' );
237 }
238 1129 ▶ return if $result->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_ready 1 1128 1129
239  
240 # Failed acknowledgement ends the job and cancels any work that can no longer reply.
241 1128 ▶ if ( $acknowledgement->is_ready && !$acknowledgement->is_done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acknowledgement->is_ready and not $acknowledgement->is_done) 6 1122 1128
Condition100%3/3
$acknowledgement->is_ready and not $acknowledgement->is_done
!ll&&!rl&&r
3410886
MC/DC100%2/2
$acknowledgement->is_ready and not $acknowledgement->is_done
$acknowledgement->is_ready not $acknowledgement->is_done
Truth table100%3/3
A and not(B)
A= $acknowledgement->is_readyB= not $acknowledgement->is_done
ABresult
0X0
100
111
242 6 my $error = $job->{error};
243 6 ▶ unless ($error) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 3 3 6
244 3 ▶ if ( $acknowledgement->is_failed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acknowledgement->is_failed) { } 3 0 3
245 3 $error = ( $acknowledgement->failure )[2];
246 }
247 else {
248 0 $error = $self->_error( 'Interaction acknowledgement cancelled', 'cancelled' );
249 }
250 3 ▶ $self->_report( $error, $context ) unless $self->{closed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"closed"} 2 1 3
251 }
252 6 ▶ $result->fail( $error->message, $error->category, $error ) unless $result->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $result->is_ready 6 0 6
253 6 ▶ $work->cancel unless $work->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $work->is_ready 1 5 6
254 6 return;
255 }
256  
257 # A reply may finish before the handler's deferred work. Both must settle
258 # before releasing the bounded active-handler slot.
259 1122 ▶ return unless $acknowledgement->is_done && $work->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $acknowledgement->is_done and $work->is_ready 52 1070 1122
Condition100%3/3
$acknowledgement->is_done and $work->is_ready
!ll&&!rl&&r
34181070
MC/DC100%2/2
$acknowledgement->is_done and $work->is_ready
$acknowledgement->is_done $work->is_ready
Truth table100%3/3
A and B
A= $acknowledgement->is_doneB= $work->is_ready
ABresult
0X0
100
111
260 1070 ▶ if ( my $error = $job->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $job->{'error'}) { } 3 1067 1070
261 3 $result->fail( $error->message, $error->category, $error );
262 }
263 else {
264 1067 ++$self->{counters}{handled};
265 1067 $result->done;
266 }
267 }
268  
269 33 ▶sub stats {
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
270 33 my ($self) = @_;
271 return {
272 33 %{ $self->{counters} },
273 33 pending => scalar( keys %{ $self->{jobs} } ),
274 33 replay_entries => scalar( keys %{ $self->{seen} } ),
275 };
276 }
277  
278 51 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
279 51 my ($self) = @_;
280  
281 # Stop admission before cancelling a snapshot of jobs that can remove themselves.
282 51 $self->{closed} = 1;
283 51 my @jobs = values %{ $self->{jobs} };
284 51 for my $job (@jobs) {
285 5 ▶ $job->{result}->cancel unless $job->{result}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $job->{"result"}->is_ready 5 0 5
286 5 $job->{context}->close;
287 }
288 51 $self->{seen} = {};
289 51 return $self->{loop}->new_future->done;
290 }
291  
292 9 1;
293  
294 __END__
« blib/lib/Discord/Client/Interaction/Verifier.pm ↑ Summary blib/lib/Discord/Client/REST/RateLimiter.pm »