Browse files

Add POE::Test::Sequence::create_generic_session().

A generic session runs all the events it receives through the test
sequence.  It seems like something that might be common for simple
tests.

t/90_regression/leolo-alarm-adjust.t uses it as a proof of concept, a
future documentation example, and to shave about 2sec off the test.
  • Loading branch information...
1 parent 903492a commit d00cd369b9f12df5f2b59bf91807243a72fe189a @rcaputo committed Jul 12, 2014
Showing with 77 additions and 33 deletions.
  1. +30 −2 lib/POE/Test/Sequence.pm
  2. +47 −31 t/90_regression/leolo-alarm-adjust.t
View
32 lib/POE/Test/Sequence.pm
@@ -4,6 +4,7 @@ use warnings;
use strict;
use Carp qw(croak);
+use POE;
sub new {
my ($class, %args) = @_;
@@ -41,6 +42,17 @@ sub test_count {
return $_[0]{test_count};
}
+sub create_generic_session {
+ my ($self) = @_;
+
+ POE::Session->create(
+ inline_states => {
+ _start => sub { goto $self->next( $_[STATE], 0 ) },
+ _default => sub { goto $self->next( $_[ARG0], 0 ) },
+ }
+ );
+}
+
1;
__END__
@@ -52,7 +64,7 @@ POE::Test::Sequence - POE test helper to verify a sequence of events
=head1 SYNOPSIS
Sorry, there isn't a synopsis at this time.
-
+
However, see t/90_regression/whjackson-followtail.t in POE's test
suite for a full example.
@@ -64,6 +76,20 @@ tedious trickery needed to verify the relative ordering of events.
With this module, one can test the sequence of events without
necessarily relying on specific times elapsing between them.
+=head2 create_generic_session
+
+The create_generic_session() method creates a POE::Session that routes
+all vents through the POE::Test::Sequence object. It returns the
+POE::Session object, but the test program does not need to store it
+anywhere. In fact, it's recommended not to do that without
+understanding the implications.
+
+The implications can be found in the documentation for POE::Kernel and
+POE::Session.
+
+An example of create_generic_session() can be found in
+POE's t/90_regression/leolo-alarm-adjust.t test program.
+
=head2 new
Create a new sequence object. Takes named parameter pairs, currently
@@ -105,7 +131,9 @@ It's intended to be used for test planning.
=head1 BUGS
-None known.
+create_generic_session() is hard-coded to pass only the event name and
+the numeric value 0 to next(). This is fine for only the most generic
+sequences.
=head1 AUTHORS & LICENSING
View
78 t/90_regression/leolo-alarm-adjust.t
@@ -1,40 +1,56 @@
#!/usr/bin/perl
-
+# vim: ts=2 sw=2 expandtab
+#
use strict;
use warnings;
+use Time::HiRes qw(time);
use POE;
-use Test::More ( tests => 4 );
+use Test::More;
+
+use POE::Test::Sequence;
-pass( "BEGIN" );
-POE::Session->create( inline_states => {
- _start => sub {
- my $heap = $_[HEAP];
- $heap->{started} = time;
- $heap->{alarm} = $poe_kernel->alarm_set( 'the_alarm' => time+10 );
- $heap->{delay} = $poe_kernel->delay_set( 'the_delay' => 10 );
- $poe_kernel->yield( 'adjust_them' );
- },
- adjust_them => sub {
- my $heap = $_[HEAP];
- $poe_kernel->delay_adjust( $heap->{delay}, 3 ); # 3 seconds from now
- $poe_kernel->alarm_adjust( $heap->{alarm}, -7 ); # 10-7 seconds
- diag( "Waiting 3 seconds (or 10)" );
- },
+my $sequence = POE::Test::Sequence->new(
+ sequence => [
+ [
+ '_start', 0, sub {
+ my $heap = $_[HEAP];
+ my $now = $heap->{started} = time();
+ $heap->{alarm} = POE::Kernel->alarm_set( 'the_alarm' => $now+10 );
+ $heap->{delay} = POE::Kernel->delay_set( 'the_delay' => 10 );
+ POE::Kernel->yield( 'adjust_them' );
+ },
+ ],
+ [
+ 'adjust_them', 0, sub {
+ my $heap = $_[HEAP];
+ POE::Kernel->delay_adjust( $heap->{delay}, 1 ); # 1 seconds from now
+ POE::Kernel->alarm_adjust( $heap->{alarm}, -9 ); # 10-9 seconds
+ note( "Waiting 1 second (or 10)" );
+ },
+ ],
+ [
+ 'the_alarm', 0, sub {
+ my $heap = $_[HEAP];
+ my $took = time() - $heap->{started};
+ ok( $took < 2, "Short alarm ($took)" );
+ },
+ ],
+ [
+ 'the_delay', 0, sub {
+ my $heap = $_[HEAP];
+ my $took = time() - $heap->{started};
+ ok( $took < 2, "Short delay ($took)" );
+ },
+ ],
+ [ '_stop', 0, undef ],
+ ],
+);
- the_delay => sub {
- my $heap = $_[HEAP];
- my $took = time - $heap->{started};
- ok( $took < 5, "Short delay ($took)" );
- },
- the_alarm => sub {
- my $heap = $_[HEAP];
- my $took = time - $heap->{started};
- ok( $took < 5, "Short alarm ($took)" );
- },
- } );
-
-$poe_kernel->run;
+# Two additional tests for short delays.
+plan tests => $sequence->test_count() + 2;
-pass( "END" );
+$sequence->create_generic_session();
+POE::Kernel->run();
+exit;

0 comments on commit d00cd36

Please sign in to comment.