« blib/lib/POE/Test/Loops/wheel_readline.pm
↑ Summary
blib/lib/POE/Test/Loops/comp_tcp_concurrent.pm »
| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use IO::File; | ||
| 7 | |||
| 8 | use Test::More tests => 28; | ||
| 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 | use POE qw(Filter::Map Driver::SysRW Pipe::TwoWay); | ||
| 18 | |||
| 19 | sub DEBUG () { 0 } | ||
| 20 | |||
| 21 | use_ok('POE::Wheel::ReadWrite'); | ||
| 22 | can_ok('POE::Wheel::ReadWrite', | ||
| 23 | qw( new put event set_filter set_input_filter set_output_filter | ||
| 24 | set_high_mark set_low_mark get_driver_out_octets get_driver_out_messages | ||
| 25 | ID pause_input resume_input shutdown_input shutdown_output )); | ||
| 26 | |||
| 27 | # checks new() fails appropriately | ||
| 28 | sub test_new { | ||
| 29 | my ($name, @args) = @_; | ||
| 30 | eval { POE::Wheel::ReadWrite->new(@args) }; | ||
| 31 | ok($@ ne '', $name); | ||
| 32 | } | ||
| 33 | |||
| 34 | # Part 0 - Dispatch tests {{{ | ||
| 35 | sub test_dispatcher { | ||
| 36 | my @tests = ( \&part1, \&part2, \&part3 ); | ||
| 37 | POE::Session->create( | ||
| 38 | inline_states => { | ||
| 39 | _start => sub { | ||
| 40 | $_[KERNEL]->yield('run_next'); | ||
| 41 | $_[KERNEL]->alias_set('test_dispatcher'); | ||
| 42 | }, | ||
| 43 | run_next => sub { | ||
| 44 | if (@tests) { | ||
| 45 | warn "dispatching $tests[0]" if DEBUG; | ||
| 46 | eval { (shift @tests)->() }; | ||
| 47 | if ($@) { warn $@; exit 1; } | ||
| 48 | # POE isn't very good at dieing hard | ||
| 49 | } else { | ||
| 50 | $_[KERNEL]->alias_remove('test_dispatcher'); | ||
| 51 | } | ||
| 52 | }, | ||
| 53 | _child => sub { | ||
| 54 | if ($_[ARG0] eq 'lose') { | ||
| 55 | delete $_[HEAP]->{$_[ARG1]->ID}; | ||
| 56 | $_[KERNEL]->yield('run_next') unless keys %{$_[HEAP]}; | ||
| 57 | } else { | ||
| 58 | $_[HEAP]->{$_[ARG1]->ID}++; | ||
| 59 | } | ||
| 60 | }, | ||
| 61 | _stop => sub { }, | ||
| 62 | }, | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | # }}} | ||
| 66 | |||
| 67 | # Appendix 1 - Mock/Proxy Driver {{{ | ||
| 68 | { | ||
| 69 | package | ||
| 70 | MockDriver; | ||
| 71 | # Those readers interested in good practice should see Test::MockObject | ||
| 72 | use vars qw($AUTOLOAD); | ||
| 73 | sub SELF_DRIVER () { 0 } | ||
| 74 | sub SELF_CALLED () { 1 } | ||
| 75 | sub new { | ||
| 76 | my ($class, $driver) = @_; | ||
| 77 | return bless [$driver, {}], $class; | ||
| 78 | } | ||
| 79 | sub mock_called { | ||
| 80 | my ($self, $meth) = @_; | ||
| 81 | return $self->[SELF_CALLED]->{$meth}; | ||
| 82 | } | ||
| 83 | sub DESTROY { } | ||
| 84 | sub AUTOLOAD { | ||
| 85 | my $self = shift; | ||
| 86 | $AUTOLOAD =~ s/^MockDriver:://; | ||
| 87 | my $meth = $self->[SELF_DRIVER]->can($AUTOLOAD); | ||
| 88 | $self->[SELF_CALLED]->{$AUTOLOAD}++; | ||
| 89 | unshift @_, $self->[SELF_DRIVER]; | ||
| 90 | goto &$meth; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | # }}} | ||
| 94 | |||
| 95 | # Part 1 - Check new() {{{ | ||
| 96 | sub part1 { | ||
| 97 | POE::Session->create( | ||
| 98 | inline_states => { | ||
| 99 | _start => sub { | ||
| 100 | test_new("new(): no args"); | ||
| 101 | test_new("new(): handles for both directions", InputHandle => \*STDIN); | ||
| 102 | local $SIG{__WARN__} = sub {}; | ||
| 103 | test_new("new(): passing kernel deprecated", $poe_kernel); | ||
| 104 | test_new("new(): both marks must be given", | ||
| 105 | Handle => \*DATA, HighMark => 5, | ||
| 106 | HighEvent => 'high', LowEvent => 'low'); | ||
| 107 | test_new("new(): both marks must be given", | ||
| 108 | Handle => \*DATA, LowMark => 5, | ||
| 109 | HighEvent => 'high', LowEvent => 'low'); | ||
| 110 | test_new("new(): both marks must be valid", | ||
| 111 | Handle => \*DATA, LowMark => 5, HighMark => -1, | ||
| 112 | HighEvent => 'high', LowEvent => 'low'); | ||
| 113 | test_new("new(): both marks must be valid", | ||
| 114 | Handle => \*DATA, LowMark => -1, HighMark => 5, | ||
| 115 | HighEvent => 'high', LowEvent => 'low'); | ||
| 116 | test_new("new(): both marks must be valid", | ||
| 117 | Handle => \*DATA, LowMark => -1, HighMark => -1, | ||
| 118 | HighEvent => 'high', LowEvent => 'low'); | ||
| 119 | test_new("new(): both mark events needed", | ||
| 120 | Handle => \*DATA, LowMark => 3, HighMark => 8, | ||
| 121 | HighEvent => 'high'); | ||
| 122 | test_new("new(): both mark events needed", | ||
| 123 | Handle => \*DATA, LowMark => 3, HighMark => 8, | ||
| 124 | LowEvent => 'low'); | ||
| 125 | test_new("new(): mark events need levels", | ||
| 126 | Handle => \*DATA, HighEvent => 'high'); | ||
| 127 | test_new("new(): mark events need levels", | ||
| 128 | Handle => \*DATA, LowEvent => 'low'); | ||
| 129 | test_new("new(): mark events need levels", | ||
| 130 | Handle => \*DATA, LowEvent => 'low', HighEvent => 'high'); | ||
| 131 | }, | ||
| 132 | _stop => sub { }, | ||
| 133 | }, | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | # }}} | ||
| 137 | |||
| 138 | # Part 2 - Check filter handling {{{ | ||
| 139 | my $TMPDATA = <<"END"; | ||
| 140 | TMPDATA 12345 | ||
| 141 | TMPDATA ABCDE | ||
| 142 | $$ $< $> $] | ||
| 143 | END | ||
| 144 | my $TMPDATA_LINES = () = $TMPDATA =~ m/\n/g; | ||
| 145 | |||
| 146 | sub part2 { | ||
| 147 | my $tmpfile = IO::File->new_tmpfile(); | ||
| 148 | die "Couldn't create temporary file" unless defined $tmpfile; | ||
| 149 | print $tmpfile $TMPDATA; | ||
| 150 | seek $tmpfile, 0, 0 or | ||
| 151 | do { print STDERR "seek failed: $!"; exit 1 }; | ||
| 152 | |||
| 153 | if (exists $INC{'Tk.pm'}) { | ||
| 154 | SKIP: { | ||
| 155 | skip( "part2 doesn't work with Tk", 13 ); | ||
| 156 | } | ||
| 157 | $poe_kernel->post("test_dispatcher" => "run_next"); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | elsif ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) { | ||
| 161 | SKIP: { | ||
| 162 | skip( "part2 doesn't work on windows", 13 ); | ||
| 163 | } | ||
| 164 | $poe_kernel->post("test_dispatcher" => "run_next"); | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | POE::Session->create( | ||
| 169 | inline_states => { | ||
| 170 | _start => sub { | ||
| 171 | $_[HEAP]->{fh} = $tmpfile; | ||
| 172 | $_[HEAP]->{driver} = MockDriver->new(POE::Driver::SysRW->new); | ||
| 173 | $_[HEAP]->{wheel} = POE::Wheel::ReadWrite->new( | ||
| 174 | Handle => $tmpfile, | ||
| 175 | Driver => $_[HEAP]->{driver}, | ||
| 176 | LowMark => 1, HighMark => 12, | ||
| 177 | InputEvent => 'wrong_input', | ||
| 178 | FlushedEvent => 'wrong_flushed', | ||
| 179 | ErrorEvent => 'wrong_error', | ||
| 180 | HighEvent => 'wrong_high', | ||
| 181 | LowEvent => 'wrong_low', | ||
| 182 | ); | ||
| 183 | $_[HEAP]->{wheel_id} = $_[HEAP]->{wheel}->ID; | ||
| 184 | |||
| 185 | # start state machines | ||
| 186 | $_[HEAP]->{read_machine} = "start"; | ||
| 187 | $_[HEAP]->{write_machine} = "start"; | ||
| 188 | |||
| 189 | # try changing all the events | ||
| 190 | $_[HEAP]->{wheel}->event( | ||
| 191 | InputEvent => 'input', | ||
| 192 | FlushedEvent => 'flushed', | ||
| 193 | ErrorEvent => 'error', | ||
| 194 | HighEvent => 'high', | ||
| 195 | LowEvent => 'low', | ||
| 196 | ); | ||
| 197 | }, | ||
| 198 | resume_input => sub { $_[HEAP]->{wheel}->resume_input }, | ||
| 199 | input => \&part2_input, | ||
| 200 | flushed => \&part2_flushed, | ||
| 201 | error => \&part2_handle, | ||
| 202 | high => \&part2_high, | ||
| 203 | low => \&part2_low, | ||
| 204 | wrong_input => \&part2_wrong, | ||
| 205 | wrong_flushed => \&part2_wrong, | ||
| 206 | wrong_error => \&part2_wrong, | ||
| 207 | wrong_high => \&part2_wrong, | ||
| 208 | wrong_low => \&part2_wrong, | ||
| 209 | _stop => \&part2_stop, | ||
| 210 | }, | ||
| 211 | ); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub part2_wrong { | ||
| 215 | print STDERR "$_[STATE] called unexpectedly"; | ||
| 216 | exit 1; | ||
| 217 | } | ||
| 218 | |||
| 219 | # two phases, first we do reads w/ pauses, then we do writes w/ pauses. | ||
| 220 | |||
| 221 | sub part2_input { | ||
| 222 | my ($heap, $input, $id) = @_[HEAP, ARG0, ARG1]; | ||
| 223 | |||
| 224 | $heap->{read_lines}++; | ||
| 225 | $heap->{"called_input"}++; | ||
| 226 | $heap->{"wrong_id"}++ if $id != $heap->{wheel_id}; | ||
| 227 | unless ($heap->{check_filters}++) { | ||
| 228 | &part2_check_filters; | ||
| 229 | } | ||
| 230 | |||
| 231 | if ($heap->{read_machine} eq "start") { | ||
| 232 | $heap->{wheel}->pause_input; | ||
| 233 | $heap->{read_machine} = "paused"; | ||
| 234 | } elsif ($heap->{read_machine} eq 'paused') { | ||
| 235 | if ($heap->{read_lines} == $TMPDATA_LINES) { | ||
| 236 | # we've reached EOF while paused | ||
| 237 | seek $heap->{fh}, 0, 0 or | ||
| 238 | do { print STDERR "seek failed: $!"; exit 1 }; | ||
| 239 | $heap->{read_machine} = "paused+reset"; | ||
| 240 | $_[KERNEL]->delay('resume_input', 0.5); | ||
| 241 | } | ||
| 242 | } elsif ($heap->{read_machine} eq 'paused+reset') { | ||
| 243 | # reading started again! | ||
| 244 | if ($heap->{read_lines} >= 2*$TMPDATA_LINES) { | ||
| 245 | $heap->{read_machine} = "stop"; | ||
| 246 | if ($heap->{write_machine} eq "start") { | ||
| 247 | $heap->{wheel}->put("LINE 1"); | ||
| 248 | $heap->{write_machine} = "line2"; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | } else { | ||
| 252 | warn "read machine state == $heap->{read_machine}"; | ||
| 253 | delete $heap->{wheel}; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | sub part2_flushed { | ||
| 258 | my ($heap, $id) = @_[HEAP, ARG0]; | ||
| 259 | $heap->{called_flushed}++; | ||
| 260 | $heap->{wrong_id}++ if $id != $heap->{wheel_id}; | ||
| 261 | |||
| 262 | if ($heap->{write_machine} eq "line2") { | ||
| 263 | $heap->{wheel}->put("LINE 2"); | ||
| 264 | $heap->{wheel}->put("LINE 3"); | ||
| 265 | $heap->{wheel}->put("LINE 4"); | ||
| 266 | $heap->{write_machine} = "line5"; | ||
| 267 | } elsif ($heap->{write_machine} eq "line5") { | ||
| 268 | # $heap->{wheel}->set_high_mark(550); | ||
| 269 | # $heap->{wheel}->set_low_mark(500); | ||
| 270 | |||
| 271 | $heap->{wheel}->put("LINE 5"); | ||
| 272 | $heap->{wheel}->put("LINE 6"); | ||
| 273 | $heap->{wheel}->put("LINE 7"); | ||
| 274 | $heap->{write_machine} = "delete"; | ||
| 275 | } elsif ($heap->{write_machine} eq "delete") { | ||
| 276 | $heap->{write_machine} = "stop"; | ||
| 277 | $heap->{wheel}->shutdown_input; | ||
| 278 | $heap->{wheel}->shutdown_output; | ||
| 279 | delete $heap->{wheel}; | ||
| 280 | } else { | ||
| 281 | warn "write machine state == $heap->{write_machine}"; | ||
| 282 | delete $heap->{wheel}; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | sub part2_high { | ||
| 287 | my $heap = $_[HEAP]; | ||
| 288 | $heap->{called_high}++; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub part2_low { | ||
| 292 | my $heap = $_[HEAP]; | ||
| 293 | if ($heap->{write_machine} eq 'delete') { | ||
| 294 | $heap->{low_not_set}++; | ||
| 295 | } | ||
| 296 | $heap->{called_low}++; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub part2_check_filters { | ||
| 300 | isa_ok($_[HEAP]->{wheel}->get_input_filter, 'POE::Filter', | ||
| 301 | "input filter isa POE::Filter"); | ||
| 302 | isa_ok($_[HEAP]->{wheel}->get_output_filter, 'POE::Filter', | ||
| 303 | "output filter isa POE::Filter"); | ||
| 304 | } | ||
| 305 | |||
| 306 | sub part2_handle { | ||
| 307 | $_[HEAP]->{"called_$_[STATE]"}++; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub part2_stop { | ||
| 311 | my $heap = $_[HEAP]; | ||
| 312 | # the post-mortem - check that things we expected to happen, happened | ||
| 313 | ok($heap->{called_input}, "input event happened"); | ||
| 314 | ok($heap->{called_flushed}, "flushed event happened"); | ||
| 315 | ok($heap->{called_error}, "error event happened"); | ||
| 316 | # ok($heap->{called_high}, "high event happened"); | ||
| 317 | ok($heap->{called_low}, "low event happened"); | ||
| 318 | # ok(!$heap->{low_not_set}, "low mark successfully changed"); | ||
| 319 | ok($heap->{driver}->mock_called('get'), "driver's get called"); | ||
| 320 | ok($heap->{driver}->mock_called('put'), "driver's put called"); | ||
| 321 | ok($heap->{driver}->mock_called('flush'), "driver's flush called"); | ||
| 322 | ok(!$heap->{wrong_id}, "correct wheel id consistently used"); | ||
| 323 | is($heap->{read_lines}, 2*$TMPDATA_LINES, "correct number of lines read"); | ||
| 324 | is($heap->{read_machine}, "stop", "read state machine finished"); | ||
| 325 | is($heap->{write_machine}, "stop", "write state machine finished"); | ||
| 326 | } | ||
| 327 | # }}} | ||
| 328 | |||
| 329 | # Part 3 - Changing watermarks (testing with a pipe) {{{ | ||
| 330 | sub part3 { | ||
| 331 | POE::Session->create( | ||
| 332 | inline_states => { _start => sub { }, _stop => sub { } }, | ||
| 333 | ); | ||
| 334 | return; # skip | ||
| 335 | |||
| 336 | # create the pipe | ||
| 337 | my ($a_read, $a_write, $b_read, $b_write) = POE::Pipe::TwoWay->new("inet"); | ||
| 338 | # flow is $b_write --> $a_read | ||
| 339 | |||
| 340 | # the two session IDs | ||
| 341 | my ($sender, $receiver); | ||
| 342 | |||
| 343 | # sender | ||
| 344 | POE::Session->create( | ||
| 345 | inline_states => { | ||
| 346 | problem => sub { | ||
| 347 | diag("problem in part3 sender!"); | ||
| 348 | $_[HEAP]->{wheel} = $_[HEAP]->{fh} = undef; | ||
| 349 | }, | ||
| 350 | _start => sub { | ||
| 351 | $sender = $_[SESSION]->ID; | ||
| 352 | |||
| 353 | $_[HEAP]->{wheel} = POE::Wheel::ReadWrite->new( | ||
| 354 | Handle => $b_write, | ||
| 355 | HighMark => 1, | ||
| 356 | LowMark => 1, | ||
| 357 | FlushedEvent => 'flushed', | ||
| 358 | HighEvent => 'high', | ||
| 359 | LowEvent => 'low', | ||
| 360 | ErrorEvent => 'problem', | ||
| 361 | ); | ||
| 362 | |||
| 363 | # now boost the watermarks much higher | ||
| 364 | $_[HEAP]->{wheel}->set_high_mark(512); | ||
| 365 | $_[HEAP]->{wheel}->set_low_mark(32); | ||
| 366 | |||
| 367 | $_[HEAP]->{state} = "start"; | ||
| 368 | $_[HEAP]->{wheel}->put("start"); | ||
| 369 | }, | ||
| 370 | second => sub { | ||
| 371 | is($_[HEAP]->{state}, "start", "sender: start --> second"); | ||
| 372 | $_[HEAP]->{state} = "second"; | ||
| 373 | $_[KERNEL]->yield("second_send"); | ||
| 374 | }, | ||
| 375 | second_send => sub { | ||
| 376 | if ($_[HEAP]->{state} eq "second") { | ||
| 377 | $_[HEAP]->{wheel}->put("\0" x (1024*1024)); | ||
| 378 | $_[KERNEL]->yield("second_send"); | ||
| 379 | } | ||
| 380 | }, | ||
| 381 | flushed => sub { }, #print "flushed\n" }, | ||
| 382 | high => sub { print "high\n"; $_[HEAP]->{state} = "high"; }, | ||
| 383 | low => sub { print "low\n" }, | ||
| 384 | _stop => sub { }, | ||
| 385 | }, | ||
| 386 | ); | ||
| 387 | |||
| 388 | # receiver | ||
| 389 | POE::Session->create( | ||
| 390 | inline_states => { | ||
| 391 | problem => sub { | ||
| 392 | diag("problem in part3 receiver!"); | ||
| 393 | $_[HEAP]->{wheel} = $_[HEAP]->{fh} = undef; | ||
| 394 | }, | ||
| 395 | _start => sub { | ||
| 396 | $receiver = $_[SESSION]->ID; | ||
| 397 | |||
| 398 | $_[HEAP]->{wheel} = POE::Wheel::ReadWrite->new( | ||
| 399 | Handle => $a_read, | ||
| 400 | InputEvent => 'input', | ||
| 401 | ErrorEvent => 'problem', | ||
| 402 | ); | ||
| 403 | |||
| 404 | $_[HEAP]->{state} = "start"; | ||
| 405 | }, | ||
| 406 | input => sub { | ||
| 407 | my ($heap, $line) = @_[HEAP, ARG0]; | ||
| 408 | my $state = $heap->{state}; | ||
| 409 | if ($state eq "start") { | ||
| 410 | is($line, "start", "first line ok"); | ||
| 411 | $heap->{state} = "second"; | ||
| 412 | $heap->{wheel}->pause_input(); | ||
| 413 | $_[KERNEL]->post($sender, "second"); | ||
| 414 | } elsif ($state eq "second") { | ||
| 415 | is($line, "second", "second line ok"); | ||
| 416 | } else { | ||
| 417 | warn "weird receive state $_[HEAP]->{state}"; | ||
| 418 | delete $heap->{wheel}; | ||
| 419 | } | ||
| 420 | }, | ||
| 421 | _stop => sub { }, | ||
| 422 | }, | ||
| 423 | ); | ||
| 424 | } | ||
| 425 | # }}} | ||
| 426 | |||
| 427 | # Start it all off | ||
| 428 | test_dispatcher(); | ||
| 429 | $poe_kernel->run(); | ||
| 430 | |||
| 431 | 1; |