blib/lib/AnyEvent/MockTCPServer.pm

filter: Statementstmt 90.4% 133 / 147 Branchbran 56.2% 9 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.6% 29 / 32 Podpod 100% 15 / 15 totaltotal 86.5% 186 / 215 CC 12 SCAR 11.9
CC
12
Cov
86%
__ANON__
20.0
DESTROY
3.0
__ANON__
3.0
CRAP 3.3
↑ Summary
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package AnyEvent::MockTCPServer;
4 5 $AnyEvent::MockTCPServer::VERSION = '1.172150';
5 # ABSTRACT: Mock TCP Server using AnyEvent
6  
7  
8 5 1;
9  
10 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 DEBUG => $ENV{ANYEVENT_MOCK_TCP_SERVER_DEBUG}
12 5 };
13 5 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 5 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
21 5 my $pkg = shift;
22 5 my $finished_cv = AnyEvent->condvar;
23 my $self =
24 {
25 connections => [],
26 listening => AnyEvent->condvar,
27 finished_cv => $finished_cv,
28 host => '127.0.0.1',
29 port => 0,
30 timeout => 2,
31 on_timeout => subname('default_client_on_timeout_cb' =>
32 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 $finished_cv->end;
34 1 die "server timeout\n";
35 5 }),
36 @_
37 };
38 5 bless $self, $pkg;
39  
40 5 foreach (@{$self->{connections}}) {
41 5 $finished_cv->begin;
42 }
43  
44 $self->{server} =
45 tcp_server $self->{host}, $self->{port}, subname('accept_cb' =>
46 6 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
47 6 my ($fh) = @_;
48 6 print STDERR "In server: $fh ", fileno($fh), "\n" if DEBUG;
49 6 my $handle;
50 $handle =
51 AnyEvent::Handle->new(fh => $fh,
52 on_error => subname('client_on_error_cb_'.$fh =>
53 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
54 1 my ($hdl, $fatal, $msg) = @_;
55 1 warn "error $msg\n";
56 $self->{on_error}->(@_)
57 1 ▶ if ($self->{on_error});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"on_error"} 1 0 1
58 1 $hdl->destroy;
59 }),
60 timeout => $self->{timeout},
61 on_timeout => $self->{on_timeout},
62 6 );
63 6 print STDERR "Connection handle: $handle\n" if DEBUG;
64 6 $self->{handles}->{$handle} = $handle;
65 6 my $con = $self->{connections};
66 6 ▶ unless (@$con) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$con) 1 5 6
67 1 die "Server received unexpected connection\n";
68 }
69 5 my $actions = shift @$con;
70 5 print STDERR "Actions: ", (scalar @$actions), "\n" if DEBUG;
71 5 ▶ unless (@$con) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$con) 4 1 5
72 4 delete $self->{server};
73 }
74 5 $self->next_action($handle, $actions);
75 5 ▶ }), subname('prepare_cb' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
76 5 my ($fh, $host, $port) = @_;
77 5 ▶ die "tcp_server setup failed: $!\n" unless ($fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 5 5
78 5 $self->{listening}->send([$host, $port]);
79 5 0;
80 5 });
81 5 return $self;
82 }
83  
84 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
85 3 my $self = shift;
86 3 delete $self->{listening};
87 3 delete $self->{server};
88 3 foreach (values %{$self->{handles}}) {
89 1 ▶ next unless (defined $_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 1 1
90 1 $_->destroy;
91 1 delete $self->{handles}->{$_};
92 }
93 }
94  
95  
96 8 ▶sub listening {
Subroutine listening: called CC=1 SCAR=0.0
Pod documented
97 8 shift->{listening};
98 }
99  
100  
101 6 ▶sub connect_address {
Subroutine connect_address: called CC=1 SCAR=0.0
Pod documented
102 6 @{shift->listening->recv};
103 }
104  
105  
106 1 ▶sub connect_host {
Subroutine connect_host: called CC=1 SCAR=0.0
Pod documented
107 1 shift->listening->recv->[0];
108 }
109  
110  
111 1 ▶sub connect_port {
Subroutine connect_port: called CC=1 SCAR=0.0
Pod documented
112 1 shift->listening->recv->[1];
113 }
114  
115  
116 1 ▶sub connect_string {
Subroutine connect_string: called CC=1 SCAR=0.0
Pod documented
117 1 join ':', shift->connect_address
118 }
119  
120  
121 0 ▶sub finished_cv {
Subroutine finished_cv: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123 0 $self->{finished_cv};
124 }
125  
126  
127 13 ▶sub next_action {
Subroutine next_action: called CC=2 SCAR=6.9
Pod documented
128 13 my ($self, $handle, $actions) = @_;
129 13 print STDERR 'In handle connection ', scalar @$actions, "\n" if DEBUG;
130 13 my $action = shift @$actions;
131 13 ▶ unless (defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 3 10 13
132 3 print STDERR "closing connection\n" if DEBUG;
133 3 $handle->push_shutdown;
134 3 delete $self->{handles}->{$handle};
135 3 $self->{finished_cv}->end;
136 3 return;
137 }
138 10 my $method = shift @$action;
139 10 print STDERR "executing action: ", $method, "\n" if DEBUG;
140 10 $self->$method($handle, $actions, @$action);
141 }
142  
143  
144 1 ▶sub send {
Subroutine send: called CC=1 SCAR=0.0
Pod documented
145 1 my ($self, $handle, $actions, $send, $desc) = @_;
146 1 print STDERR 'Sending: ', $send, ' ', $desc, "\n" if DEBUG;
147 1 print STDERR 'Sending ', length $send, " bytes\n" if DEBUG;
148 1 $handle->push_write($send);
149 1 $self->next_action($handle, $actions);
150 }
151  
152  
153 2 ▶sub packsend {
Subroutine packsend: called CC=1 SCAR=0.0
Pod documented
154 2 my ($self, $handle, $actions, $data, $desc) = @_;
155 2 my $send = $data;
156 2 $send =~ s/\s+//g;
157 2 print STDERR 'Sending: ', $send, ' ', $desc, "\n" if DEBUG;
158 2 $send = pack 'H*', $send;
159 2 print STDERR 'Sending ', length $send, " bytes\n" if DEBUG;
160 2 $handle->push_write($send);
161 2 $self->next_action($handle, $actions);
162 }
163  
164  
165 1 ▶sub recv {
Subroutine recv: called CC=1 SCAR=0.0
Pod documented
166 1 my ($self, $handle, $actions, $recv, $desc) = @_;
167 1 print STDERR 'Waiting for ', $recv, ' ', $desc, "\n" if DEBUG;
168 1 my $len = length $recv;
169 1 print STDERR 'Waiting for ', $len, " bytes\n" if DEBUG;
170 $handle->push_read(chunk => $len,
171 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 1 my ($hdl, $data) = @_;
173 1 print STDERR "In receive handler\n" if DEBUG;
174 1 is($data, $recv,
175 '... correct message received by server - '.$desc);
176 1 $self->next_action($hdl, $actions);
177 1 1;
178 1 });
179 }
180  
181  
182 0 ▶sub recvline {
Subroutine recvline: not called CC=1 SCAR=6.9
Pod documented
183 0 my ($self, $handle, $actions, $recv, $desc) = @_;
184 0 print STDERR 'Waiting for ', $recv, ' ', $desc, "\n" if DEBUG;
185 0 print STDERR "Waiting for line\n" if DEBUG;
186 $handle->push_read(line =>
187 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
188 0 my ($hdl, $data) = @_;
189 0 print STDERR "In receive handler\n" if DEBUG;
190 0 ▶ $recv = $recv->() if (ref $recv && ref $recv eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if ref $recv and ref $recv eq "CODE" 0 0 0
Condition0%0/3
ref $recv and ref $recv eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $recv and ref $recv eq "CODE"
ref $recv ref $recv eq "CODE"
Truth table0%0/3
A and B
A= ref $recvB= ref $recv eq "CODE"
ABresult
0X0
100
111
191 0 ▶ if (ref $recv) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $recv) { } 0 0 0
192 0 like($data, $recv,
193 '... correct message received by server - '.$desc);
194 } else {
195 0 is($data, $recv,
196 '... correct message received by server - '.$desc);
197 }
198 0 $self->next_action($hdl, $actions);
199 0 1;
200 0 });
201 }
202  
203  
204 4 ▶sub packrecv {
Subroutine packrecv: called CC=1 SCAR=0.0
Pod documented
205 4 my ($self, $handle, $actions, $data, $desc) = @_;
206 4 my $recv = $data;
207 4 $recv =~ s/\s+//g;
208 4 my $expect = $recv;
209 4 print STDERR 'Waiting for ', $recv, ' ', $desc, "\n" if DEBUG;
210 4 my $len = .5*length $recv;
211 4 print STDERR 'Waiting for ', $len, " bytes\n" if DEBUG;
212 $handle->push_read(chunk => $len,
213 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 2 my ($hdl, $data) = @_;
215 2 print STDERR "In receive handler\n" if DEBUG;
216 2 my $got = uc unpack 'H*', $data;
217 2 is($got, $expect,
218 '... correct message received by server - '.$desc);
219 2 $self->next_action($hdl, $actions);
220 2 1;
221 4 });
222 }
223  
224  
225 1 ▶sub sleep {
Subroutine sleep: called CC=1 SCAR=0.0
Pod documented
226 1 my ($self, $handle, $actions, $interval, $desc) = @_;
227 1 print STDERR 'Sleeping for ', $interval, ' ', $desc, "\n" if DEBUG;
228 1 my $w;
229 $w = AnyEvent->timer(after => $interval,
230 1 ▶ cb => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
231 1 $self->next_action($handle, $actions);
232 1 undef $w;
233 1 });
234 }
235  
236  
237 1 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
Pod documented
238 1 my ($self, $handle, $actions, $code, $desc) = @_;
239 1 print STDERR 'Executing ', $code, ' for ', $desc, "\n" if DEBUG;
240 1 $code->($self, $handle, $desc);
241 1 $self->next_action($handle, $actions);
242 }
243  
244 5 1;
245  
246 __END__
↑ Summary