Skip to content

Commit

Permalink
Add timeout test
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Oct 23, 2017
1 parent dcce613 commit 9759473
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/ClientHttpBinIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
use Amp\Artax\RequestBody;
use Amp\Artax\Response;
use Amp\Artax\SocketException;
use Amp\Artax\TimeoutException;
use Amp\Artax\TooManyRedirectsException;
use Amp\ByteStream\InMemoryStream;
use Amp\ByteStream\InputStream;
use Amp\ByteStream\IteratorStream;
use Amp\CancellationTokenSource;
use Amp\CancelledException;
use Amp\Delayed;
use Amp\Loop;
use Amp\Promise;
use Amp\Socket;
use Amp\Success;
Expand Down Expand Up @@ -140,6 +143,40 @@ public function testIncompleteHttpResponseWithoutChunkedEncodingAndWithoutConten
}
}

public function testTimeout() {
$client = new DefaultClient;
$client->setOption(Client::OP_TRANSFER_TIMEOUT, 500);

$server = Socket\listen("tcp://127.0.0.1:0");

asyncCall(function () use ($server) {
/** @var Socket\ClientSocket $client */
while ($client = yield $server->accept()) {
yield $client->write("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\n.");

Loop::delay(3000, function() use ($client) {
$client->close();
});
}
});

try {
$uri = "http://" . $server->getAddress() . "/";

$start = \microtime(true);
$promise = $client->request($uri);

/** @var Response $response */
$response = wait($promise);
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage("Allowed transfer timeout exceeded: 500 ms");
wait($response->getBody());
} finally {
$this->assertLessThan(0.6, \microtime(true) - $start);
$server->close();
}
}

public function testDefaultUserAgentSent() {
$uri = 'http://httpbin.org/user-agent';
$client = new DefaultClient;
Expand Down

0 comments on commit 9759473

Please sign in to comment.