Skip to content

Commit

Permalink
just die on socket errors.
Browse files Browse the repository at this point in the history
following the discussion at

    15c655c#diff-4d52833b8aa64cefe7650fe92f375da4R110

we decided that dying on socket errors (SO_ERROR) is OK.

However, then it makes it difficult to properly test a "connect timeout".
So we update the test suite accordingly to accomodate the fact that,
on some platforms, we die on socket errors (eg, No route to host),
on others, it becomes a connection timeout.

The platform-dependent behaviour feels odd, but packing one type
of error into other type of errors is not better.
  • Loading branch information
gugod committed Mar 21, 2014
1 parent 2d1cd30 commit 7439c92
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/Hijk.pm
Expand Up @@ -107,7 +107,7 @@ sub construct_socket {
}

if ($! = unpack("L", getsockopt($soc, SOL_SOCKET, SO_ERROR))) {
return (undef, Hijk::Error::CONNECT_TIMEOUT);
die $!;
}

return $soc;
Expand Down
26 changes: 19 additions & 7 deletions t/live-connect-timeout.t
Expand Up @@ -27,16 +27,28 @@ if ($iter == 0) {

pass "ip generated = $ip";

lives_ok {
my $res = Hijk::request({
my ($res, $exception);

eval {
$res = Hijk::request({
host => $ip,
port => 80,
timeout => 1 # seconds
timeout => 1 # seconds
});

ok exists $res->{error}, '$res->{error} exists because we expect error to happen.';
1;
}
or do {
$exception = $@ || "unknown error.";
$exception =~ s/\n//g;
};

if ($exception) {
pass "On $^O, we have exception trying to connect to an unreachable IP: $exception";
is(scalar(keys %{$Hijk::SOCKET_CACHE}), 0, "We have nothing in the socket cache after the connect exception.");
} else {
ok exists $res->{error}, "On $^O, ".'$res->{error} exists because we expect error to happen.';
is $res->{error}, Hijk::Error::CONNECT_TIMEOUT, '$res->{error} contiain the value of Hijk::Error::CONNECT_TIMEOUT, indicating that it timed-out when establishing connection';
} "We could make the request";
is(scalar(keys %{$Hijk::SOCKET_CACHE}), 0, "We have nothing in the socket cache after a timeout");
is(scalar(keys %{$Hijk::SOCKET_CACHE}), 0, "We have nothing in the socket cache after a timeout");
}

done_testing;

0 comments on commit 7439c92

Please sign in to comment.