| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Tests basic select operations. | ||
| 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 => 17; | ||
| 19 | |||
| 20 | use POE qw(Pipe::OneWay Pipe::TwoWay); | ||
| 21 | |||
| 22 | ### Test parameters. | ||
| 23 | |||
| 24 | my $pair_count = 5; | ||
| 25 | my $chat_count = 5; | ||
| 26 | |||
| 27 | # What to do here? Create ten master sessions that create socket | ||
| 28 | # pairs. Each master session spawns a slave session and gives it the | ||
| 29 | # other end of the pair. The master and slave chat a while, then the | ||
| 30 | # slave exits (odd pairs) or the master exits (even pairs). | ||
| 31 | # Everything should shut down cleanly. | ||
| 32 | |||
| 33 | # We'll use send and recv with small enough packets to avoid worrying | ||
| 34 | # about combining broken datagrams. | ||
| 35 | |||
| 36 | ### Master session. | ||
| 37 | |||
| 38 | sub master_start { | ||
| 39 | my ($kernel, $heap ) = @_[KERNEL, HEAP, ARG0]; | ||
| 40 | |||
| 41 | my ($master_read, $master_write, $slave_read, $slave_write) = | ||
| 42 | POE::Pipe::TwoWay->new(); | ||
| 43 | |||
| 44 | ok( defined($master_read), "master: created two-way pipe for testing" ); | ||
| 45 | |||
| 46 | # Listen on the uplink_read side. | ||
| 47 | $kernel->select_read($master_read, 'input'); | ||
| 48 | |||
| 49 | # Give the other side to a newly spawned session. | ||
| 50 | POE::Session->create( | ||
| 51 | inline_states => { | ||
| 52 | _start => \&slave_start, | ||
| 53 | _stop => \&slave_stop, | ||
| 54 | input => \&slave_got_input, | ||
| 55 | resume => \&slave_resume_read, | ||
| 56 | output => \&slave_put_output, | ||
| 57 | }, | ||
| 58 | args => [ $slave_read, $slave_write ], | ||
| 59 | ); | ||
| 60 | |||
| 61 | # Save some values for later. | ||
| 62 | $heap->{write} = $master_write; | ||
| 63 | $heap->{test_count} = 0; | ||
| 64 | $heap->{queue} = [ ]; | ||
| 65 | |||
| 66 | # Start the write thing. | ||
| 67 | $kernel->select_write($master_write, 'output'); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub master_stop { | ||
| 71 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 72 | |||
| 73 | # Determine if we were successful. | ||
| 74 | ok( | ||
| 75 | $heap->{test_count} == $chat_count, | ||
| 76 | "master: expected number of messages" | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub master_got_input { | ||
| 81 | my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0]; | ||
| 82 | |||
| 83 | my $received = sysread($handle, my $buffer = '', 4); | ||
| 84 | unless ($received == 4) { | ||
| 85 | $kernel->select_read($handle); | ||
| 86 | $kernel->select_write($heap->{write}); | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | # The other session requested a quit. Shut down gracefully. | ||
| 91 | if ($buffer eq 'quit') { | ||
| 92 | $kernel->select_read($handle); | ||
| 93 | $kernel->select_write($heap->{write}); | ||
| 94 | return; | ||
| 95 | } | ||
| 96 | |||
| 97 | # The other session sent a ping. Count it, and send a pong. | ||
| 98 | if ($buffer eq 'ping') { | ||
| 99 | $heap->{test_count}++; | ||
| 100 | push @{$heap->{queue}}, 'pong'; | ||
| 101 | $kernel->select_resume_write($heap->{write}); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub master_put_output { | ||
| 106 | my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0]; | ||
| 107 | |||
| 108 | # If there is a message queued, write it. | ||
| 109 | if (@{$heap->{queue}}) { | ||
| 110 | my $message = shift @{$heap->{queue}}; | ||
| 111 | die $! unless ( | ||
| 112 | syswrite($handle, $message, length($message)) == length($message) | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | # Otherwise pause the write select. | ||
| 117 | else { | ||
| 118 | $kernel->select_pause_write($handle); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | ### Slave session. | ||
| 123 | |||
| 124 | sub slave_start { | ||
| 125 | my ($kernel, $heap, $read_handle, $write_handle, $test_index) = | ||
| 126 | @_[KERNEL, HEAP, ARG0, ARG1, ARG2]; | ||
| 127 | |||
| 128 | # Select on our read handle. | ||
| 129 | $kernel->select_read($read_handle, 'input'); | ||
| 130 | |||
| 131 | # Remember some things. | ||
| 132 | $heap->{read} = $read_handle; | ||
| 133 | $heap->{write} = $write_handle; | ||
| 134 | $heap->{test_index} = $test_index; | ||
| 135 | $heap->{queue} = [ ]; | ||
| 136 | |||
| 137 | # Say hello to the master session. | ||
| 138 | push @{$heap->{queue}}, 'ping'; | ||
| 139 | $kernel->select_write($write_handle, 'output'); | ||
| 140 | } | ||
| 141 | |||
| 142 | sub slave_stop { | ||
| 143 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 144 | |||
| 145 | # Determine if we were successful. | ||
| 146 | ok( | ||
| 147 | $heap->{test_count} == $chat_count, | ||
| 148 | "slave: expected number of messages" | ||
| 149 | ); | ||
| 150 | } | ||
| 151 | |||
| 152 | # Resume reading after a brief delay. | ||
| 153 | sub slave_resume_read { | ||
| 154 | $_[KERNEL]->select_resume_read( $_[HEAP]->{read} ); | ||
| 155 | $_[KERNEL]->delay( error_resuming => undef ); | ||
| 156 | $_[HEAP]->{resume_count}++; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub slave_got_input { | ||
| 160 | my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0]; | ||
| 161 | |||
| 162 | my $received = sysread($handle, my $buffer = '', 4); | ||
| 163 | unless ($received == 4) { | ||
| 164 | $kernel->select_read($handle); | ||
| 165 | $kernel->select_write($heap->{write}); | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | # The other session sent a pong. | ||
| 170 | if ($buffer eq 'pong') { | ||
| 171 | $heap->{test_count}++; | ||
| 172 | |||
| 173 | # Send another ping if we're not done. | ||
| 174 | if ($heap->{test_count} < $chat_count) { | ||
| 175 | push @{$heap->{queue}}, 'ping'; | ||
| 176 | $kernel->select_resume_write($heap->{write}); | ||
| 177 | |||
| 178 | # Pause reading. Gets resumed after a delay. | ||
| 179 | $kernel->select_pause_read( $heap->{read} ); | ||
| 180 | $kernel->delay( resume => 0.5 ); | ||
| 181 | } | ||
| 182 | |||
| 183 | # Otherwise we're done. Send a quit, and quit ourselves. | ||
| 184 | else { | ||
| 185 | push @{$heap->{queue}}, 'quit'; | ||
| 186 | $kernel->select_read($handle); | ||
| 187 | $kernel->select_resume_write($heap->{write}); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub slave_put_output { | ||
| 193 | my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0]; | ||
| 194 | |||
| 195 | # If there is a message queued, write it. | ||
| 196 | if (@{$heap->{queue}}) { | ||
| 197 | my $message = shift @{$heap->{queue}}; | ||
| 198 | die $! unless ( | ||
| 199 | syswrite($handle, $message, length($message)) == length($message) | ||
| 200 | ); | ||
| 201 | |||
| 202 | # Kludge. We requested quit, so go ahead and quit. | ||
| 203 | $kernel->select_write($handle) if $message eq 'quit'; | ||
| 204 | } | ||
| 205 | |||
| 206 | # Otherwise pause the write select. | ||
| 207 | else { | ||
| 208 | $kernel->select_pause_write($handle); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | ### Main loop. | ||
| 213 | |||
| 214 | # Spawn a group of master sessions. | ||
| 215 | |||
| 216 | for (my $index = 0; $index < $pair_count; $index++) { | ||
| 217 | POE::Session->create( | ||
| 218 | inline_states => { | ||
| 219 | _start => \&master_start, | ||
| 220 | _stop => \&master_stop, | ||
| 221 | _child => sub { }, | ||
| 222 | input => \&master_got_input, | ||
| 223 | output => \&master_put_output, | ||
| 224 | }, | ||
| 225 | args => [ $index ], | ||
| 226 | ); | ||
| 227 | } | ||
| 228 | |||
| 229 | # Spawn a quick and dirty session to test a new bug found in | ||
| 230 | # _internal_select. | ||
| 231 | |||
| 232 | POE::Session->create( | ||
| 233 | inline_states => { | ||
| 234 | _start => sub { | ||
| 235 | my $conduit; | ||
| 236 | $conduit = "inet" if $^O eq "MSWin32"; | ||
| 237 | |||
| 238 | my ($r, $w) = POE::Pipe::OneWay->new($conduit); | ||
| 239 | |||
| 240 | my $kernel = $_[KERNEL]; | ||
| 241 | $kernel->select_read($r, "input"); | ||
| 242 | $kernel->select_write($r, "output"); | ||
| 243 | $kernel->select_write($r); | ||
| 244 | $kernel->select_write($r, "output"); | ||
| 245 | $kernel->select($r); | ||
| 246 | }, | ||
| 247 | _stop => sub { }, | ||
| 248 | }, | ||
| 249 | ); | ||
| 250 | |||
| 251 | # Now run them until they're done. | ||
| 252 | POE::Kernel->run(); | ||
| 253 | |||
| 254 | # Try a re-entrant version. | ||
| 255 | POE::Session->create( | ||
| 256 | inline_states => { | ||
| 257 | _start => sub { | ||
| 258 | $_[HEAP]->{count} = 0; | ||
| 259 | $_[KERNEL]->yield("increment"); | ||
| 260 | }, | ||
| 261 | increment => sub { | ||
| 262 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 263 | if ($heap->{count} < 10) { | ||
| 264 | $kernel->yield("increment"); | ||
| 265 | $heap->{count}++; | ||
| 266 | } | ||
| 267 | }, | ||
| 268 | _stop => sub { | ||
| 269 | ok( $_[HEAP]->{count} == 10, "re-entered event loop ran" ); | ||
| 270 | }, | ||
| 271 | } | ||
| 272 | ); | ||
| 273 | |||
| 274 | # Verify that the main loop can run yet again. | ||
| 275 | POE::Kernel->run(); | ||
| 276 | |||
| 277 | pass("second event loop run exited normally"); | ||
| 278 | |||
| 279 | 1; |