JetStream.pm

filter: Statementstmt 13.7% 15 / 109 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 17.2% 5 / 29 Podpod 0.0% 0 / 20 totaltotal 8.4% 20 / 238 SCAR 67.1
CC
32
Cov
8%
fetch
156.0
_request
56.0
_status
30.0
CRAP 817.1
« Client.pm ↑ Summary Connection.pm »
1 package Net::NATS2::JetStream;
2  
3 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
12 0 return bless {@_}, $class;
13 }
14  
15 sub client {
16 0 ▶ my $self = shift;
Subroutine client: not called CC=2 SCAR=17.9
Pod undocumented
17 0 ▶ $self->{client} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
18 0 return $self->{client};
19 }
20  
21 sub timeout {
22 0 ▶ my $self = shift;
Subroutine timeout: not called CC=2 SCAR=17.9
Pod undocumented
23 0 ▶ $self->{timeout} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
24 0 return $self->{timeout};
25 }
26  
27 sub last_error {
28 0 ▶ my $self = shift;
Subroutine last_error: not called CC=2 SCAR=17.9
Pod undocumented
29 0 ▶ $self->{last_error} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
30 0 return $self->{last_error};
31 }
32  
33 sub api_info {
34 0 ▶ my ($self, $timeout) = @_;
Subroutine api_info: not called CC=1 SCAR=6.9
Pod undocumented
35 0 return $self->_request('$JS.API.INFO', '', $timeout);
36 }
37  
38 sub add_stream {
39 0 ▶ my ($self, $config, $timeout) = @_;
Subroutine add_stream: not called CC=1 SCAR=6.9
Pod undocumented
40 0 return $self->_request('$JS.API.STREAM.CREATE.' . $config->{name}, encode_json($config), $timeout);
41 }
42  
43 sub update_stream {
44 0 ▶ my ($self, $config, $timeout) = @_;
Subroutine update_stream: not called CC=1 SCAR=6.9
Pod undocumented
45 0 return $self->_request('$JS.API.STREAM.UPDATE.' . $config->{name}, encode_json($config), $timeout);
46 }
47  
48 sub stream_info {
49 0 ▶ my ($self, $name, $timeout) = @_;
Subroutine stream_info: not called CC=1 SCAR=6.9
Pod undocumented
50 0 return $self->_request('$JS.API.STREAM.INFO.' . $name, '', $timeout);
51 }
52  
53 sub stream_list {
54 0 ▶ my ($self, $offset, $timeout) = @_;
Subroutine stream_list: not called CC=2 SCAR=17.9
Pod undocumented
55 0 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 0 0
56 0 return $self->_request('$JS.API.STREAM.LIST', encode_json({offset => $offset}), $timeout);
57 }
58  
59 sub delete_stream {
60 0 ▶ my ($self, $name, $timeout) = @_;
Subroutine delete_stream: not called CC=1 SCAR=6.9
Pod undocumented
61 0 return $self->_request('$JS.API.STREAM.DELETE.' . $name, '', $timeout);
62 }
63  
64 sub purge_stream {
65 0 ▶ my ($self, $name, $timeout) = @_;
Subroutine purge_stream: not called CC=1 SCAR=6.9
Pod undocumented
66 0 return $self->_request('$JS.API.STREAM.PURGE.' . $name, '', $timeout);
67 }
68  
69 sub add_consumer {
70 0 ▶ my ($self, $stream, $config, $timeout) = @_;
Subroutine add_consumer: not called CC=3 SCAR=24.8
Pod undocumented
71 0 ▶ my $durable = $config->{durable_name} || $config->{name};
Condition0%0/3
$config->{'durable_name'} || $config->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{'durable_name'} || $config->{'name'}
$config->{'durable_name'} $config->{'name'}
Truth table0%0/3
A || B
A= $config->{'durable_name'}B= $config->{'name'}
ABresult
1X1
011
000
72 0 ▶ my $subject
Branch: 1 branch
BranchTrueFalseTotal
defined $durable ? : 0 0 0
73 = defined $durable
74 ? '$JS.API.CONSUMER.DURABLE.CREATE.' . $stream . '.' . $durable
75 : '$JS.API.CONSUMER.CREATE.' . $stream;
76 0 return $self->_request($subject, encode_json({stream_name => $stream, config => $config,}), $timeout);
77 }
78  
79 sub consumer_info {
80 0 ▶ my ($self, $stream, $consumer, $timeout) = @_;
Subroutine consumer_info: not called CC=1 SCAR=6.9
Pod undocumented
81 0 return $self->_request('$JS.API.CONSUMER.INFO.' . $stream . '.' . $consumer, '', $timeout);
82 }
83  
84 sub delete_consumer {
85 0 ▶ my ($self, $stream, $consumer, $timeout) = @_;
Subroutine delete_consumer: not called CC=1 SCAR=6.9
Pod undocumented
86 0 return $self->_request('$JS.API.CONSUMER.DELETE.' . $stream . '.' . $consumer, '', $timeout);
87 }
88  
89 sub publish {
90 0 ▶ my ($self, $subject, $data, $timeout) = @_;
Subroutine publish: not called CC=1 SCAR=6.9
Pod undocumented
91 0 return $self->_request($subject, $data, $timeout);
92 }
93  
94 sub fetch {
95 0 ▶ my ($self, $stream, $consumer, $batch, $timeout) = @_;
Subroutine fetch: not called CC=12 SCAR=50.5
Pod undocumented
96 0 ▶ $batch = 1 unless defined $batch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $batch 0 0 0
97 0 ▶ $timeout = $self->timeout unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
98 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
99 0 $self->last_error(undef);
100  
101 0 my @messages;
102 my $status;
103 0 my $inbox = $self->client->new_inbox;
104 my $subscription = $self->client->subscribe(
105 $inbox,
106 sub {
107 0 ▶ my ($message) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
108 0 ▶ $status = _status($message) || $status;
Condition0%0/3
_status($message) || $status
l!l&&r!l&&!r
000
MC/DC0%0/2
_status($message) || $status
_status($message) $status
Truth table0%0/3
A || B
A= _status($message)B= $status
ABresult
1X1
011
000
109 0 ▶ push @messages, $message unless _status($message);
Branch: 1 branch
BranchTrueFalseTotal
unless _status($message) 0 0 0
110 }
111 0 );
112  
113 0 my $request = encode_json({batch => $batch, expires => int($timeout * 1_000_000_000),});
114 0 my $subject = '$JS.API.CONSUMER.MSG.NEXT.' . $stream . '.' . $consumer;
115 0 ▶ unless ($self->client->publish($subject, $request, $inbox)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client->publish($subject, $request, $inbox)) 0 0 0
116 0 $self->client->unsubscribe($subscription);
117 0 $self->last_error({description => 'JetStream fetch publish failed'});
118 0 return;
119 }
120  
121 0 my $deadline = time + $timeout;
122 0 ▶ while (@messages < $batch && !$status) {
Branch: 1 branch
BranchTrueFalseTotal
if (@messages < $batch and not $status) 0 0 0
Condition0%0/3
@messages < $batch and not $status
!ll&&!rl&&r
000
MC/DC0%0/2
@messages < $batch and not $status
@messages < $batch not $status
Truth table0%0/3
A and not(B)
A= @messages < $batchB= not $status
ABresult
0X0
100
111
123 0 my $remaining = $deadline - time;
124 0 ▶ last if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 0 0
125 0 ▶ last unless $self->client->wait_for_op($remaining);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->client->wait_for_op($remaining) 0 0 0
126 }
127 0 $self->client->unsubscribe($subscription);
128  
129 0 ▶ $self->last_error($status) if $status && $status->{code} != 404 && $status->{code} != 408;
Branch: 1 branch
BranchTrueFalseTotal
if $status and $status->{"code"} != 404 and $status->{"code"} != 408 0 0 0
Condition0%0/6
$status and $status->{"code"} != 404 and $status->{"code"} != 408
!ll&&!rl&&r
000
$status and $status->{"code"} != 404
!ll&&!rl&&r
000
MC/DC0%0/3
$status and $status->{"code"} != 404 and $status->{"code"} != 408
$status $status->{"code"} != 404 $status->{"code"} != 408
Truth table0%0/4
A and B and C
A= $statusB= $status->{"code"} != 404C= $status->{"code"} != 408
ABCresult
0XX0
10X0
1100
1111
130 0 return \@messages;
131 }
132  
133 sub next_message {
134 0 ▶ my ($self, $stream, $consumer, $timeout) = @_;
Subroutine next_message: not called CC=2 SCAR=17.9
Pod undocumented
135 0 ▶ my $messages = $self->fetch($stream, $consumer, 1, $timeout) || return;
Condition0%0/2
$self->fetch($stream, $consumer, 1, $timeout) || (return)
l!l
00
MC/DC0%0/1
$self->fetch($stream, $consumer, 1, $timeout) || (return)
$self->fetch($stream, $consumer, 1, $timeout)
Truth table0%0/2
A
A= $self->fetch($stream, $consumer, 1, $timeout)
Aresult
11
00
136 0 return $messages->[0];
137 }
138  
139 sub ack {
140 0 ▶ return _ack($_[0], $_[1], '');
Subroutine ack: not called CC=1 SCAR=6.9
Pod undocumented
141 }
142  
143 sub nak {
144 0 ▶ return _ack($_[0], $_[1], '-NAK');
Subroutine nak: not called CC=1 SCAR=6.9
Pod undocumented
145 }
146  
147 sub term {
148 0 ▶ return _ack($_[0], $_[1], '+TERM');
Subroutine term: not called CC=1 SCAR=6.9
Pod undocumented
149 }
150  
151 sub _ack {
152 0 ▶ my ($self, $message, $payload) = @_;
Subroutine _ack: not called CC=2 SCAR=17.9
153 0 ▶ return unless defined $message->reply_to;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message->reply_to 0 0 0
154 0 return $self->client->publish($message->reply_to, $payload);
155 }
156  
157 sub _status {
158 0 ▶ my $message = shift;
Subroutine _status: not called CC=5 SCAR=34.0
159 0 ▶ return unless defined $message->headers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message->headers 0 0 0
160 0 my ($code, $description) = $message->headers =~ /^NATS\/1\.0\s+(\d+)(?:\s+([^\r\n]*))?/m;
161 0 ▶ ($code) = $message->headers =~ /^Status:\s*(\d+)(?:\s+.*)?$/mi unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
162 0 ▶ return unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
163 0 ▶ ($description) = $message->headers =~ /^Description:\s*(.*)$/mi unless defined $description;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $description 0 0 0
164 0 return {code => $code, description => $description};
165 }
166  
167 sub _request {
168 0 ▶ my ($self, $subject, $data, $timeout) = @_;
Subroutine _request: not called CC=7 SCAR=40.3
169 0 $self->last_error(undef);
170 0 ▶ $timeout = $self->timeout unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
171 0 ▶ $timeout = 1 unless defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 0 0 0
172  
173 0 my $message = $self->client->request_sync($subject, $data, $timeout);
174 0 ▶ unless ($message) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($message) 0 0 0
175 0 $self->last_error({description => 'JetStream request timed out or failed'});
176 0 return;
177 }
178  
179 0 my $response = eval { decode_json($message->data) };
180 0 ▶ if ($@ || ref $response ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $response ne "HASH") 0 0 0
Condition0%0/3
$@ or ref $response ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or ref $response ne "HASH"
$@ ref $response ne "HASH"
Truth table0%0/3
A or B
A= $@B= ref $response ne "HASH"
ABresult
1X1
011
000
181 0 $self->last_error({description => 'JetStream returned an invalid JSON response'});
182 0 return;
183 }
184 0 ▶ if ($response->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"error"}) 0 0 0
185 0 $self->last_error($response->{error});
186 0 return;
187 }
188 0 return $response;
189 }
190  
191 1 1;
192  
193 __END__
« Client.pm ↑ Summary Connection.pm »