File: timeout.t

package info (click to toggle)
libio-socket-timeout-perl 0.32-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 160 kB
  • sloc: perl: 330; makefile: 2
file content (94 lines) | stat: -rw-r--r-- 3,461 bytes parent folder | download | duplicates (2)
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
87
88
89
90
91
92
93
94
use strict;
use warnings;

BEGIN {
    $ENV{PERL_IO_SOCKET_TIMEOUT_FORCE_SELECT} = 1;
}

use Test::More;
use FindBin qw($Bin);
use lib "$Bin/tlib";
use TestTimeout;
use Errno qw(ETIMEDOUT);

subtest 'test with no delays and no timeouts', sub {
TestTimeout->test( connection_delay => 0,
                   read_delay => 0,
                   write_delay => 0,
                   callback => sub {
                       my ($client) = @_;
                       $client->print("OK\n");
                       my $response = $client->getline;
                       is $response, "SOK\n", "got proper response 1";
                       $client->print("OK2\n");
                       $response = $client->getline;
                       is $response, "SOK2\n", "got proper response 2";
                   },
                 );
};

subtest 'test with read timeout', sub {

TestTimeout->test( connection_delay => 0,
                   read_timeout => 0.2,
                   read_delay => 3,
                   write_timeout => 0,
                   write_delay => 0,
                   callback => sub {
                       my ($client) = @_;
                       ok $client->isa('IO::Socket::Timeout::Role::PerlIO'), 'client does PerlIO';
                       $client->print("OK\n");
                       my $response = $client->getline;
                       is $response, "SOK\n", "got proper response 1";
                       $client->print("OK2\n");
                       $response = $client->getline;
                       is $response, undef, "we've hit timeout";
                       is 0+$!, ETIMEDOUT, "and error is timeout";
                   },
                 );
};

subtest 'test with sysread timeout', sub {
TestTimeout->test( connection_delay => 0,
                   read_timeout => 0.2,
                   read_delay => 3,
                   write_timeout => 0,
                   write_delay => 0,
                   callback => sub {
                       my ($client) = @_;
                       ok $client->isa('IO::Socket::Timeout::Role::PerlIO'), 'client does PerlIO';
                       $client->print("OK\n");
                       sysread $client, my $response, 4;

                       is $response, "SOK\n", "got proper response 1";
                       $client->print("OK2\n");
                       $response = undef;
                       sysread $client, $response, 5;
                       is $response, undef, "we've hit timeout";
                       is 0+$!, ETIMEDOUT, "and error is timeout";
                   },
                 );
};

subtest 'test standard sysread/syswrite no timeout', sub {
TestTimeout->test( connection_delay => 0,
                   read_delay => 0,
                   write_delay => 0,
                   no_timeouts => 1,
                   callback => sub {
                       my ($client) = @_;
                       ok ! $client->isa('IO::Socket::Timeout::Role::PerlIO'), 'client does not do PerlIO';
                       $client->print("OK\n");
                       sysread $client, my $response, 4;

                       is $response, "SOK\n", "got proper response 1";
                       $client->print("OK2\n");
                       $response = undef;
                       sysread $client, $response, 5;
                       is $response, "SOK2\n", "got proper response 2";
                   },
                 );
};

done_testing;