Permalink
Browse files

fix test 4 to use pipe instead of socketpair

  • Loading branch information...
1 parent c1469b6 commit a0b9dee3e92dfef2ecf6689f430d2f0a942c6b0f @rcaputo committed Mar 28, 2000
Showing with 96 additions and 56 deletions.
  1. +3 −3 Makefile.PL
  2. +2 −2 lib/POE/Kernel.pm
  3. +6 −6 tests/03_aliases.t
  4. +85 −45 tests/04_selects.t
View
@@ -3,12 +3,12 @@
use ExtUtils::MakeMaker;
-# See lib/ExtUtils/MakeMaker.pm for details of how to influence
-# the contents of the Makefile that is written.
+# See lib/ExtUtils/MakeMaker.pm for details of how to influence the
+# contents of the Makefile that is written.
WriteMakefile
( NAME => 'POE',
- AUTHOR => 'Rocco Caputo <troc@netrus.net>',
+ AUTHOR => 'Rocco Caputo <troc+poe@netrus.net>',
ABSTRACT => 'Event driven state machines and I/O abstractions.',
VERSION_FROM => 'POE.pm',
dist => { 'COMPRESS' => 'gzip -9',
View
@@ -140,11 +140,11 @@ macro state_to_enqueue {
}
macro define_trace (<const>) {
- defined &TRACE_<const> or eval 'sub TRACE_<const> { TRACE_DEFAULT }';
+ defined &TRACE_<const> or eval 'sub TRACE_<const> () { TRACE_DEFAULT }';
}
macro define_assert (<const>) {
- defined &ASSERT_<const> or eval 'sub ASSERT_<const> { ASSERT_DEFAULT }';
+ defined &ASSERT_<const> or eval 'sub ASSERT_<const> () { ASSERT_DEFAULT }';
}
macro test_resolve (<name>,<resolved>) {
View
@@ -26,27 +26,27 @@ sub machine_start {
# Resolve weak, stringified session reference.
$resolved_session = $kernel->alias_resolve( "$session" );
print "not " unless $resolved_session eq $session;
- print "ok 3\n";
+ print "ok 2\n";
# Resolve against session ID.
$resolved_session = $kernel->alias_resolve( $session->ID );
print "not " unless $resolved_session eq $session;
- print "ok 4\n";
+ print "ok 3\n";
# Resolve against alias.
$resolved_session = $kernel->alias_resolve( 'new name' );
print "not " unless $resolved_session eq $session;
- print "ok 5\n";
+ print "ok 4\n";
# Resolve against blessed session reference.
$resolved_session = $kernel->alias_resolve( $session );
print "not " unless $resolved_session eq $session;
- print "ok 6\n";
+ print "ok 5\n";
# Resolve against something that doesn't exist.
$resolved_session = $kernel->alias_resolve( 'nonexistent' );
print "not " if defined $resolved_session;
- print "ok 7\n";
+ print "ok 6\n";
}
# Catch SIGIDLE and SIGZOMBIE.
@@ -94,7 +94,7 @@ POE::Session->create
},
);
-print "ok 2\n";
+print "ok 7\n";
# Now run the kernel until there's nothing left to do.
View
@@ -5,7 +5,7 @@
use strict;
use lib qw(./lib ../lib);
-use TestSetup qw(99);
+use TestSetup qw(23);
# Turn on all asserts.
sub POE::Kernel::ASSERT_DEFAULT () { 1 }
@@ -39,30 +39,38 @@ sub master_start {
$test_index *= 2;
- # Create a socket pain.
- my ($master_socket, $slave_socket) = (gensym, gensym);
- my $proto = getprotobyname('tcp');
- die "could not get tcp protocol number: $!" unless defined $proto;
- socketpair($master_socket, $slave_socket, AF_INET, SOCK_STREAM, $proto)
- or die "could not open a socket pain: $!";
+ # Create a pair of pipes.
+ my ($downlink_read, $downlink_write) = (gensym, gensym);
+ pipe($downlink_read, $downlink_write)
+ or die "cannot create downlink pipe: $!";
- # Select on one side.
- select_read($master_socket, 'input');
+ # Create a pair of pipes.
+ my ($uplink_read, $uplink_write) = (gensym, gensym);
+ pipe($uplink_read, $uplink_write)
+ or die "cannot create uplink pipe: $!";
+
+ # Listen on the uplink_read side.
+ $kernel->select_read($uplink_read, 'input');
# Give the other side to a newly spawned session.
POE::Session->create
( inline_states =>
{ _start => \&slave_start,
_stop => \&slave_stop,
- input => \&slave_input,
+ input => \&slave_got_input,
+ output => \&slave_put_output,
},
- args => [ $slave_socket, $test_index + 1 ],
+ args => [ $downlink_read, $uplink_write, $test_index + 1 ],
);
# Save some values for later.
- $heap->{socket} = $master_socket;
+ $heap->{write} = $downlink_write;
$heap->{test_index} = $test_index;
$heap->{test_count} = 0;
+ $heap->{queue} = [ ];
+
+ # Start the write thing.
+ $kernel->select_write($downlink_write, 'output');
}
sub master_stop {
@@ -75,36 +83,61 @@ sub master_stop {
sub master_got_input {
my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0];
- my $buffer = '';
- my $got = recv($handle, $buffer, 4, 0);
+ my $received = sysread($handle, my $buffer = '', 4);
+ unless ($received == 4) {
+die;
+ $kernel->select_read($handle);
+ $kernel->select_write($heap->{write});
+ return;
+ }
# The other session requested a quit. Shut down gracefully.
if ($buffer eq 'quit') {
- select_read($handle);
+ $kernel->select_read($handle);
+ $kernel->select_write($heap->{write});
return;
}
# The other session sent a ping. Count it, and send a pong.
if ($buffer eq 'ping') {
$heap->{test_count}++;
- my $sent = send($handle, 'pong', 0);
+ push @{$heap->{queue}}, 'pong';
+ $kernel->select_resume_write($heap->{write});
+ }
+}
- # Stop on error.
- select_read($handle) unless $sent == 4;
- return;
+sub master_put_output {
+ my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0];
+
+ # If there is a message queued, write it.
+ if (@{$heap->{queue}}) {
+ my $message = shift @{$heap->{queue}};
+ die $! unless syswrite($handle, $message) == length($message);
+ }
+
+ # Otherwise pause the write select.
+ else {
+ $kernel->select_pause_write($handle);
}
}
### Slave session.
sub slave_start {
- my ($kernel, $heap, $handle, $test_index) = @_[KERNEL, HEAP, ARG0, ARG1];
+ my ($kernel, $heap, $read_handle, $write_handle, $test_index) =
+ @_[KERNEL, HEAP, ARG0, ARG1, ARG2];
+
+ # Select on our read handle.
+ $kernel->select_read($read_handle, 'input');
- # Select on our socket.
- select_read($handle, 'input');
+ # Remember some things.
+ $heap->{write} = $write_handle;
+ $heap->{test_index} = $test_index;
+ $heap->{queue} = [ ];
# Say hello to the master session.
- send($handle, 'ping', 0);
+ push @{$heap->{queue}}, 'ping';
+ $kernel->select_write($write_handle, 'output');
}
sub slave_stop {
@@ -117,43 +150,49 @@ sub slave_stop {
sub slave_got_input {
my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0];
- my $buffer = '';
- my $got = recv($handle, $buffer, 4, 0);
-
- # The other session requested a quit. Shut down gracefully.
- if ($buffer eq 'quit') {
- select_read($handle);
+ my $received = sysread($handle, my $buffer = '', 4);
+ unless ($received == 4) {
+die;
+ $kernel->select_read($handle);
+ $kernel->select_write($heap->{write});
return;
}
# The other session sent a pong.
if ($buffer eq 'pong') {
-
- # Count it.
$heap->{test_count}++;
# Send another ping if we're not done.
if ($heap->{test_count} < $chat_count) {
- my $sent = send($handle, 'ping', 0);
-
- # Stop on error.
- select_read($handle) unless $sent == 4;
+ push @{$heap->{queue}}, 'ping';
+ $kernel->select_resume_write($heap->{write});
}
# Otherwise we're done. Send a quit, and quit ourselves.
else {
- my $sent = send($handle, 'quit', 0);
-
- # Stop on error.
- select_read($handle) unless $sent == 4;
+ push @{$heap->{queue}}, 'quit';
+ $kernel->select_read($handle);
+ $kernel->select_resume_write($heap->{write});
}
-
}
+}
+
+sub slave_put_output {
+ my ($kernel, $heap, $handle) = @_[KERNEL, HEAP, ARG0];
- # Received a message from the master session.
- # Make a note.
- # Send a response to the master.
+ # If there is a message queued, write it.
+ if (@{$heap->{queue}}) {
+ my $message = shift @{$heap->{queue}};
+ die $! unless syswrite($handle, $message) == length($message);
+ # Kludge. We requested quit, so go ahead and quit.
+ $kernel->select_write($handle) if $message eq 'quit';
+ }
+
+ # Otherwise pause the write select.
+ else {
+ $kernel->select_pause_write($handle);
+ }
}
### Main loop.
@@ -168,6 +207,7 @@ for (my $index = 0; $index < $pair_count; $index++) {
{ _start => \&master_start,
_stop => \&master_stop,
input => \&master_got_input,
+ output => \&master_put_output,
},
args => [ $index ],
);
@@ -180,11 +220,11 @@ $poe_kernel->run();
# Now make sure they've run.
for (my $index = 0; $index < $pair_count << 1; $index++) {
- "not " unless $test_results[$index];
+ print "not " unless $test_results[$index];
print "ok ", $index + 3, "\n";
}
# And one to grow on.
-print "ok 99\n";
+print "ok 23\n";
exit;

0 comments on commit a0b9dee

Please sign in to comment.