Skip to content

Commit

Permalink
clue#123 Revert timeouts to throwing a TimeoutException
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenJB committed Jan 19, 2019
1 parent 06f2be4 commit 79ea268
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
10 changes: 1 addition & 9 deletions src/Io/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use React\EventLoop\LoopInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Promise\Timer\TimeoutException;
use React\Stream\ReadableStreamInterface;

/**
Expand Down Expand Up @@ -87,14 +86,7 @@ public function send(RequestInterface $request)
return $deferred->promise();
}

return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop)->then(null, function ($e) {
if ($e instanceof TimeoutException) {
throw new \RuntimeException(
'Request timed out after ' . $e->getTimeout() . ' seconds'
);
}
throw $e;
});
return \React\Promise\Timer\timeout($deferred->promise(), $timeout, $this->loop);
}

private function next(RequestInterface $request, Deferred $deferred)
Expand Down
14 changes: 10 additions & 4 deletions tests/FunctionalBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use React\Http\Response;
use React\Http\StreamingServer;
use React\Promise\Stream;
use React\Promise\Timer\TimeoutException;
use React\Socket\Connector;
use React\Stream\ThroughStream;
use RingCentral\Psr7\Request;
Expand Down Expand Up @@ -115,15 +116,20 @@ public function testCancelRedirectedRequestShouldReject()
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Request timed out after 0.1 seconds
* @group online
*/
public function testTimeoutDelayedResponseShouldReject()
{
$promise = $this->browser->withOptions(array('timeout' => 0.1))->get($this->base . 'delay/10');
$caught = false;
try {
$promise = $this->browser->withOptions(array('timeout' => 0.1))->get($this->base . 'delay/10');

Block\await($promise, $this->loop);
Block\await($promise, $this->loop);
} catch (TimeoutException $e) {
$caught = true;
$this->assertEquals(0.1, $e->getTimeout());
}
$this->assertTrue($caught);
}

/**
Expand Down
42 changes: 24 additions & 18 deletions tests/Io/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,6 @@ public function testCancelTransactionShouldCancelSendingPromise()
$promise->cancel();
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Request timed out after 0.001 seconds
*/
public function testTimeoutExplicitOptionWillThrowException()
{
$messageFactory = new MessageFactory();
Expand All @@ -462,17 +458,20 @@ public function testTimeoutExplicitOptionWillThrowException()
$sender = $this->getMockBuilder('Clue\React\Buzz\Io\Sender')->disableOriginalConstructor()->getMock();
$sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response));

$transaction = new Transaction($sender, $messageFactory, $loop);
$transaction = $transaction->withOptions(array('timeout' => 0.001));
$promise = $transaction->send($request);
$caught = false;
try {
$transaction = new Transaction($sender, $messageFactory, $loop);
$transaction = $transaction->withOptions(array('timeout' => 0.001));
$promise = $transaction->send($request);

Block\await($promise, $loop);
Block\await($promise, $loop);
} catch (Promise\Timer\TimeoutException $e) {
$caught = true;
$this->assertEquals(0.001, $e->getTimeout());
}
$this->assertTrue($caught);
}

/**
* @expectedException RuntimeException
* @expectedExceptionMessage Request timed out after 0.001 seconds
*/
public function testTimeoutImplicitFromIniWillThrowException()
{
$messageFactory = new MessageFactory();
Expand All @@ -490,14 +489,21 @@ public function testTimeoutImplicitFromIniWillThrowException()
$sender = $this->getMockBuilder('Clue\React\Buzz\Io\Sender')->disableOriginalConstructor()->getMock();
$sender->expects($this->once())->method('send')->with($this->equalTo($request))->willReturn(Promise\resolve($response));

$transaction = new Transaction($sender, $messageFactory, $loop);
$caught = false;
try {
$transaction = new Transaction($sender, $messageFactory, $loop);

$old = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', '0.001');
$promise = $transaction->send($request);
ini_set('default_socket_timeout', $old);
$old = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', '0.001');
$promise = $transaction->send($request);
ini_set('default_socket_timeout', $old);

Block\await($promise, $loop);
Block\await($promise, $loop);
} catch (Promise\Timer\TimeoutException $e) {
$caught = true;
$this->assertEquals(0.001, $e->getTimeout());
}
$this->assertTrue($caught);
}

public function testTimeoutExplicitNegativeWillNotTimeOut()
Expand Down

0 comments on commit 79ea268

Please sign in to comment.