// test connect timeout
import std.conv : to;
import vibe.core.stream: pipe, nullSink;
HTTPClientSettings settings = new HTTPClientSettings;
settings.connectTimeout = 50.msecs;
// Use an IP address that is guaranteed to be unassigned globally to force
// a timeout (see RFC 3330)
auto cli = connectHTTP("192.0.2.0", 80, false, settings);
auto timer = setTimer(500.msecs, { assert(false, "Connect timeout occurred too late"); });
scope (exit) timer.stop();
try {
cli.request(
(scope req) { assert(false, "Expected no connection"); },
(scope res) { assert(false, "Expected no response"); }
);
assert(false, "Response read expected to fail due to timeout");
} catch(Exception e) {}
Example
// test read timeout
import std.conv : to;
import vibe.core.stream: pipe, nullSink;
version (VibeLibasyncDriver) {
logInfo("Skipping HTTP client read timeout test due to buggy libasync driver.");
} else {
HTTPClientSettings settings = new HTTPClientSettings;
settings.readTimeout = 50.msecs;
auto l = listenTCP(0, (conn) {
try conn.pipe(nullSink);
catch (Exception e) assert(false, e.msg);
conn.close();
}, "127.0.0.1");
auto cli = connectHTTP("127.0.0.1", l.bindAddress.port, false, settings);
auto timer = setTimer(500.msecs, { assert(false, "Read timeout occurred too late"); });
scope (exit) {
timer.stop();
l.stopListening();
cli.disconnect();
sleep(10.msecs); // allow the read connection end to fully close
}
try {
cli.request(
(scope req) { req.method = HTTPMethod.GET; },
(scope res) { assert(false, "Expected no response"); }
);
assert(false, "Response read expected to fail due to timeout");
} catch(Exception e) {}
}