Skip to content

Commit

Permalink
Remove potential for NPE in waiter and flag connection errors as retr…
Browse files Browse the repository at this point in the history
…iable within waiter limits
  • Loading branch information
jeskew committed Aug 13, 2015
1 parent 2b43a45 commit 38fc1fb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Waiter.php
Expand Up @@ -241,8 +241,8 @@ private function matchesStatus($result, array $acceptor)
{
if ($result instanceof ResultInterface) {
return $acceptor['expected'] == $result['@metadata']['statusCode'];
} elseif ($result instanceof AwsException) {
return $acceptor['expected'] == $result->getResponse()->getStatusCode();
} elseif ($result instanceof AwsException && $response = $result->getResponse()) {
return $acceptor['expected'] == $response->getStatusCode();
} else {
return false;
}
Expand All @@ -256,8 +256,11 @@ private function matchesStatus($result, array $acceptor)
*/
private function matchesError($result, array $acceptor)
{
return !($result instanceof AwsException)
? false
: $result->getAwsErrorCode() == $acceptor['expected'];
if ($result instanceof AwsException) {
return $result->isConnectionError()
|| $result->getAwsErrorCode() == $acceptor['expected'];
}

return false;
}
}
37 changes: 37 additions & 0 deletions tests/WaiterTest.php
Expand Up @@ -6,10 +6,15 @@
use Aws\DynamoDb\DynamoDbClient;
use Aws\Exception\AwsException;
use Aws\Result;
use Aws\S3\S3Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\RejectedPromise;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;

/**
* @covers Aws\Waiter
Expand Down Expand Up @@ -54,6 +59,38 @@ public function testErrorOnBadBeforeCallback()
);
}

public function testContinueWaitingOnHandlerError()
{
$retries = 10;
$client = new DynamoDbClient([
'version' => 'latest',
'region' => 'us-west-2',
'http_handler' => function (
RequestInterface $request,
array $options
) use (&$retries) {
if (0 === --$retries) {
return new FulfilledPromise(new Response(200, [],
Psr7\stream_for('{"Table":{"TableStatus":"ACTIVE"}}')
));
}

return new RejectedPromise([
'connection_error' => true,
'exception' => $this->getMockBuilder(ConnectException::class)
->disableOriginalConstructor()
->getMock(),
'response' => null,
]);
},
]);

$client->waitUntil('TableExists', [
'TableName' => 'table',
'retries' => 0,
]);
}

public function testCanCancel()
{
$client = $this->getTestClient('DynamoDb');
Expand Down

0 comments on commit 38fc1fb

Please sign in to comment.