#!/usr/bin/perl -w

use strict;

use Test::More tests => 3;

use POSIX qw( EAGAIN );

use IO::Async::Loop;
use IO::Async::Stream;

# 4 ends of sockets:
#  test => notifier ; notifier => test
#  S[0]    S[1]       S[2]        S[3]

my $loop = IO::Async::Loop->new;

my @S;
@S[0,1] = $loop->socketpair() or die "Cannot create socket pair - $!";
@S[2,3] = $loop->socketpair() or die "Cannot create socket pair - $!";

# Want all pipes to be nonblocking, autoflushing
for ( @S ) {
   $_->blocking( 0 );
   $_->autoflush( 1 );
}

sub read_data($)
{
   my ( $s ) = @_;

   my $buffer;
   my $ret = sysread( $s, $buffer, 8192 );

   return $buffer if( defined $ret && $ret > 0 );
   die "Socket closed" if( defined $ret && $ret == 0 );
   return "" if( $! == EAGAIN );
   die "Cannot sysread() - $!";
}

my $incoming_buffer = "";
sub on_read
{
   my $self = shift;
   my ( $buffref, $buffclosed ) = @_;

   $incoming_buffer .= $$buffref;
   $$buffref = "";

   return 0;
}

my $stream = IO::Async::Stream->new(
   read_handle  => $S[1],
   write_handle => $S[2],
   on_read => \&on_read,
);

# Writing
$stream->write( "message\n" );
$stream->on_write_ready;

is( read_data( $S[3] ), "message\n", '$S[3] receives data' );
is( read_data( $S[0] ), "",          '$S[0] empty' );

# Receiving
$S[0]->syswrite( "another message\n" );
# Reverse push - should be ignored
$S[3]->syswrite( "reverse\n" );

$stream->on_read_ready;

is( $incoming_buffer, "another message\n", 'incoming buffer contains message' );
