File: 70future-io.t

package info (click to toggle)
libio-async-perl 0.802-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,184 kB
  • sloc: perl: 13,932; makefile: 8
file content (74 lines) | stat: -r--r--r-- 1,751 bytes parent folder | download
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
#!/usr/bin/perl

use strict;
use warnings;

use IO::Async::Test;

use Test::More;
use Test::Future::IO::Impl;

use lib ".";
use t::TimeAbout;

use IO::Async::Loop;
use IO::Async::OS;

use Errno;

eval { require Future::IO; require Future::IO::ImplBase } or
   plan skip_all => "Future::IO is not available";
require Future::IO::Impl::IOAsync;

use constant AUT => $ENV{TEST_QUICK_TIMERS} ? 0.1 : 1;

testing_loop( IO::Async::Loop->new_builtin );

# ->sleep
{
   my $f = Future::IO->sleep( 2 * AUT );

   time_about( sub { wait_for_future $f }, 2, 'Future::IO->sleep' );
}

# ->sysread
{
   my ( $rd, $wr ) = IO::Async::OS->pipepair or die "Cannot pipe() - $!";
   $rd->blocking( 0 );

   $wr->autoflush();
   $wr->print( "Some bytes\n" );

   my $f = Future::IO->sysread( $rd, 256 );

   is( ( wait_for_future $f )->get, "Some bytes\n", 'Future::IO->sysread' );
}

# ->syswrite
{
   my ( $rd, $wr ) = IO::Async::OS->pipepair or die "Cannot pipe() - $!";
   $wr->blocking( 0 );

   $wr->autoflush();
   1 while $wr->syswrite( "X" x 4096 ); # This will eventually return undef/EAGAIN
   $! == Errno::EAGAIN or $! == Errno::EWOULDBLOCK or
      die "Expected EAGAIN, got $!";

   my $f = Future::IO->syswrite( $wr, "ABCD" );

   my $n = 0;
   # read as long as the pipe still blocks
   $rd->sysread( my $buf, 4096 ) and $n++ while !$wr->syswrite( "X" x 4096)
    and ($! == Errno::EAGAIN or $! == Errno::EWOULDBLOCK);
   # now we know how much we need to read out to unblock
   $rd->sysread( $buf, 4096 ) for 1..$n;

   is( ( wait_for_future $f )->get, 4, 'Future::IO->syswrite' );

   1 while $rd->sysread( $buf, 4096 ) == 4096;
   is( $buf, "ABCD", 'Future::IO->syswrite wrote data' );
}

run_tests qw( sleep sysread syswrite waitpid );

done_testing;