blib/lib/POE/Test/Loops/ses_session.pm untested

filter: Statementstmt 0.0% 0 / 264 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 468 CC 16 SCAR 20.7
CC
16
Cov
0%
task_run
30.0
task_default
12.0
count
12.0
CRAP 8.0
1 #!/usr/bin/perl -w
2 # vim: ts=2 sw=2 expandtab
3  
4 # Tests basic compilation and events.
5  
6 use strict;
7  
8 use lib qw(./mylib ../mylib);
9  
10 sub POE::Kernel::ASSERT_DEFAULT () { 1 }
11  
12 BEGIN {
13 package
14 POE::Kernel;
15 use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'});
16 }
17  
18 use Test::More tests => 41;
19 use POE;
20  
21 diag("This test generates some STDERR during trace testing.");
22  
23 ### Test parameters and results.
24  
25 my $machine_count = 10;
26 my $event_count = 5;
27 my $sigalrm_caught = 0;
28 my $sigpipe_caught = 0;
29 my $sender_count = 0;
30 my $got_heap_count = 0;
31 my $default_count = 0;
32  
33 die "machine count must be even" if $machine_count & 1;
34  
35 ### Status registers for each state machine instance.
36  
37 my ( @completions, @objpack );
38  
39 #------------------------------------------------------------------------------
40 # Define a simple state machine.
41  
42 sub task_start {
43 my ($kernel, $session, $heap, $id) = @_[KERNEL, SESSION, HEAP, ARG0];
44 $heap->{count} = 0;
45 $kernel->yield( count => $id );
46 }
47  
48 sub task_run {
49 my ($kernel, $session, $heap, $id) = @_[KERNEL, SESSION, HEAP, ARG0];
50  
51 $sender_count++ if $_[SENDER] == $session;
52  
53 if ($heap->{count} & 1) {
54 $kernel->yield( bogus => $id ); # _default
55 }
56 else {
57 $kernel->post( $session, bogus => $id ); # _default
58 }
59  
60 if ( $kernel->call( $session, next_count => $id ) < $event_count ) {
61  
62 if ($heap->{count} & 1) {
63 $kernel->yield( count => $id );
64 }
65 else {
66 $kernel->post( $session, count => $id );
67 }
68  
69 }
70 else {
71 $heap->{id} = $id;
72 }
73 }
74  
75 sub task_default {
76 return 0 if $_[ARG0] eq '_signal'; # ignore signals
77 $default_count++ if $_[STATE] eq '_default';
78 }
79  
80 sub task_next_count {
81 my ($kernel, $session, $heap, $id) = @_[KERNEL, SESSION, HEAP, ARG0];
82 ++$heap->{count};
83 }
84  
85 sub task_stop {
86 $completions[$_[HEAP]->{id}] = $_[HEAP]->{count};
87 $got_heap_count++ if (
88 defined($_[HEAP]->{got_heap}) and
89 $_[HEAP]->{got_heap} == $_[HEAP]->{id}
90 );
91 }
92  
93 #------------------------------------------------------------------------------
94 # Test simple signals.
95  
96 # Spawn a quick state machine to test signals. This is a classic
97 # example of inline states being just that: inline anonymous coderefs.
98 # It makes quick hacks quicker!
99 POE::Session->create(
100 inline_states => {
101 _start => sub {
102 $_[HEAP]->{kills_to_go} = $event_count;
103 $_[KERNEL]->sig( ALRM => 'sigalrm_target' );
104 $_[KERNEL]->sig( PIPE => 'sigpipe_target' );
105 $_[KERNEL]->delay( fire_signals => 0.5 );
106 },
107 fire_signals => sub {
108 if ($_[HEAP]->{kills_to_go}--) {
109 $_[KERNEL]->delay( fire_signals => 0.5 );
110 if ($^O eq 'MSWin32') {
111 $_[KERNEL]->signal( $_[KERNEL], 'ALRM' );
112 $_[KERNEL]->signal( $_[KERNEL], 'PIPE' );
113 }
114 else {
115 kill ALRM => $$;
116 kill PIPE => $$;
117 }
118 }
119 # One last timer so the session lingers long enough to catch
120 # the final signal.
121 else {
122 $_[KERNEL]->delay( done_waiting => 1 );
123 }
124 },
125 sigalrm_target => sub {
126 $sigalrm_caught++ if $_[ARG0] eq 'ALRM';
127 $_[KERNEL]->sig_handled();
128 },
129 sigpipe_target => sub {
130 $sigpipe_caught++ if $_[ARG0] eq 'PIPE';
131 $_[KERNEL]->sig_handled();
132 },
133 done_waiting => sub {
134 $_[KERNEL]->sig( ALRM => undef );
135 $_[KERNEL]->sig( PIPE => undef );
136 },
137 _stop => sub { }, # Pacify assertions.
138 }
139 );
140  
141 # Spawn ten state machines.
142 for (my $i=0; $i<$machine_count; $i++) {
143  
144 POE::Session->create(
145 inline_states => {
146 _start => \&task_start,
147 _stop => \&task_stop,
148 count => \&task_run,
149 next_count => \&task_next_count,
150 _default => \&task_default,
151 },
152 args => [ $i ],
153 heap => { got_heap => $i },
154 );
155 }
156  
157 #------------------------------------------------------------------------------
158 # Simple client/server sessions using events as inter-session
159 # communications. Tests postbacks, too.
160  
161 POE::Session->create(
162 inline_states => {
163 _start => sub {
164 $_[KERNEL]->alias_set( 'server' );
165 $_[HEAP]->{response} = 0;
166 },
167 sync_query => sub {
168 $_[ARG0]->( ++$_[HEAP]->{response} );
169 },
170 query => sub {
171 $_[ARG0]->( ++$_[HEAP]->{response} );
172 },
173 _stop => sub { }, # Pacify assertions.
174 },
175 );
176  
177 # A simple client session. It requests five counts and then stops.
178 # Its magic is that it passes a postback for the response.
179  
180 my $postback_test = 1;
181 my $callback_test = 1;
182  
183 POE::Session->create(
184 inline_states => {
185 _start => sub {
186 $_[KERNEL]->yield( 'query' );
187 $_[HEAP]->{cookie} = 0;
188 },
189 query => sub {
190 $_[KERNEL]->post(
191 server =>
192 query => $_[SESSION]->postback(response => ++$_[HEAP]->{cookie})
193 );
194 $_[HEAP]->{sync_called_back} = 0;
195 $_[KERNEL]->call(
196 server =>
197 sync_query =>
198 $_[SESSION]->callback(sync_response => ++$_[HEAP]->{cookie})
199 );
200 $callback_test = 0 unless $_[HEAP]->{sync_called_back};
201 },
202 sync_response => sub {
203 my ($req, $rsp) = ($_[ARG0]->[0], $_[ARG1]->[0] + 1);
204 $callback_test = 0 unless $req == $rsp;
205 $_[HEAP]->{sync_called_back} = 1;
206 },
207 response => sub {
208 my ($req, $rsp) = ($_[ARG0]->[0], $_[ARG1]->[0] - 1);
209 $postback_test = 0 unless $req == $rsp;
210 if ($_[HEAP]->{cookie} < 5) {
211 $_[KERNEL]->yield( 'query' );
212 }
213 },
214 _stop => sub {
215 is(
216 $_[KERNEL]->get_active_session(), $_[SESSION],
217 "get_active_session within session"
218 );
219 is(
220 $_[KERNEL]->get_active_session()->get_heap(), $_[HEAP],
221 "get_heap during stop"
222 );
223 },
224 }
225 );
226  
227 #------------------------------------------------------------------------------
228 # Unmapped package session.
229  
230 package
231 UnmappedPackage;
232 use POE::Session; # for constants
233  
234 sub _start {
235 $_[KERNEL]->yield( 'count' );
236 $_[HEAP]->{count} = 0;
237 $_[HEAP]->{id} = $_[ARG0];
238 }
239  
240 sub count {
241 return unless $_[OBJECT] eq __PACKAGE__;
242 $_[KERNEL]->yield( 'count' ) if ++$_[HEAP]->{count} < $event_count;
243 }
244  
245 sub _stop {
246 $objpack[$_[HEAP]->{id}] = $_[HEAP]->{count};
247 }
248  
249 #------------------------------------------------------------------------------
250 # Unmapped object session.
251  
252 package
253 UnmappedObject;
254 use POE::Session; # for constants
255  
256 # Trivial constructor.
257 sub new { bless [ ], shift; }
258  
259 sub _start {
260 $_[KERNEL]->yield( 'count' );
261 $_[HEAP]->{count} = 0;
262 $_[HEAP]->{id} = $_[ARG0];
263 }
264  
265 sub count {
266 return unless ref($_[OBJECT]) eq __PACKAGE__;
267 $_[KERNEL]->yield( 'count' ) if ++$_[HEAP]->{count} < $event_count;
268 }
269  
270 sub _stop {
271 $objpack[$_[HEAP]->{id}] = $_[HEAP]->{count};
272 }
273  
274 #------------------------------------------------------------------------------
275 # Unmapped package session.
276  
277 package
278 MappedPackage;
279 use POE::Session; # for constants
280  
281 sub my_start {
282 $_[KERNEL]->yield( 'count' );
283 $_[HEAP]->{count} = 0;
284 $_[HEAP]->{id} = $_[ARG0];
285 }
286  
287 sub my_count {
288 return unless $_[OBJECT] eq __PACKAGE__;
289 $_[KERNEL]->yield( 'count' ) if ++$_[HEAP]->{count} < $event_count;
290 }
291  
292 sub my_stop {
293 $objpack[$_[HEAP]->{id}] = $_[HEAP]->{count};
294 }
295  
296 #------------------------------------------------------------------------------
297 # Unmapped object session.
298  
299 package
300 MappedObject;
301 use POE::Session; # for constants
302  
303 # Trivial constructor.
304 sub new { bless [ ], shift; }
305  
306 sub my_start {
307 $_[KERNEL]->yield( 'count' );
308 $_[HEAP]->{count} = 0;
309 $_[HEAP]->{id} = $_[ARG0];
310 }
311  
312 sub my_count {
313 return unless ref($_[OBJECT]) eq __PACKAGE__;
314 $_[KERNEL]->yield( 'count' ) if ++$_[HEAP]->{count} < $event_count;
315 }
316  
317 sub my_stop {
318 $objpack[$_[HEAP]->{id}] = $_[HEAP]->{count};
319 }
320  
321 #------------------------------------------------------------------------------
322 # Test the Package and Object sessions.
323  
324 package
325 main;
326  
327 # New style (create) object session without event to method name map.
328 POE::Session->create(
329 object_states => [
330 UnmappedObject->new() => [ '_start', 'count', '_stop' ],
331 ],
332 args => [ 0 ],
333 );
334  
335 # New style (create) object session with event to method name map.
336 POE::Session->create(
337 object_states => [
338 MappedObject->new => {
339 _start => 'my_start',
340 count => 'my_count',
341 _stop => 'my_stop',
342 },
343 ],
344 args => [ 1 ],
345 );
346  
347 # New style (create) package session without event to method name map.
348 POE::Session->create(
349 package_states => [
350 UnmappedPackage => [ '_start', 'count', '_stop' ],
351 ],
352 args => [ 2 ],
353 );
354  
355 # New style (create) package session with event to method name map.
356 POE::Session->create(
357 package_states => [
358 MappedPackage => {
359 _start => 'my_start',
360 count => 'my_count',
361 _stop => 'my_stop',
362 },
363 ],
364 args => [ 3 ],
365 );
366  
367 #------------------------------------------------------------------------------
368 # Test changing options
369 POE::Session->create(
370 inline_states => {
371 _start => sub {
372 my $orig = $_[SESSION]->option(default => 1);
373 Test::More::ok($orig, "option original value");
374 my $rv = $_[SESSION]->option('default');
375 Test::More::ok($rv, "set default option successfully");
376 $rv = $_[SESSION]->option('default' => $orig);
377 Test::More::ok($rv, "reset default option successfully");
378 $rv = $_[SESSION]->option('default');
379 Test::More::ok(!($rv xor $orig), "reset default option successfully");
380  
381 $_[KERNEL]->yield("idle");
382 },
383 idle => sub { },
384 _stop => sub { }, # Pacify assertions.
385 },
386 options => { default => 1 },
387 );
388  
389 #------------------------------------------------------------------------------
390 # Test deprecation of new(), test invalid arguments to create()
391 eval { POE::Session->new("foo" => sub { } ) };
392 ok($@ ne '', "new() is deprecated");
393  
394 eval { POE::Session->create("an", "odd", "number", "of", "elephants") };
395 ok($@ ne '', "create() doesn't accept an odd number of args");
396  
397 #------------------------------------------------------------------------------
398 # Main loop.
399  
400 is(
401 $poe_kernel->get_active_session(), $poe_kernel,
402 "get_active_session before POE::Kernel->run()"
403 );
404  
405 POE::Kernel->run();
406  
407 is(
408 $poe_kernel->get_active_session(), $poe_kernel,
409 "get_active_session after POE::Kernel->run()"
410 );
411  
412 #------------------------------------------------------------------------------
413 # Final tests.
414  
415 # Now make sure they've run.
416 for (my $i=0; $i<$machine_count; $i++) {
417 is(
418 $completions[$i], $event_count,
419 "test $i ran"
420 );
421 }
422  
423 # Were all the signals caught?
424 SKIP: {
425 if (($^O eq "MSWin32" or $^O eq "MacOS") and not $ENV{POE_DANTIC}) {
426 skip "$^O does not support signals", 2;
427 }
428  
429 is(
430 $sigalrm_caught, $event_count,
431 "caught enough SIGALRMs"
432 );
433  
434 is(
435 $sigpipe_caught, $event_count,
436 "caught enough SIGPIPEs"
437 );
438 }
439  
440 # Did the postbacks work?
441 ok( $postback_test, "postback test" );
442 ok( $callback_test, "callback test" );
443  
444 # Gratuitous tests to appease the coverage gods.
445 ok(
446 (ARG1 == ARG0+1) && (ARG2 == ARG1+1) && (ARG3 == ARG2+1) &&
447 (ARG4 == ARG3+1) && (ARG5 == ARG4+1) && (ARG6 == ARG5+1) &&
448 (ARG7 == ARG6+1) && (ARG8 == ARG7+1) && (ARG9 == ARG8+1),
449 "ARG constants are good"
450 );
451  
452 is(
453 $sender_count, $machine_count * $event_count,
454 "sender_count"
455 );
456  
457 is(
458 $default_count, $machine_count * $event_count,
459 "default_count"
460 );
461  
462 is(
463 $got_heap_count, $machine_count,
464 "got_heap_count"
465 );
466  
467 # Object/package sessions.
468 is_deeply(
469 \@objpack, [ ($event_count) x 4 ],
470 "object/package session event count"
471 );
472  
473 my $sessions_destroyed = 0;
474 my $objects_destroyed = 0;
475 my $stop_called = 0;
476 my $parent_called = 0;
477 my $child_called = 0;
478  
479 package
480 POE::MySession;
481  
482 use vars qw(@ISA);
483  
484 use POE::Session;
485 @ISA = qw(POE::Session);
486  
487 sub DESTROY {
488 $_[0]->SUPER::DESTROY;
489 $sessions_destroyed++;
490 }
491  
492 package
493 MyObject;
494  
495 sub new { bless {} }
496 sub DESTROY { $objects_destroyed++ }
497  
498 package
499 main;
500  
501 POE::MySession->create(
502 inline_states => {
503 _start => sub {
504 $_[HEAP]->{object} = MyObject->new;
505 POE::MySession->create(
506 inline_states => {
507 _start => sub {
508 $_[HEAP]->{object} = MyObject->new;
509 POE::MySession->create(
510 inline_states => {
511 _start => sub {
512 $_[HEAP]->{object} = MyObject->new;
513 POE::MySession->create(
514 inline_states => {
515 _start => sub {
516 $_[HEAP]->{object} = MyObject->new;
517 $_[KERNEL]->delay(nonexistent => 3600);
518 $_[KERNEL]->alias_set('test4');
519 },
520 _parent => sub {
521 $parent_called++;
522 },
523 _child => sub { }, # To shush ASSERT
524 _stop => sub {
525 $stop_called++;
526 },
527 },
528 );
529 $_[KERNEL]->delay(nonexistent => 3600);
530 $_[KERNEL]->alias_set('test3');
531 },
532 _parent => sub {
533 $parent_called++;
534 },
535 _child => sub {
536 $child_called++ if $_[ARG0] eq 'lose';
537 },
538 _stop => sub {
539 $stop_called++;
540 },
541 },
542 );
543 $_[KERNEL]->delay(nonexistent => 3600);
544 $_[KERNEL]->alias_set('test2');
545 },
546 _parent => sub {
547 $parent_called++;
548 },
549 _child => sub {
550 $child_called++ if $_[ARG0] eq 'lose';
551 },
552 _stop => sub {
553 $stop_called++;
554 },
555 },
556 );
557 $_[KERNEL]->delay(nonexistent => 3600);
558 $_[KERNEL]->alias_set('test1');
559 $_[KERNEL]->yield("stop");
560 },
561 _parent => sub {
562 $parent_called++;
563 },
564 _child => sub {
565 $child_called++ if $_[ARG0] eq 'lose';
566 },
567 _stop => sub {
568 $stop_called++;
569 },
570 stop => sub {
571 POE::Kernel->stop();
572  
573 my $expected;
574 if ($] >= 5.004 and $] < 5.00405) {
575 diag( "Note: We find your choice of Perl versions disturbing" );
576 diag( "primarily due to the number of bugs POE triggers within" );
577 diag( "it. You should seriously consider upgrading." );
578 $expected = 0;
579 }
580 else {
581 $expected = 3;
582 }
583  
584 is(
585 $sessions_destroyed, $expected,
586 "$sessions_destroyed sessions destroyed (expected $expected)"
587 );
588  
589 # 5.004 and 5.005 have some nasty gc issues. Near as I can tell,
590 # data inside the heap is surviving the session DESTROY. This
591 # isn't possible in a sane and normal world. So if this is giving
592 # you fits, consider it a sign that your "legacy perl" fetish is
593 # bizarre and harmful.
594 if ($] >= 5.006 or ($] >= 5.004 and $] < 5.00405)) {
595 $expected = 3;
596 } else {
597 $expected = 2;
598 diag("Detected a memory leak in Perl version $].");
599 diag("Please consider upgrading if you use Perl in production.");
600 }
601  
602 is(
603 $objects_destroyed, $expected,
604 "$objects_destroyed objects destroyed (expected $expected)"
605 );
606 }
607 }
608 );
609  
610 POE::Kernel->run();
611  
612 is(
613 $stop_called, 4,
614 "_stop was called the correct number of times"
615 );
616  
617 is(
618 $child_called, 3,
619 "_child wasn't called"
620 );
621  
622 is(
623 $parent_called, 0,
624 "_parent wasn't called"
625 );
626  
627 my $expected;
628 if ($] >= 5.004 and $] < 5.00405) {
629 diag( "Seriously. We've had to create special cases just to cater" );
630 diag( "to your freakish 'legacy buggy perl' fetish. Consider upgrading" );
631 $expected = 0;
632 }
633 else {
634 $expected = 4;
635 }
636  
637 is(
638 $sessions_destroyed, $expected,
639 "destroyed $sessions_destroyed sessions (expected $expected)"
640 );
641  
642 # 5.004 and 5.005 have some nasty gc issues. Near as I can tell,
643 # data inside the heap is surviving the session DESTROY. This
644 # isn't possible in a sane and normal world.
645 if($] >= '5.006') {
646 $expected = 4;
647 }
648 elsif ($] == 5.005_04 or $] == 5.004_05) {
649 $expected = 3;
650 diag( "Here's yet another special test case to work around memory" );
651 diag( "leaks in Perl $]." );
652 }
653 else {
654 $expected = 4;
655 }
656  
657 is(
658 $objects_destroyed, $expected,
659 "destroyed $objects_destroyed objects (expected $expected)"
660 );
661  
662 # This simple session just makes sure we can start another Session and
663 # another Kernel. If all goes well, it'll dispatch some events and
664 # exit normally.
665  
666 # The restart test dumps core when using Tk with Perl 5.8.0 and
667 # beyond, but only if they're built without threading support. It
668 # happens consistently in a pure Tk test case. It happens
669 # consistently in POE's "make test" suite. It doesn't happen at all
670 # when running the test by hand.
671 #
672 # http://rt.cpan.org/Ticket/Display.html?id=8588 is tracking the Tk
673 # test case. Wish us luck there.
674 #
675 # Meanwhile, these tests will be skipped under Tk if Perl is 5.8.0 or
676 # beyond, and it's not built for threading.
677  
678 SKIP: {
679 # use Config;
680 # skip "Restarting Tk dumps core in single-threaded perl $]", 6 if (
681 # $] >= 5.008 and
682 # exists $INC{"Tk.pm"} and
683 # !$Config{useithreads}
684 # );
685  
686 POE::Session->create(
687 options => { trace => 1, default => 1, debug => 1 },
688 inline_states => {
689 _start => sub {
690 pass("restarted event loop session _start");
691 $_[KERNEL]->yield("woot");
692 $_[KERNEL]->delay(narf => 1);
693 },
694 woot => sub {
695 pass("restarted event loop session yield()");
696 },
697 narf => sub {
698 pass("restarted event loop session timer delay()");
699 },
700 _stop => sub {
701 pass("restarted event loop session _stop");
702 },
703 }
704 );
705  
706 POE::Kernel->run();
707 pass("restarted event loop returned normally");
708 }
709  
710 1;