« blib/lib/POE/Test/Loops/wheel_tail.pm
↑ Summary
blib/lib/POE/Test/Loops/z_rt53302_fh_watchers.pm »
| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Tests NFA sessions. | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use lib qw(./mylib ../mylib); | ||
| 8 | |||
| 9 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 10 | |||
| 11 | BEGIN { | ||
| 12 | package | ||
| 13 | POE::Kernel; | ||
| 14 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 15 | } | ||
| 16 | |||
| 17 | use Test::More; | ||
| 18 | |||
| 19 | use POE qw(NFA); | ||
| 20 | my $NEW_POE; | ||
| 21 | BEGIN { | ||
| 22 | # fixes Argument "1.294_272" isn't numeric in numeric le | ||
| 23 | my $poe_ver = eval("\$POE::VER" . "SION"); # Pesky CPAN indexer. | ||
| 24 | $poe_ver = $1 if $poe_ver =~ /^(?:[^_]+)\_/; | ||
| 25 | |||
| 26 | if ($poe_ver <= 1.003) { | ||
| 27 | $NEW_POE = 0; | ||
| 28 | plan tests => 28; | ||
| 29 | } else { | ||
| 30 | $NEW_POE = 1; | ||
| 31 | plan tests => 39; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | ### Plain NFA. This simulates a pushbutton that toggles a light. | ||
| 36 | ### This goes in its own package because POE::Session and POE::NFA | ||
| 37 | ### export conflicting constants. | ||
| 38 | |||
| 39 | package | ||
| 40 | Switch; | ||
| 41 | use POE::NFA; | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $class = shift; | ||
| 45 | |||
| 46 | return bless {}, $class; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _default { | ||
| 50 | 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub off_enter { | ||
| 54 | Test::More::is($_[OBJECT], 'Switch', '$_[OBJECT] is a package') | ||
| 55 | if ($NEW_POE); | ||
| 56 | $_[KERNEL]->post( $_[ARG0] => visibility => 0 ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub off_pushed { | ||
| 60 | $_[MACHINE]->goto_state( on => enter => $_[SENDER] ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub enter { | ||
| 64 | Test::More::isa_ok($_[OBJECT], 'Switch', '$_[OBJECT]') | ||
| 65 | if ($NEW_POE); | ||
| 66 | $_[KERNEL]->post( $_[ARG0] => visibility => 1 ); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub pushed { | ||
| 70 | $_[MACHINE]->goto_state( off => enter => $_[SENDER] ); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $self = Switch->new; | ||
| 74 | |||
| 75 | my $args = { | ||
| 76 | inline_states => { | ||
| 77 | # The initial state, and its start event. Make the switch | ||
| 78 | # visible by name, and start in the 'off' state. | ||
| 79 | initial => { | ||
| 80 | start => sub { | ||
| 81 | Test::More::is($_[OBJECT], undef, 'no object') | ||
| 82 | if ($NEW_POE); | ||
| 83 | $_[KERNEL]->alias_set( 'switch' ); | ||
| 84 | $_[MACHINE]->goto_state( 'off' ); | ||
| 85 | }, | ||
| 86 | _default => \&_default, | ||
| 87 | }, | ||
| 88 | }, | ||
| 89 | }; | ||
| 90 | |||
| 91 | if ($NEW_POE) { | ||
| 92 | $args->{package_states} = { | ||
| 93 | off => [ | ||
| 94 | Switch => { | ||
| 95 | enter => 'off_enter', | ||
| 96 | pushed => 'off_pushed', | ||
| 97 | _default => '_default', | ||
| 98 | }, | ||
| 99 | ], | ||
| 100 | }; | ||
| 101 | $args->{object_states} = { | ||
| 102 | # The light is on. When this state is entered, post a visibility | ||
| 103 | # event at whatever had caused the light to go on. When it's | ||
| 104 | # pushed, have the light go off. | ||
| 105 | on => [ | ||
| 106 | $self => [qw(enter pushed _default)], | ||
| 107 | ], | ||
| 108 | }, | ||
| 109 | } else { | ||
| 110 | $args->{inline_states}->{off} = { | ||
| 111 | enter => \&off_enter, | ||
| 112 | pushed => \&off_pushed, | ||
| 113 | _default => \&_default, | ||
| 114 | }; | ||
| 115 | $args->{inline_states}->{on} = { | ||
| 116 | enter => \&enter, | ||
| 117 | pushed => \&pushed, | ||
| 118 | _default => \&_default, | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | POE::NFA->spawn(%$args) | ||
| 123 | ->goto_state( initial => 'start' ); # enter the initial state | ||
| 124 | |||
| 125 | ### This NFA uses the stop() method. Gabriel Kihlman discovered that | ||
| 126 | ### POE::NFA lags behind POE::Kernel after 0.24, and stop() wasn't | ||
| 127 | ### fixed to use the new _data_ses_free() method of POE::Kernel. | ||
| 128 | |||
| 129 | POE::NFA->spawn( | ||
| 130 | inline_states => { | ||
| 131 | initial => { | ||
| 132 | start => sub { $_[MACHINE]->stop() } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | )->goto_state(initial => 'start'); | ||
| 136 | |||
| 137 | ### A plain session to interact with the switch. It's in its own | ||
| 138 | ### package to avoid conflicting constants. This simulates a causal | ||
| 139 | ### observer who pushes the light's button over and over, watching it | ||
| 140 | ### as it goes on and off. | ||
| 141 | |||
| 142 | package | ||
| 143 | Operator; | ||
| 144 | use POE::Session; | ||
| 145 | |||
| 146 | POE::Session->create( | ||
| 147 | inline_states => { | ||
| 148 | # Start by giving the session a name. This keeps the session | ||
| 149 | # alive while other sessions (the light) operate. Set a test | ||
| 150 | # counter, and yield to the 'push' handler. | ||
| 151 | _start => sub { | ||
| 152 | $_[KERNEL]->alias_set( 'operator' ); | ||
| 153 | $_[KERNEL]->yield( 'push' ); | ||
| 154 | $_[HEAP]->{push_count} = 0; | ||
| 155 | }, | ||
| 156 | # Push the button, and count the button push for testing. | ||
| 157 | push => sub { | ||
| 158 | $_[HEAP]->{push_count}++; | ||
| 159 | $_[KERNEL]->post( switch => 'pushed' ); | ||
| 160 | }, | ||
| 161 | # The light did something observable. Check that its on/off | ||
| 162 | # state matches our expectation. If we need to test some more, | ||
| 163 | # push the button again. | ||
| 164 | visibility => sub { | ||
| 165 | Test::More::ok( | ||
| 166 | ($_[HEAP]->{push_count} & 1) == $_[ARG0], | ||
| 167 | "light state matches expected state" | ||
| 168 | ); | ||
| 169 | $_[KERNEL]->yield( 'push' ) if $_[HEAP]->{push_count} < 10; | ||
| 170 | }, | ||
| 171 | # Dummy handlers to avoid ASSERT_STATES warnings. | ||
| 172 | _stop => sub { 0 }, | ||
| 173 | } | ||
| 174 | ); | ||
| 175 | |||
| 176 | ### This is a Fibonacci number servlet. Post it a request with the F | ||
| 177 | ### number you want, and it calculates and returns it. | ||
| 178 | |||
| 179 | package | ||
| 180 | FibServer; | ||
| 181 | use POE::NFA; | ||
| 182 | |||
| 183 | POE::NFA->spawn( | ||
| 184 | inline_states => { | ||
| 185 | # Set up an alias so that clients can find us. | ||
| 186 | initial => | ||
| 187 | { start => sub { $_[KERNEL]->alias_set( 'server' ); | ||
| 188 | $_[MACHINE]->goto_state( 'listen' ); | ||
| 189 | }, | ||
| 190 | _default => sub { 0 }, | ||
| 191 | }, | ||
| 192 | # Listen for a request. The request includes which Fibonacci | ||
| 193 | # number to return. | ||
| 194 | listen => | ||
| 195 | { request => sub { | ||
| 196 | $_[RUNSTATE]->{client} = $_[SENDER]; | ||
| 197 | $_[MACHINE]->call_state( answer => # return event | ||
| 198 | calculate => # new state | ||
| 199 | start => # new state's entry event | ||
| 200 | $_[ARG0] # F-number to return | ||
| 201 | ); | ||
| 202 | }, | ||
| 203 | answer => sub { | ||
| 204 | $_[KERNEL]->post( delete($_[RUNSTATE]->{client}), 'fib', $_[ARG0] ); | ||
| 205 | }, | ||
| 206 | _default => sub { 0 }, | ||
| 207 | }, | ||
| 208 | calculate => | ||
| 209 | { start => sub { | ||
| 210 | $_[MACHINE]->return_state( 0 ) if $_[ARG0] == 0; | ||
| 211 | $_[MACHINE]->return_state( 1 ) if $_[ARG0] == 1; | ||
| 212 | $_[RUNSTATE]->{f} = [ 0, 1 ]; | ||
| 213 | $_[RUNSTATE]->{n} = 1; | ||
| 214 | $_[RUNSTATE]->{target} = $_[ARG0]; | ||
| 215 | $_[KERNEL]->yield( 'next' ); | ||
| 216 | }, | ||
| 217 | next => sub { | ||
| 218 | $_[RUNSTATE]->{n}++; | ||
| 219 | $_[RUNSTATE]->{f}->[2] = | ||
| 220 | $_[RUNSTATE]->{f}->[0] + $_[RUNSTATE]->{f}->[1]; | ||
| 221 | shift @{$_[RUNSTATE]->{f}}; | ||
| 222 | if ($_[RUNSTATE]->{n} == $_[RUNSTATE]->{target}) { | ||
| 223 | $_[MACHINE]->return_state( $_[RUNSTATE]->{f}->[1] ); | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | $_[KERNEL]->yield( 'next' ); | ||
| 227 | } | ||
| 228 | }, | ||
| 229 | _default => sub { 0 }, | ||
| 230 | }, | ||
| 231 | } | ||
| 232 | )->goto_state( initial => 'start' ); | ||
| 233 | |||
| 234 | ### This is a Fibonacci client. It asks for F numbers and checks the | ||
| 235 | ### responses vs. expectations. | ||
| 236 | |||
| 237 | package | ||
| 238 | FibClient; | ||
| 239 | use POE::Session; | ||
| 240 | |||
| 241 | my $test_number = 11; | ||
| 242 | my @test = ( | ||
| 243 | [ 0, 0 ], | ||
| 244 | [ 1, 1 ], | ||
| 245 | [ 2, 1 ], | ||
| 246 | [ 3, 2 ], | ||
| 247 | [ 4, 3 ], | ||
| 248 | [ 5, 5 ], | ||
| 249 | |||
| 250 | [ 17, 1597 ], | ||
| 251 | [ 23, 28657 ], | ||
| 252 | [ 29, 514229 ], | ||
| 253 | [ 43, 433494437 ], | ||
| 254 | ); | ||
| 255 | |||
| 256 | POE::Session->create( | ||
| 257 | inline_states => { | ||
| 258 | _start => sub { | ||
| 259 | # Set up an alias so we'll stay alive until everything is done. | ||
| 260 | $_[KERNEL]->alias_set( 'client' ); | ||
| 261 | $_[KERNEL]->yield( 'next_test' ); | ||
| 262 | }, | ||
| 263 | next_test => sub { | ||
| 264 | $_[KERNEL]->post( server => request => $test[0]->[0] ); | ||
| 265 | }, | ||
| 266 | fib => sub { | ||
| 267 | Test::More::ok( | ||
| 268 | $_[ARG0] == $test[0]->[1], | ||
| 269 | "fib($test[0]->[0]) returned $_[ARG0] (wanted $test[0]->[1])" | ||
| 270 | ); | ||
| 271 | shift @test; | ||
| 272 | $test_number++; | ||
| 273 | $_[KERNEL]->yield( 'next_test' ) if @test; | ||
| 274 | }, | ||
| 275 | # Dummy handlers to avoid ASSERT_STATES warnings. | ||
| 276 | _stop => sub { 0 }, | ||
| 277 | }, | ||
| 278 | ); | ||
| 279 | |||
| 280 | ### This tests using POE::Kernel->state() with a POE::NFA in the same way | ||
| 281 | ### attaching a wheel to a session does | ||
| 282 | ### Also tests options, and (call|post)backs | ||
| 283 | |||
| 284 | package | ||
| 285 | DynamicStates; | ||
| 286 | use POE::NFA; | ||
| 287 | |||
| 288 | POE::NFA->spawn( | ||
| 289 | inline_states => { | ||
| 290 | initial => { | ||
| 291 | start => sub { | ||
| 292 | $_[KERNEL]->alias_set( 'dynamicstates' ); | ||
| 293 | $_[MACHINE]->goto_state( 'listen', 'send' ); | ||
| 294 | $_[KERNEL]->state("test_wheel_event" => sub { | ||
| 295 | $_[KERNEL]->yield("happened"); | ||
| 296 | } | ||
| 297 | ); | ||
| 298 | |||
| 299 | # test options | ||
| 300 | my $orig = $_[MACHINE]->option(default => 1); | ||
| 301 | my $rv = $_[MACHINE]->option('default'); | ||
| 302 | Test::More::ok($rv, "set default option successfully"); | ||
| 303 | $rv = $_[MACHINE]->option('default' => $orig); | ||
| 304 | Test::More::ok($rv, "reset default option successfully"); | ||
| 305 | $rv = $_[MACHINE]->option('default'); | ||
| 306 | Test::More::ok(!($rv xor $orig), "reset default option successfully"); | ||
| 307 | |||
| 308 | # test (post|call)backs | ||
| 309 | $_[MACHINE]->callback("callback")->(); | ||
| 310 | $_[MACHINE]->postback("postback")->(); | ||
| 311 | }, | ||
| 312 | _default => sub { 0 }, | ||
| 313 | callback => sub { | ||
| 314 | Test::More::pass("POE::NFA::callback"); | ||
| 315 | }, | ||
| 316 | postback => sub { | ||
| 317 | Test::More::fail("POE::NFA::postback"); | ||
| 318 | }, | ||
| 319 | }, | ||
| 320 | listen => { | ||
| 321 | send => sub { | ||
| 322 | $_[KERNEL]->yield("test_wheel_event"); | ||
| 323 | }, | ||
| 324 | happened => sub { | ||
| 325 | Test::More::pass("wheel event happened"); | ||
| 326 | Test::More::is($_[MACHINE]->get_current_state(), $_[STATE], | ||
| 327 | "get_current_state returns the same as \$_[STATE]"); | ||
| 328 | Test::More::is_deeply($_[MACHINE]->get_runstate(), $_[RUNSTATE], | ||
| 329 | "get_runstate returns the same as \$_[RUNSTATE]"); | ||
| 330 | }, | ||
| 331 | callback => sub { | ||
| 332 | Test::More::fail("POE::NFA::callback"); | ||
| 333 | }, | ||
| 334 | postback => sub { | ||
| 335 | Test::More::pass("POE::NFA::postback"); | ||
| 336 | }, | ||
| 337 | }, | ||
| 338 | }, | ||
| 339 | )->goto_state("initial", "start"); | ||
| 340 | |||
| 341 | ### Run everything until it's all done. | ||
| 342 | |||
| 343 | package | ||
| 344 | main; | ||
| 345 | |||
| 346 | POE::Kernel->run(); | ||
| 347 | |||
| 348 | 1; |