summaryrefslogtreecommitdiff
path: root/t/50client-pingpong.t
blob: 4f715bf320c36d2ca15e141cae975c226392ab37 (plain)
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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

use Time::HiRes qw(); # Empty import, just there to let IO::Async and Net::Async::IRC use it

use IO::Async::Test;
use IO::Async::OS;
use IO::Async::Loop;
use IO::Async::Stream;

use Net::Async::IRC;

my $CRLF = "\x0d\x0a"; # because \r\n isn't portable

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

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

my $lag;
my $pingout;

my $irc = Net::Async::IRC->new(
   handle => $S1,
   on_message => sub { "IGNORE" },

   pingtime => 2,
   pongtime => 1,

   on_pong_reply   => sub { $lag = $_[1] },
   on_ping_timeout => sub { $pingout = 1 },
);

$loop->add( $irc );

# This is all tricky timing-related code. Pay attention

# First [the server] will send three messages, separated by 1sec, and assert
# that the client didn't send a PING

my $serverstream = "";

my $msgcount = 0;

sub tick {
   $msgcount++;
   $S2->syswrite( "HELLO client$CRLF" );

   $loop->enqueue_timer(
      delay => 1,
      code => \&tick
   ) if $msgcount < 3;
}

tick();

wait_for_stream { $msgcount == 3 } $S2 => $serverstream;

is( $serverstream, "", 'client quiet after server noise' );

# Now [the server] will be quiet and assert that the client sends a PING

wait_for_stream { $serverstream =~ m/$CRLF/ } $S2 => $serverstream;

like( $serverstream, qr/^PING .*$CRLF$/, 'client sent PING after server idle' );

# Now lets be a good server and reply to the PING
my ( $pingarg ) = $serverstream =~ m/^PING (.*)$CRLF$/;
$S2->syswrite( ":irc.example.com PONG $pingarg$CRLF" );

undef $lag;
wait_for { defined $lag };

ok( $lag >= 0 && $lag <= 1, 'client acknowledges PONG reply' );

# Now [the server] won't reply to a PING at all, and hope for an event to note
# that it failed

wait_for { defined $pingout };
ok( $pingout, 'client reports PING timeout' );

done_testing;