blib/lib/Net/Async/EmptyPort.pm

filter: Statementstmt 100% 33 / 33 Branchbran 62.5% 5 / 8 Conditioncond 64.2% 9 / 14 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 90.0% 9 / 10 Podpod 100% 2 / 2 totaltotal 81.0% 60 / 74 CC 12 SCAR 14.1
CC
12
Cov
85%
wait_port
6.4
empty_port
6.1
__ANON__
2.1
CRAP 4.1
↑ Summary
1 package Net::Async::EmptyPort;
2 1 $Net::Async::EmptyPort::VERSION = '0.001000';
3 # ABSTRACT: Asynchronously wait for a port to open
4  
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::Utils 'try_repeat_until_success', 'try_repeat';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 has _loop => (
9 is => 'ro',
10 init_arg => 'loop',
11 required => 1,
12 handles => {
13 _connect => 'connect',
14 _listen => 'listen',
15 _delay => 'delay_future',
16 _timeout => 'timeout_future',
17 },
18 );
19  
20 1 my %family_map = (
21 tcp => 'stream',
22 udp => 'dgram',
23 );
24 3 ▶sub empty_port {
Subroutine empty_port: called CC=6 SCAR=18.0
Pod documented
25 3 my ($self, $args) = @_;
26  
27 3 ▶ $args //= {};
Condition100%2/2
$args //= {}
l!l
12
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
28 3 ▶ $args->{host} //= '127.0.0.1';
Condition50%1/2
$args->{"host"} //= "127.0.0.1"
l!l
03
MC/DC0%0/1
$args->{"host"} //= "127.0.0.1"
$args->{"host"}
Truth table50%1/2
A
A= $args->{"host"}
Aresult
11
00
29 3 ▶ $args->{port} //= 0;
Condition100%2/2
$args->{"port"} //= 0
l!l
12
MC/DC100%1/1
$args->{"port"} //= 0
$args->{"port"}
Truth table100%2/2
A
A= $args->{"port"}
Aresult
11
00
30 3 ▶ $args->{proto} //= 'tcp';
Condition50%1/2
$args->{"proto"} //= "tcp"
l!l
03
MC/DC0%0/1
$args->{"proto"} //= "tcp"
$args->{"proto"}
Truth table50%1/2
A
A= $args->{"proto"}
Aresult
11
00
31  
32 3 ▶ if ($args->{port} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'port'} == 0) { } 2 1 3
33 $self->_listen(
34 1 ▶ on_socket => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
35 host => $args->{host},
36 socktype => $family_map{$args->{proto}},
37 service => $args->{port},
38 )
39 2 } else {
40 1 my $port = $args->{port};
41  
42 1 ▶ try_repeat {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 $self->_listen(
44 0 ▶ on_socket => sub {},
Subroutine __ANON__: not called CC=1 SCAR=0.0
45 host => $args->{host},
46 socktype => $family_map{$args->{proto}},
47 1 service => $port++,
48 )
49 1 ▶ } while => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
50 1 ▶ !shift->is_done && $port < 65000
Branch: 1 branch
BranchTrueFalseTotal
not +(shift())->is_done and $port < 65000 0 1 1
51 },
52 1 }
53 }
54  
55 1 ▶sub wait_port {
Subroutine wait_port: called CC=6 SCAR=18.6
Pod documented
56 1 my ($self, $args) = @_;
57  
58 die 'port is a required argument'
59 1 ▶ unless $args->{port};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"port"} 0 1 1
60  
61 1 ▶ $args->{host} //= '127.0.0.1';
Condition50%1/2
$args->{"host"} //= "127.0.0.1"
l!l
01
MC/DC0%0/1
$args->{"host"} //= "127.0.0.1"
$args->{"host"}
Truth table50%1/2
A
A= $args->{"host"}
Aresult
11
00
62 1 ▶ $args->{proto} //= 'tcp';
Condition50%1/2
$args->{"proto"} //= "tcp"
l!l
01
MC/DC0%0/1
$args->{"proto"} //= "tcp"
$args->{"proto"}
Truth table50%1/2
A
A= $args->{"proto"}
Aresult
11
00
63 1 ▶ $args->{max_wait} //= 10;
Condition50%1/2
$args->{"max_wait"} //= 10
l!l
01
MC/DC0%0/1
$args->{"max_wait"} //= 10
$args->{"max_wait"}
Truth table50%1/2
A
A= $args->{"max_wait"}
Aresult
11
00
64  
65 1 my $amount = 2;
66 1 my $attempt = 0;
67  
68 1 ▶ my $f = try_repeat_until_success {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 $self->_delay(
70 after => $amount * (2 ** $attempt++) - $amount,
71 1 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 $self->_connect(
73 host => $args->{host},
74 socktype => $family_map{$args->{proto}},
75 service => $args->{port},
76 )
77 1 })
78 1 };
79  
80 $f = Future->wait_any(
81 $f,
82 $self->_timeout( after => $args->{max_wait} )
83 1 ▶ ) if $args->{max_wait} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"max_wait"} > 0 1 0 1
84  
85 1 $f
86 }
87  
88 1 1;
89  
90 __END__
↑ Summary