blib/lib/TAP/Parser/Multiplexer.pm

filter: Statementstmt 100% 69 / 69 Branchbran 92.8% 13 / 14 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 92.7% 102 / 110 CC 11 SCAR 13.0
CC
11
Cov
96%
__ANON__
8.0
add
3.0
next
2.0
CRAP 3.7
« blib/lib/TAP/Parser/Iterator/Stream.pm ↑ Summary blib/lib/TAP/Parser/SourceHandler/File.pm »
1 package TAP::Parser::Multiplexer;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use base 'TAP::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use constant IS_WIN32 => $^O =~ /^(MS)?Win32$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use constant IS_VMS => $^O eq 'VMS';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use constant SELECT_OK => !( IS_VMS || IS_WIN32 );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 TAP::Parser::Multiplexer - Multiplex multiple TAP::Parsers
18  
19 =head1 VERSION
20  
21 Version 3.52
22  
23 =cut
24  
25 4 our $VERSION = '3.52';
26  
27 =head1 SYNOPSIS
28  
29 use TAP::Parser::Multiplexer;
30  
31 my $mux = TAP::Parser::Multiplexer->new;
32 $mux->add( $parser1, $stash1 );
33 $mux->add( $parser2, $stash2 );
34 while ( my ( $parser, $stash, $result ) = $mux->next ) {
35 # do stuff
36 }
37  
38 =head1 DESCRIPTION
39  
40 C<TAP::Parser::Multiplexer> gathers input from multiple TAP::Parsers.
41 Internally it calls select on the input file handles for those parsers
42 to wait for one or more of them to have input available.
43  
44 See L<TAP::Harness> for an example of its use.
45  
46 =head1 METHODS
47  
48 =head2 Class Methods
49  
50 =head3 C<new>
51  
52 my $mux = TAP::Parser::Multiplexer->new;
53  
54 Returns a new C<TAP::Parser::Multiplexer> object.
55  
56 =cut
57  
58 # new() implementation supplied by TAP::Object
59  
60 10 ▶sub _initialize {
Subroutine _initialize: called CC=1 SCAR=0.0
61 10 my $self = shift;
62 10 $self->{select} = IO::Select->new;
63 10 $self->{avid} = []; # Parsers that can't select
64 10 $self->{count} = 0;
65 10 return $self;
66 }
67  
68 ##############################################################################
69  
70 =head2 Instance Methods
71  
72 =head3 C<add>
73  
74 $mux->add( $parser, $stash );
75  
76 Add a TAP::Parser to the multiplexer. C<$stash> is an optional opaque
77 reference that will be returned from C<next> along with the parser and
78 the next result.
79  
80 =cut
81  
82 24 ▶sub add {
Subroutine add: called CC=3 SCAR=11.0
Pod documented
83 24 my ( $self, $parser, $stash ) = @_;
84  
85 24 ▶ if ( SELECT_OK && ( my @handles = $parser->get_select_handles ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@handles) = $parser->get_select_handles) { } 14 10 24
86 14 my $sel = $self->{select};
87  
88 # We have to turn handles into file numbers here because by
89 # the time we want to remove them from our IO::Select they
90 # will already have been closed by the iterator.
91 14 my @filenos = map { fileno $_ } @handles;
92 14 for my $h (@handles) {
93 21 $sel->add( [ $h, $parser, $stash, @filenos ] );
94 }
95  
96 14 $self->{count}++;
97 }
98 else {
99 10 push @{ $self->{avid} }, [ $parser, $stash ];
100 }
101 }
102  
103 =head3 C<parsers>
104  
105 my $count = $mux->parsers;
106  
107 Returns the number of parsers. Parsers are removed from the multiplexer
108 when their input is exhausted.
109  
110 =cut
111  
112 20 ▶sub parsers {
Subroutine parsers: called CC=1 SCAR=0.0
Pod documented
113 20 my $self = shift;
114 20 return $self->{count} + scalar @{ $self->{avid} };
115 }
116  
117 10 ▶sub _iter {
Subroutine _iter: called CC=1 SCAR=0.0
118 10 my $self = shift;
119  
120 10 my $sel = $self->{select};
121 10 my $avid = $self->{avid};
122 10 my @ready = ();
123  
124 142 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.9
125  
126 # Drain all the non-selectable parsers first
127 142 ▶ if (@$avid) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$avid) 34 108 142
128 34 my ( $parser, $stash ) = @{ $avid->[0] };
129 34 my $result = $parser->next;
130 34 ▶ shift @$avid unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 10 24 34
131 34 return ( $parser, $stash, $result );
132 }
133  
134 108 ▶ until (@ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ready) 86 22 108
135 86 ▶ return unless $sel->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $sel->count 10 76 86
136 76 @ready = $sel->can_read;
137 76 ▶ last if @ready || !$!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
if @ready or not $!{"EINTR"} 76 0 76
Condition33%1/3
@ready or not $!{"EINTR"}
l!l&&r!l&&!r
7600
MC/DC0%0/2
@ready or not $!{"EINTR"}
@ready not $!{"EINTR"}
Truth table33%1/3
A or not(B)
A= @readyB= not $!{"EINTR"}
ABresult
1X1
011
000
138 }
139  
140 98 my ( $h, $parser, $stash, @handles ) = @{ shift @ready };
141 98 my $result = $parser->next;
142  
143 98 ▶ unless ( defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 14 84 98
144 14 $sel->remove(@handles);
145 14 $self->{count}--;
146  
147 # Force another can_read - we may now have removed a handle
148 # thought to have been ready.
149 14 @ready = ();
150 }
151  
152 98 return ( $parser, $stash, $result );
153 10 };
154 }
155  
156 =head3 C<next>
157  
158 Return a result from the next available parser. Returns a list
159 containing the parser from which the result came, the stash that
160 corresponds with that parser and the result.
161  
162 my ( $parser, $stash, $result ) = $mux->next;
163  
164 If C<$result> is undefined the corresponding parser has reached the end
165 of its input (and will automatically be removed from the multiplexer).
166  
167 When all parsers are exhausted an empty list will be returned.
168  
169 if ( my ( $parser, $stash, $result ) = $mux->next ) {
170 if ( ! defined $result ) {
171 # End of this parser
172 }
173 else {
174 # Process result
175 }
176 }
177 else {
178 # All parsers finished
179 }
180  
181 =cut
182  
183 142 ▶sub next {
Subroutine next: called CC=2 SCAR=7.1
Pod documented
184 142 my $self = shift;
185 142 ▶ return ( $self->{_iter} ||= $self->_iter )->();
Condition66%2/3
$self->{"_iter"} ||= $self->_iter
l!l&&r!l&&!r
132100
MC/DC0%0/2
$self->{"_iter"} ||= $self->_iter
$self->{"_iter"} $self->_iter
Truth table66%2/3
A ||= B
A= $self->{"_iter"}B= $self->_iter
ABresult
1X1
011
000
186 }
187  
188 =head1 See Also
189  
190 L<TAP::Parser>
191  
192 L<TAP::Harness>
193  
194 =cut
195  
196 4 1;
« blib/lib/TAP/Parser/Iterator/Stream.pm ↑ Summary blib/lib/TAP/Parser/SourceHandler/File.pm »