File: 20-timeout.t

package info (click to toggle)
libanyevent-handle-udp-perl 0.050-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 160 kB
  • sloc: perl: 360; makefile: 6; sh: 4
file content (34 lines) | stat: -rw-r--r-- 1,009 bytes parent folder | download | duplicates (5)
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
#! perl

use strict;
use warnings FATAL => 'all';
use Test::More 0.89;
use Test::Fatal;

use AnyEvent::Handle::UDP;
use IO::Socket::INET;

alarm 12;

{
	my $cb = AE::cv;
	my $cb2 = AE::cv;
	my $server = AnyEvent::Handle::UDP->new(
		bind => [ localhost => 0 ],
		on_recv => $cb, 
		timeout => 3,    on_timeout => sub { $cb->croak("Timeout") },
		rtimeout => 4.5, on_rtimeout => sub { $cb2->croak("Read Timeout") }
	);
	my $start_time = AE::now;
	like(exception { $cb->recv }, qr/Timeout/, 'Receive throws a timeout');
	cmp_ok AE::now, '>=', $start_time + 3, 'Three seconds have passed';
	like(exception { $cb2->recv }, qr/Read Timeout/, 'Receive throws a timeout again');
	cmp_ok AE::now, '>=', $start_time + 4.5, '1.5 more seconds have passed';
	$server->timeout_reset;
	my $cb3 = AE::cv;
	$server->on_timeout(sub { $cb3->croak('Reset') });
	like(exception { $cb3->recv }, qr/Reset/, 'Receive throws a timeout again');
	cmp_ok AE::now, '>=', $start_time + 7.5, '3 more seconds have passed';
}

done_testing;