Mojo_IOLoop.pm

filter: Statementstmt 92.8% 104 / 112 Branchbran 76.4% 26 / 34 Conditioncond 72.7% 8 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 84.0% 21 / 25 Podpod 0.0% 0 / 13 totaltotal 80.2% 162 / 202 SCAR 31.8
CC
23
Cov
88%
_loop_select_callback
6.0
loop_watch_filehandle
5.1
loop_initialize
3.3
CRAP 23.9
↑ Summary Mojo_IOLoop_Tester_EV.pm »
1 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package POE::Loop::Mojo_IOLoop;
4  
5 35 ▶use POE::Loop::PerlSignals;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '0.002';
8  
9 =for poe_tests
10 BEGIN { $ENV{POE_EVENT_LOOP} = 'POE::Loop::Mojo_IOLoop' }
11 BEGIN { $ENV{MOJO_REACTOR} ||= 'Mojo::Reactor::Poll' }
12 sub skip_tests {
13 return "Mojo::IOLoop tests require the Mojo::IOLoop module" if (
14 do { eval "use Mojo::IOLoop"; $@ }
15 );
16 if (shift eq '00_info') {
17 my $reactor = Mojo::IOLoop->singleton->reactor;
18 diag("Using reactor $reactor");
19 }
20 }
21  
22 =cut
23  
24 package
25 POE::Kernel;
26  
27 35 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 35 ▶use constant MOJO_DEBUG => $ENV{POE_LOOP_MOJO_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'POE_LOOP_MOJO_DEBUG'} || 0
l!l
035
MC/DC0%0/1
$ENV{'POE_LOOP_MOJO_DEBUG'} || 0
$ENV{'POE_LOOP_MOJO_DEBUG'}
Truth table50%1/2
A
A= $ENV{'POE_LOOP_MOJO_DEBUG'}
Aresult
11
00
31  
32 35 my $_timer_id;
33 my $_idle_id;
34 35 my @fileno_watcher;
35 35 my $_async_check;
36 35 my $DIE_MESSAGE;
37  
38 # Loop construction and destruction.
39  
40 sub loop_initialize {
41 51 ▶ my $self = shift;
Subroutine loop_initialize: called CC=3 SCAR=12.0
Pod undocumented
42
43 51 my $reactor = Mojo::IOLoop->singleton->reactor;
44 51 ▶ if ($reactor->isa('Mojo::Reactor::EV')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reactor->isa("Mojo::Reactor::EV")) 0 51 51
45 # Workaround to ensure perl signal handlers are called
46 0 ▶ $_async_check = EV::check(sub { });
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 # EV will ignore exceptions unless this is set
48 0 $EV::DIED = \&_die_handler;
49 }
50
51 51 warn "-- Initialized loop with reactor $reactor\n" if MOJO_DEBUG;
52
53 51 ▶ unless (defined $_idle_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_idle_id) 51 0 51
54 51 $_idle_id = Mojo::IOLoop->next_tick(\&_loop_resume_timer);
55 }
56 }
57  
58 sub loop_finalize {
59 46 ▶ my $self = shift;
Subroutine loop_finalize: called CC=3 SCAR=11.0
Pod undocumented
60
61 46 warn "-- Finalized loop\n" if MOJO_DEBUG;
62
63 46 foreach my $fd (0..$#fileno_watcher) {
64 517 ▶ POE::Kernel::_warn(
Branch: 1 branch
BranchTrueFalseTotal
if defined $fileno_watcher[$fd] 0 517 517
65 "Filehandle watcher for fileno $fd is defined during loop finalize"
66 ) if defined $fileno_watcher[$fd];
67 }
68
69 46 $self->loop_ignore_all_signals();
70 46 undef $_async_check;
71 }
72  
73 sub _die_handler {
74 0 ▶ warn "-- Died $@\n" if MOJO_DEBUG;
Subroutine _die_handler: not called CC=1 SCAR=6.9
75
76 # EV doesn't let you rethrow an error here, so we have
77 # to stop the loop and get the error later
78 0 $DIE_MESSAGE = $@;
79
80 # This will cause the EV::run call in loop_run to return,
81 # and cause the process to die.
82 0 EV::break();
83 }
84  
85 # Signal handler maintenance functions.
86  
87 ▶sub loop_attach_uidestroy {
Subroutine loop_attach_uidestroy: not called CC=1 SCAR=0.0
Pod undocumented
88 # does nothing
89 }
90  
91 # Maintain time watchers.
92  
93 sub loop_resume_time_watcher {
94 1237 ▶ my ($self, $next_time) = @_;
Subroutine loop_resume_time_watcher: called CC=3 SCAR=11.0
Pod undocumented
95
96 1237 $next_time -= Time::HiRes::time;
97 1237 ▶ $next_time = 0 if $next_time < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next_time < 0 944 293 1237
98
99 1237 warn "-- Resume time watcher in ${next_time}s\n" if MOJO_DEBUG;
100
101 1237 ▶ Mojo::IOLoop->remove($_timer_id) if defined $_timer_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_timer_id 384 853 1237
102 1237 $_timer_id = Mojo::IOLoop->timer($next_time => \&_loop_event_callback);
103 }
104  
105 sub loop_reset_time_watcher {
106 432 ▶ my ($self, $next_time) = @_;
Subroutine loop_reset_time_watcher: called CC=2 SCAR=6.9
Pod undocumented
107
108 432 warn "-- Reset time watcher to $next_time\n" if MOJO_DEBUG;
109
110 432 ▶ Mojo::IOLoop->remove($_timer_id) if defined $_timer_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_timer_id 324 108 432
111 432 undef $_timer_id;
112 432 $self->loop_resume_time_watcher($next_time);
113 }
114  
115 sub _loop_resume_timer {
116 446 ▶ Mojo::IOLoop->remove($_idle_id) if defined $_idle_id;
Subroutine _loop_resume_timer: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if defined $_idle_id 0 446 446
117 446 undef $_idle_id;
118 446 $poe_kernel->loop_resume_time_watcher($poe_kernel->get_next_event_time());
119 }
120  
121 ▶sub loop_pause_time_watcher {
Subroutine loop_pause_time_watcher: called CC=1 SCAR=0.0
Pod undocumented
122 # does nothing
123 }
124  
125 # Maintain filehandle watchers.
126  
127 sub loop_watch_filehandle {
128 330 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_watch_filehandle: called CC=5 SCAR=16.3
Pod undocumented
129 330 my $fileno = fileno $handle;
130
131 330 ▶ confess "POE::Loop::Mojo_IOLoop does not support MODE_EX" if $mode == MODE_EX;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == 2 0 330 330
132 330 ▶ confess "Unknown mode $mode" unless $mode == MODE_RD or $mode == MODE_WR;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode == 0 or $mode == 1 0 330 330
Condition66%2/3
$mode == 0 or $mode == 1
l!l&&r!l&&!r
1911390
MC/DC0%0/2
$mode == 0 or $mode == 1
$mode == 0 $mode == 1
Truth table66%2/3
A or B
A= $mode == 0B= $mode == 1
ABresult
1X1
011
000
133
134 330 warn "-- Watch filehandle $fileno, mode $mode\n" if MOJO_DEBUG;
135
136 # Set up callback if needed
137 330 ▶ unless (defined $fileno_watcher[$fileno]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fileno_watcher[$fileno]) 253 77 330
138 Mojo::IOLoop->singleton->reactor->io($handle => sub {
139 620 ▶ my ($reactor, $writable) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
140 620 _loop_select_callback($fileno, $writable);
141 253 });
142 }
143
144 330 $fileno_watcher[$fileno][$mode] = 1;
145
146 330 _update_select_watcher($handle);
147 }
148  
149 sub loop_ignore_filehandle {
150 294 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_ignore_filehandle: called CC=1 SCAR=0.0
Pod undocumented
151 294 my $fileno = fileno $handle;
152
153 294 warn "-- Ignore filehandle $fileno, mode $mode\n" if MOJO_DEBUG;
154
155 294 undef $fileno_watcher[$fileno][$mode];
156
157 294 _update_select_watcher($handle);
158 }
159  
160 sub loop_pause_filehandle {
161 288 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_pause_filehandle: called CC=1 SCAR=0.0
Pod undocumented
162 288 my $fileno = fileno $handle;
163
164 288 warn "-- Pause filehandle $fileno, mode $mode\n" if MOJO_DEBUG;
165
166 288 $fileno_watcher[$fileno][$mode] = 0;
167
168 288 _update_select_watcher($handle);
169 }
170  
171 sub loop_resume_filehandle {
172 248 ▶ my ($self, $handle, $mode) = @_;
Subroutine loop_resume_filehandle: called CC=1 SCAR=0.0
Pod undocumented
173 248 my $fileno = fileno $handle;
174
175 248 warn "-- Resume filehandle $fileno, mode $mode\n" if MOJO_DEBUG;
176
177 248 $fileno_watcher[$fileno][$mode] = 1;
178
179 248 _update_select_watcher($handle);
180 }
181  
182 sub _update_select_watcher {
183 1160 ▶ my ($handle) = @_;
Subroutine _update_select_watcher: called CC=3 SCAR=11.0
184 1160 my $fileno = fileno $handle;
185
186 1160 my ($read, $write) = @{$fileno_watcher[$fileno]}[MODE_RD(),MODE_WR()];
187
188 # Don't remove watcher unless both read and write have been ignored
189 1160 ▶ if (defined $read or defined $write) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read or defined $write) { } 939 221 1160
Condition100%3/3
defined $read or defined $write
l!l&&r!l&&!r
647292221
MC/DC100%2/2
defined $read or defined $write
defined $read defined $write
Truth table100%3/3
A or B
A= defined $readB= defined $write
ABresult
1X1
011
000
190 939 Mojo::IOLoop->singleton->reactor->watch($handle, $read, $write);
191 } else {
192 221 Mojo::IOLoop->singleton->reactor->remove($handle);
193 221 undef $fileno_watcher[$fileno];
194 }
195 }
196  
197 # Timer callback to dispatch events.
198  
199 sub _loop_event_callback {
200 567 ▶ my $self = $poe_kernel;
Subroutine _loop_event_callback: called CC=2 SCAR=6.9
201
202 567 warn "-- Timer callback\n" if MOJO_DEBUG;
203
204 567 $self->_data_ev_dispatch_due();
205 567 $self->_test_if_kernel_is_idle();
206
207 567 undef $_timer_id;
208
209 567 ▶ if ($self->get_event_count()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_event_count) 410 157 567
210 410 $_idle_id = Mojo::IOLoop->next_tick(\&_loop_resume_timer);
211 }
212
213 # Transferring control back to Mojo::IOLoop; this is idle time.
214 }
215  
216 # Mojo::IOLoop filehandle callback to dispatch selects.
217  
218 sub _loop_select_callback {
219 620 ▶ my $self = $poe_kernel;
Subroutine _loop_select_callback: called CC=6 SCAR=18.0
220 620 my ($fileno, $writable) = @_;
221
222 620 ▶ my $mode = $writable ? MODE_WR : MODE_RD;
Branch: 1 branch
BranchTrueFalseTotal
$writable ? : 246 374 620
223
224 # Make sure this callback is actually registered
225 620 ▶ return unless defined $fileno_watcher[$fileno];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fileno_watcher[$fileno] 0 620 620
226
227 # Workaround for write-only mode receiving read callback
228 620 ▶ unless ($fileno_watcher[$fileno][$mode]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fileno_watcher[$fileno][$mode]) 43 577 620
229 43 ▶ if ($mode == MODE_RD and $fileno_watcher[$fileno][MODE_WR()]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode == 0 and $fileno_watcher[$fileno][1]) { } 1 42 43
Condition66%2/3
$mode == 0 and $fileno_watcher[$fileno][1]
!ll&&!rl&&r
0421
MC/DC50%1/2
$mode == 0 and $fileno_watcher[$fileno][1]
$mode == 0 $fileno_watcher[$fileno][1]
Truth table66%2/3
A and B
A= $mode == 0B= $fileno_watcher[$fileno][1]
ABresult
0X0
100
111
230 # Use write callback
231 1 $mode = MODE_WR;
232 } else {
233 42 return;
234 }
235 }
236
237 578 warn "-- Select callback for filehandle $fileno, mode $mode\n"
238 if MOJO_DEBUG;
239
240 578 $self->_data_handle_enqueue_ready($mode, $fileno);
241 578 $self->_test_if_kernel_is_idle();
242 }
243  
244 # The event loop itself.
245  
246 ▶sub loop_do_timeslice {
Subroutine loop_do_timeslice: not called CC=1 SCAR=0.0
Pod undocumented
247 }
248  
249 sub loop_run {
250 46 ▶ warn "-- Loop run\n" if MOJO_DEBUG;
Subroutine loop_run: called CC=2 SCAR=9.2
Pod undocumented
251 46 Mojo::IOLoop->start;
252
253 46 ▶ if (defined $DIE_MESSAGE) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DIE_MESSAGE) 0 46 46
254 0 my $message = $DIE_MESSAGE;
255 0 undef $DIE_MESSAGE;
256 0 die $message;
257 }
258 }
259  
260 sub loop_halt {
261 46 ▶ warn "-- Loop halt\n" if MOJO_DEBUG;
Subroutine loop_halt: called CC=1 SCAR=0.0
Pod undocumented
262 46 Mojo::IOLoop->stop;
263 }
264  
265 =head1 NAME
266  
267 POE::Loop::Mojo_IOLoop - a bridge that allows POE to be driven by Mojo::IOLoop
268  
269 =head1 SYNOPSIS
270  
271 See L<POE::Loop>.
272  
273 use POE qw(Loop::Mojo_IOLoop);
274
275 use POE::Kernel { loop => 'Mojo::IOLoop' };
276
277 BEGIN { $ENV{POE_EVENT_LOOP} = 'POE::Loop::Mojo_IOLoop' }
278 use POE;
279  
280 =head1 DESCRIPTION
281  
282 L<POE::Loop::Mojo_IOLoop> implements the interface documented in L<POE::Loop>.
283 Therefore it has no documentation of its own. Please see L<POE::Loop> for more
284 details.
285  
286 When using L<Mojo::IOLoop> with L<POE>, the loop must be set explicitly or
287 L<POE> will detect multiple event loops and fail. You can set the event loop
288 by setting the environment variable C<POE_EVENT_LOOP> to
289 C<POE::Loop::Mojo_IOLoop> before L<POE> is loaded, or by including it in the
290 C<use> directive when initially loading L<POE>. See
291 L<POE::Kernel/"Using POE with Other Event Loops"> for more information.
292  
293 =head1 BUGS
294  
295 Report any issues on the public bugtracker.
296  
297 =head1 AUTHOR
298  
299 Dan Book, C<dbook@cpan.org>
300  
301 =head1 COPYRIGHT AND LICENSE
302  
303 Copyright 2015, Dan Book.
304  
305 This library is free software; you may redistribute it and/or modify it under
306 the terms of the Artistic License version 2.0.
307  
308 =head1 SEE ALSO
309  
310 L<POE>, L<POE::Loop>, L<Mojo::IOLoop>, L<POE::Loop::PerlSignals>
311  
312 =cut
313  
314 35 1;
↑ Summary Mojo_IOLoop_Tester_EV.pm »