QWorker/Worker.pm

filter: Statementstmt 90.1% 73 / 81 Branchbran 54.5% 12 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 4 totaltotal 79.5% 101 / 127 CC 13 SCAR 10.2
CC
13
Cov
81%
_fork_worker
5.2
_child_loop
4.4
_shutdown
3.2
CRAP 2.8
« QWorker.pm ↑ Summary QWorker/WorkUnit.pm »
1 package IPC::QWorker::Worker;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 our $VERSION = '0.07'; # VERSION
8  
9 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Storable qw(fd_retrieve store_fd);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new {
15 65 ▶ my $this = shift;
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
16 65 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0650
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
17 65 my $self = {
18 'pid' => 0,
19 'pipe' => undef,
20 'calls' => {@_},
21 'ctx' => {},
22 'ready' => 0,
23 };
24 65 bless( $self, $class );
25 65 $self->_fork_worker();
26 55 return ($self);
27 }
28  
29 sub _fork_worker {
30 65 ▶ my $self = shift;
Subroutine _fork_worker: called CC=5 SCAR=16.5
31 65 my $pid;
32 my $parent_pipe;
33 65 my $child_pipe;
34  
35 65 ▶ socketpair( $parent_pipe, $child_pipe, AF_UNIX, SOCK_STREAM, PF_UNIX )
Branch: 1 branch
BranchTrueFalseTotal
socketpair $parent_pipe, $child_pipe, 1, 1, 1 or croak("cant create socketpair: $!") 0 65 65
36 or croak("cant create socketpair: $!");
37  
38 65 $parent_pipe->autoflush(1);
39 65 $child_pipe->autoflush(1);
40  
41 65 ▶ if ( $pid = fork ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid = fork) { } 55 10 65
42 55 close($parent_pipe);
43 55 $self->{'pid'} = $pid;
44 55 $self->{'pipe'} = $child_pipe;
45 }
46 else {
47 10 ▶ if ( !defined($pid) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 10 10
48 0 croak("cannot fork child process");
49 }
50 10 close($child_pipe);
51 10 $self->{'pipe'} = $parent_pipe;
52 10 ▶ if ( defined( $self->{'calls'}->{'_init'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"calls"}{"_init"}) 10 0 10
53 10 $self->{'calls'}->{'_init'}->( $self->{'ctx'} );
54 }
55 10 ▶ $SIG{'HUP'} = sub { $self->_shutdown() };
Subroutine __ANON__: called CC=1 SCAR=0.0
56 10 $self->_child_loop();
57 0 close($parent_pipe);
58 0 exit;
59 }
60 }
61  
62 sub _child_loop {
63 10 ▶ my $self = shift;
Subroutine _child_loop: called CC=4 SCAR=14.8
64 10 my $qentry;
65  
66 10 $self->send_ready();
67  
68 10 ▶ while ( $qentry = ${ fd_retrieve( $self->{'pipe'} ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qentry = ${fd_retrieve($self->{"pipe"});}) 120 0 120
69 120 ▶ if ($IPC::QWorker::DEBUG) { print STDERR Dumper($qentry); }
Branch: 1 branch
BranchTrueFalseTotal
if ($IPC::QWorker::DEBUG) 0 120 120
70  
71 120 ▶ if ( defined $self->{'calls'}->{ $qentry->{'cmd'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'calls'}{$qentry->{'cmd'}}) { } 120 0 120
72 $self->{'calls'}->{ $qentry->{'cmd'} }
73 120 ->( $self->{'ctx'}, $qentry->{'params'} );
74 }
75 else {
76 0 croak( $$ . ": no such call defined in this worker" );
77 }
78  
79 120 $self->send_ready();
80 }
81 }
82  
83 sub send_ready {
84 130 ▶ my $self = shift();
Subroutine send_ready: called CC=1 SCAR=0.0
Pod undocumented
85  
86 130 print ${ $self->{'pipe'} }($$." READY\n");
87 }
88  
89 sub send_entry {
90 120 ▶ my $self = shift;
Subroutine send_entry: called CC=2 SCAR=7.2
Pod undocumented
91 120 my $qentry = shift;
92  
93 120 $self->{'ready'} = 0;
94  
95 120 ▶ if ($IPC::QWorker::DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IPC::QWorker::DEBUG) 0 120 120
96 0 print STDERR $$ . ": sending entry...\n";
97 }
98 120 store_fd( \$qentry, $self->{'pipe'} );
99 }
100  
101 sub exit_child {
102 10 ▶ my $self = shift();
Subroutine exit_child: called CC=2 SCAR=7.4
Pod undocumented
103  
104 10 ▶ if ( $self->{'pid'} == 0 ) { # do nothing when called from within child
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pid"} == 0) 0 10 10
105 0 return ();
106 }
107  
108 10 kill( 'HUP', $self->{'pid'} );
109 10 waitpid( $self->{'pid'}, 0 );
110 }
111  
112 sub _shutdown {
113 10 ▶ my $self = shift();
Subroutine _shutdown: called CC=3 SCAR=11.8
114  
115 10 ▶ if ( defined( $self->{'calls'}->{'_destroy'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"calls"}{"_destroy"}) 10 0 10
116 10 $self->{'calls'}->{'_destroy'}->( $self->{'ctx'} );
117 }
118 10 ▶ if ($IPC::QWorker::DEBUG) { print STDERR $$ . ": exiting...\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($IPC::QWorker::DEBUG) 0 10 10
119 10 exit(0);
120 }
121  
122 12 1;
123  
124 # vim:ts=2:syntax=perl:
125 # vim600:foldmethod=marker:
« QWorker.pm ↑ Summary QWorker/WorkUnit.pm »