File: 19test.t

package info (click to toggle)
libio-async-perl 0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 684 kB
  • ctags: 239
  • sloc: perl: 6,439; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,335 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
#!/usr/bin/perl -w

use strict;

use Test::More tests => 4;
use Test::Refcount;
use IO::Async::Test;

use IO::Async::Loop;

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

is_oneref( $loop, '$loop has refcount 1' );

testing_loop( $loop );

is_refcount( $loop, 2, '$loop has refcount 2 after adding to IO::Async::Test' );

my ( $S1, $S2 ) = $loop->socketpair() or die "Cannot create socket pair - $!";

my $readbuffer = "";

$loop->watch_io(
   handle => $S1,
   on_read_ready => sub {
      $S1->sysread( $readbuffer, 8192, length $readbuffer ) or die "Test failed early";
   },
);

# This is just a token "does it run once?" test. A test of a test script. 
# Mmmmmm. Meta-testing.
# Coming up with a proper test that would guarantee multiple loop_once()
# cycles, etc.. is difficult. TODO for later I feel.
# In any case, the wait_for() function is effectively tested to death in later
# test scripts which use it. If it fails to work, they'd notice it.

$S2->syswrite( "A line\n" );

wait_for { $readbuffer =~ m/\n/ };

is( $readbuffer, "A line\n", 'Single-wait' );

$loop->unwatch_io(
   handle => $S1,
   on_read_ready => 1,
);

# Now the automatic version

$readbuffer = "";

$S2->syswrite( "Another line\n" );

wait_for_stream { $readbuffer =~ m/\n/ } $S1 => $readbuffer;

is( $readbuffer, "Another line\n", 'Automatic stream read wait' );