Simple.pm

filter: Statementstmt 92.9% 145 / 156 Branchbran 61.1% 11 / 18 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.8% 36 / 41 Podpod 55.5% 5 / 9 totaltotal 86.5% 199 / 230 CC 12 SCAR 6.4
CC
12
Cov
89%
_on_exit
5.5
launch
4.1
_on_error
2.0
CRAP 1.9
« Simple/Util.pm ↑ Summary Simple/Channel.pm »
1 package IPC::Simple;
2 # ABSTRACT: simple, non-blocking IPC
3 4 $IPC::Simple::VERSION = '0.03';
4  
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use IPC::Open3 qw(open3);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use IPC::Simple::Channel;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use IPC::Simple::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use IPC::Simple::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 BEGIN{
21 4 ▶ extends 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 our @EXPORT = qw(
24 IPC_STDIN
25 IPC_STDOUT
26 IPC_STDERR
27 IPC_ERROR
28 );
29 }
30  
31 4 ▶use constant STATE_READY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use constant STATE_RUNNING => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use constant STATE_STOPPING => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 4 has cmd =>
36 is => 'ro',
37 isa => Str,
38 require => 1;
39  
40 has args =>
41 is => 'ro',
42 isa => ArrayRef[Str],
43 0 ▶ default => sub{ [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44  
45 4 has eol =>
46 is => 'ro',
47 isa => Str,
48 default => "\n",
49  
50 has run_state =>
51 is => 'rw',
52 isa => Enum[ STATE_READY, STATE_RUNNING, STATE_STOPPING ],
53 default => STATE_READY;
54  
55 4 has pid =>
56 is => 'rw',
57 isa => Num,
58 init_arg => undef;
59  
60 4 has fh_in =>
61 is => 'rw',
62 isa => FileHandle,
63 init_arg => undef;
64  
65 4 has fh_out =>
66 is => 'rw',
67 isa => FileHandle,
68 init_arg => undef;
69  
70 4 has fh_err =>
71 is => 'rw',
72 isa => FileHandle,
73 init_arg => undef;
74  
75 4 has handle_in =>
76 is => 'rw',
77 isa => InstanceOf['AnyEvent::Handle'],
78 init_arg => undef;
79  
80 4 has handle_out =>
81 is => 'rw',
82 isa => InstanceOf['AnyEvent::Handle'],
83 init_arg => undef;
84  
85 4 has handle_err =>
86 is => 'rw',
87 isa => InstanceOf['AnyEvent::Handle'],
88 init_arg => undef;
89  
90 4 has exit_status =>
91 is => 'rw',
92 isa => Maybe[Int],
93 init_arg => undef;
94  
95 4 has exit_code =>
96 is => 'rw',
97 isa => Maybe[Int],
98 init_arg => undef;
99  
100 4 has messages =>
101 is => 'rw',
102 isa => InstanceOf['IPC::Simple::Channel'],
103 init_arg => undef,
104 handles => {
105 recv => 'get',
106 };
107  
108 4 has sigchld_watcher =>
109 is => 'rw',
110 init_arg => undef;
111  
112 4 has sigchld_cv =>
113 is => 'rw',
114 isa => InstanceOf['AnyEvent::CondVar'],
115 init_arg => undef;
116  
117 sub DEMOLISH {
118 0 ▶ my $self = shift;
Subroutine DEMOLISH: not called CC=1 SCAR=6.9
Pod undocumented
119 0 $self->terminate;
120 0 $self->join;
121 }
122  
123 1 ▶sub is_ready { $_[0]->run_state == STATE_READY }
Subroutine is_ready: called CC=1 SCAR=0.0
Pod undocumented
124 19 ▶sub is_running { $_[0]->run_state == STATE_RUNNING }
Subroutine is_running: called CC=1 SCAR=0.0
Pod undocumented
125 5 ▶sub is_stopping { $_[0]->run_state == STATE_STOPPING }
Subroutine is_stopping: called CC=1 SCAR=0.0
Pod undocumented
126  
127 sub launch {
128 4 ▶ my $self = shift;
Subroutine launch: called CC=4 SCAR=14.0
Pod documented
129  
130 4 ▶ if ($self->is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_running) 0 4 4
131 0 croak 'already running';
132 }
133  
134 4 ▶ if ($self->is_stopping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_stopping) 0 4 4
135 0 croak 'process is terminating';
136 }
137  
138 4 debug('launching: %s %s', $self->cmd, "@{$self->args}");
139  
140 4 ▶ my $pid = open3(my $in, my $out, my $err = gensym, $self->cmd, @{$self->args})
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open3(my $in, my $out, my $err = &gensym(), $self->cmd, @{$self->args;}) or croak($!) 0 4 4
141 or croak $!;
142  
143 4 $self->sigchld_cv(AnyEvent->condvar);
144  
145 $self->sigchld_watcher(
146 AnyEvent->child(
147 pid => $pid,
148 cb => sub{
149 4 ▶ my ($pid, $status) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
150 4 $self->_on_exit($status);
151 },
152 )
153 4 );
154  
155 4 debug('process launched with pid %d', $pid);
156  
157 4 $self->run_state(STATE_RUNNING);
158 4 $self->exit_status(undef);
159 4 $self->exit_code(undef);
160 4 $self->pid($pid);
161 4 $self->fh_in($in);
162 4 $self->fh_out($out);
163 4 $self->fh_err($err);
164 4 $self->messages(IPC::Simple::Channel->new);
165 4 $self->handle_err($self->_build_input_handle($err, IPC_STDERR));
166 4 $self->handle_out($self->_build_input_handle($out, IPC_STDOUT));
167 4 $self->handle_in($self->_build_output_handle($in));
168  
169 4 return 1;
170 }
171  
172 sub _build_output_handle {
173 4 ▶ my ($self, $fh) = @_;
Subroutine _build_output_handle: called CC=1 SCAR=0.1
174  
175 # set non-blocking
176 4 AnyEvent::fh_unblock($fh);
177  
178 my $handle = AnyEvent::Handle->new(
179 fh => $fh,
180 0 ▶ on_error => sub{ $self->_on_error(IPC_STDIN, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 4 );
182  
183 4 return $handle;
184 }
185  
186 sub _build_input_handle {
187 8 ▶ my ($self, $fh, $type) = @_;
Subroutine _build_input_handle: called CC=1 SCAR=0.0
188  
189 # set non-blocking
190 8 AnyEvent::fh_unblock($fh);
191  
192 my $handle = AnyEvent::Handle->new(
193 fh => $fh,
194 3 ▶ on_eof => sub{ $self->terminate },
Subroutine __ANON__: called CC=1 SCAR=0.0
195 5 ▶ on_error => sub{ $self->_on_error($type, @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
196 0 ▶ on_read => sub{ $self->_on_read($type, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 8 );
198  
199 # push an initial read to prime the queue
200 8 $self->_push_read($handle, $type);
201  
202 8 return $handle;
203 }
204  
205 sub _on_error {
206 5 ▶ my ($self, $type, $handle, $fatal, $msg) = @_;
Subroutine _on_error: called CC=2 SCAR=7.0
207 5 debug('recv error type=%d, msg="%s"', $type, $msg);
208  
209 5 $self->messages->put(
210 IPC::Simple::Message->new(
211 source => IPC_ERROR,
212 message => $msg,
213 ),
214 );
215  
216 5 ▶ $self->terminate if $fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $fatal 5 0 5
217 }
218  
219 sub _on_exit {
220 4 ▶ my ($self, $status) = @_;
Subroutine _on_exit: called CC=5 SCAR=17.0
221 4 $self->run_state(STATE_READY);
222 4 ▶ $self->exit_status($status || 0);
Condition50%1/2
$status || 0
l!l
40
MC/DC0%0/1
$status || 0
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
223 4 $self->exit_code($self->exit_status >> 8);
224  
225 4 ▶ debug('child (pid %s) exited with status %d (exit code: %d)',
Condition50%1/2
$self->pid || '(no pid)'
l!l
40
MC/DC0%0/1
$self->pid || '(no pid)'
$self->pid
Truth table50%1/2
A
A= $self->pid
Aresult
11
00
226 $self->pid || '(no pid)',
227 $self->exit_status,
228 $self->exit_code,
229 );
230  
231 4 ▶ $self->messages->shutdown
Branch: 1 branch
BranchTrueFalseTotal
if $self->messages 4 0 4
232 if $self->messages; # won't be set if launch failed early enough
233  
234 4 ▶ $self->sigchld_cv->send
Branch: 1 branch
BranchTrueFalseTotal
if $self->sigchld_cv 4 0 4
235 if $self->sigchld_cv;
236 }
237  
238 sub _on_read {
239 0 ▶ my ($self, $type, $handle) = @_;
Subroutine _on_read: not called CC=1 SCAR=6.9
240 0 debug('read event type=%d', $type);
241 0 $self->_push_read($handle, $type);
242 }
243  
244 sub _push_read {
245 8 ▶ my ($self, $handle, $type) = @_;
Subroutine _push_read: called CC=1 SCAR=0.0
246  
247 $handle->push_read(line => $self->eol, sub{
248 3 ▶ my ($handle, $line) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
249 3 chomp $line;
250 3 debug('recv type=%d, msg="%s"', $type, $line);
251  
252 3 $self->messages->put(
253 IPC::Simple::Message->new(
254 source => $type,
255 message => $line,
256 ),
257 );
258 8 });
259 }
260  
261 sub terminate {
262 13 ▶ my $self = shift;
Subroutine terminate: called CC=2 SCAR=6.9
Pod documented
263 13 ▶ if ($self->is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_running) 4 9 13
264 4 $self->run_state(STATE_STOPPING);
265 4 debug('sending TERM to pid %d', $self->pid);
266 4 kill 'TERM', $self->pid;
267 }
268 }
269  
270 sub join {
271 5 ▶ my $self = shift;
Subroutine join: called CC=2 SCAR=7.0
Pod documented
272 5 debug('waiting for process to exit, pid %d', $self->pid);
273 5 ▶ return unless $self->sigchld_cv;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->sigchld_cv 0 5 5
274 5 $self->sigchld_cv->recv;
275 }
276  
277 sub send {
278 2 ▶ my ($self, $msg) = @_;
Subroutine send: called CC=1 SCAR=0.0
Pod documented
279 2 debug('sending "%s"', $msg);
280 2 $self->handle_in->push_write($msg . $self->eol);
281 2 1;
282 }
283  
284 around recv => sub{
285 2 ▶ my $orig = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
286 2 my $self = shift;
287 2 debug('waiting on message');
288 2 $self->$orig();
289 4 };
290  
291 sub async {
292 1 ▶ my ($self, $cb) = @_;
Subroutine async: called CC=1 SCAR=0.0
Pod documented
293 1 my $cv = $self->messages->async;
294 1 ▶ $cv->cb(sub{ $cb->( $cv->recv ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
295 1 return;
296 }
297  
298 after run_state => sub{
299 37 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
300 37 ▶ debug('run state changed to %d', @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 12 25 37
301 4 };
302  
303 4 1;
304  
305 __END__
« Simple/Util.pm ↑ Summary Simple/Channel.pm »