Skip to content

Instantly share code, notes, and snippets.

@olegwtf
Created April 14, 2012 10:09
Show Gist options
  • Save olegwtf/2383333 to your computer and use it in GitHub Desktop.
Save olegwtf/2383333 to your computer and use it in GitHub Desktop.
coro::lwp and timeout test
#!/usr/bin/env perl
use strict;
use Coro::LWP;
use Coro;
use LWP;
use Test::More tests => 1;
sub make_broken_http_server {
my $serv = IO::Socket::INET->new(Listen => 1);
my $child = fork();
die 'fork:', $! unless defined $child;
if ($child == 0) {
while (1) {
my $cli = $serv->accept()
or next;
sleep 30;
$cli->close();
}
exit;
}
return ($child, $serv->sockhost eq "0.0.0.0" ? "127.0.0.1" : $serv->sockhost, $serv->sockport);
}
my ($pid, $host, $port) = make_broken_http_server();
my $coro = async {
my $ua = LWP::UserAgent->new(timeout => 5);
my $resp = $ua->get("http://$host:$port");
warn $resp->status_line;
};
my $time_start = time();
$coro->join();
my $time_spent = time() - $time_start;
ok($time_spent < 30, 'Read timed out')
or diag("$time_spent sec spent");
kill 15, $pid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment