File: 19test.t

package info (click to toggle)
libio-async-perl 0.64-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,068 kB
  • ctags: 491
  • sloc: perl: 12,530; makefile: 8
file content (69 lines) | stat: -rw-r--r-- 1,575 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Refcount;
use IO::Async::Test;

use IO::Async::OS;

use IO::Async::Loop;

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

is_refcount( $loop, 2, '$loop has refcount 2 initially' );

testing_loop( $loop );

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

my ( $S1, $S2 ) = IO::Async::OS->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' );

$readbuffer = "";

$S2->syswrite( "Some dynamic data\n" );

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

is( $readbuffer, "Some dynamic data\n" );

done_testing;