1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#!/usr/bin/perl -w
use strict;
use IO::Async::Test;
use Test::More tests => 11;
use Test::Identity;
use IO::Async::Channel;
use IO::Async::OS;
use IO::Async::Loop::Poll;
use Storable qw( freeze );
my $loop = IO::Async::Loop::Poll->new;
testing_loop( $loop );
# sync->sync - mostly doesn't involve IO::Async
{
my ( $pipe_rd, $pipe_wr ) = IO::Async::OS->pipepair;
my $channel_rd = IO::Async::Channel->new;
$channel_rd->setup_sync_mode( $pipe_rd );
my $channel_wr = IO::Async::Channel->new;
$channel_wr->setup_sync_mode( $pipe_wr );
$channel_wr->send( [ structure => "here" ] );
is_deeply( $channel_rd->recv, [ structure => "here" ], 'Sync mode channels can send/recv structures' );
$channel_wr->send_frozen( freeze [ prefrozen => "data" ] );
is_deeply( $channel_rd->recv, [ prefrozen => "data" ], 'Sync mode channels can send_frozen' );
$channel_wr->close;
is( $channel_rd->recv, undef, 'Sync mode can be closed' );
}
# async->sync
{
my ( $pipe_rd, $pipe_wr ) = IO::Async::OS->pipepair;
my $channel_rd = IO::Async::Channel->new;
$channel_rd->setup_sync_mode( $pipe_rd );
my $channel_wr = IO::Async::Channel->new;
$channel_wr->setup_async_mode( write_handle => $pipe_wr );
$loop->add( $channel_wr );
$channel_wr->send( [ data => "by async" ] );
# Cheat for semi-sync
my $flushed;
$channel_wr->{stream}->write( "", on_flush => sub { $flushed++ } );
wait_for { $flushed };
is_deeply( $channel_rd->recv, [ data => "by async" ], 'Async mode channel can send' );
$channel_wr->close;
is( $channel_rd->recv, undef, 'Sync mode can be closed' );
}
# sync->async configured on_recv
{
my ( $pipe_rd, $pipe_wr ) = IO::Async::OS->pipepair;
my @recv_queue;
my $recv_eof;
my $channel_rd = IO::Async::Channel->new;
$channel_rd->setup_async_mode( read_handle => $pipe_rd );
$loop->add( $channel_rd );
$channel_rd->configure(
on_recv => sub {
identical( $_[0], $channel_rd, 'Channel passed to on_recv' );
push @recv_queue, $_[1];
},
on_eof => sub {
$recv_eof++;
},
);
my $channel_wr = IO::Async::Channel->new;
$channel_wr->setup_sync_mode( $pipe_wr );
$channel_wr->send( [ data => "by sync" ] );
wait_for { @recv_queue };
is_deeply( shift @recv_queue, [ data => "by sync" ], 'Async mode channel can on_recv' );
$channel_wr->close;
wait_for { $recv_eof };
is( $recv_eof, 1, 'Async mode channel can on_eof' );
}
# sync->async oneshot ->recv
{
my ( $pipe_rd, $pipe_wr ) = IO::Async::OS->pipepair;
my $channel_rd = IO::Async::Channel->new;
$channel_rd->setup_async_mode( read_handle => $pipe_rd );
$loop->add( $channel_rd );
my $channel_wr = IO::Async::Channel->new;
$channel_wr->setup_sync_mode( $pipe_wr );
$channel_wr->send( [ data => "by sync" ] );
my $recved;
$channel_rd->recv(
on_recv => sub {
identical( $_[0], $channel_rd, 'Channel passed to ->recv on_recv' );
$recved = $_[1];
},
on_eof => sub { die "Test failed early" },
);
wait_for { $recved };
is_deeply( $recved, [ data => "by sync" ], 'Async mode channel can ->recv on_recv' );
$channel_wr->close;
my $recv_eof;
$channel_rd->recv(
on_recv => sub { die "Channel recv'ed when not expecting" },
on_eof => sub { $recv_eof++ },
);
wait_for { $recv_eof };
is( $recv_eof, 1, 'Async mode channel can ->recv on_eof' );
}
|