Skip to content

Commit

Permalink
fix: error matcher on booleans
Browse files Browse the repository at this point in the history
When the expected value from acceptor is a boolean then, the matcher should:
- if expected is true then an error should be matched.
- if expected is false then no error should has been occurred.
  • Loading branch information
yenfryherrerafeliz committed Aug 22, 2024
1 parent 8dd47fd commit fbfa6b7
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Waiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ private function matchesStatus($result, array $acceptor)
*/
private function matchesError($result, array $acceptor)
{
// If expected is true then the $result should be an instance of
// AwsException, otherwise it should not.
if (isset($acceptor['expected']) && is_bool($acceptor['expected'])) {
return $acceptor['expected'] === ($result instanceof AwsException);
}

if ($result instanceof AwsException) {
return $result->isConnectionError()
|| $result->getAwsErrorCode() == $acceptor['expected'];
Expand Down
101 changes: 101 additions & 0 deletions tests/WaiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Aws\DynamoDb\DynamoDbClient;
use Aws\Exception\AwsException;
use Aws\Result;
use Aws\S3\S3Client;
use Aws\Waiter;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\RejectedPromise;
Expand Down Expand Up @@ -400,4 +402,103 @@ private function getMockResult($data = [])

return new Result($data + ['@metadata' => ['statusCode' => 200]]);
}


/**
* Tests the waiter expects not error.
* This means the operation should succeed.
*
* @return void
*/
public function testWaiterMatcherExpectNoError(): void
{
$client = new S3Client([
'region' => 'us-east-2',
'http_handler' => function (RequestInterface $_) {
$responseBody = <<<EOXML
<?xml version="1.0" encoding="UTF-8"?><Operation></Operation>
EOXML;
return new Response(200, [], $responseBody);
}
]);
$commandArgs = [
'Bucket' => 'fuzz',
'Key' => 'bazz'
];
$waiterConfig = [
'delay' => 5,
'operation' => 'headObject',
'maxAttempts' => 20,
'acceptors' => [
[
'expected' => false,
'matcher' => 'error',
'state' => 'success'
]
]
];
$waiter = new Waiter(
$client,
'foo',
$commandArgs,
$waiterConfig
);
$waiter->promise()
->then(function (CommandInterface $_) {
$this->assertTrue(true); // Waiter succeeded
})->wait();
}

/**
* Tests the waiter should receive an error.
* This means the operation should fail.
*
* @return void
*/
public function testWaiterMatcherExpectsAnyError(): void
{
$client = new S3Client([
'region' => 'us-east-2',
'http_handler' => function (RequestInterface $request) {
$responseBody = <<<EOXML
<?xml version="1.0" encoding="UTF-8"?><Operation></Operation>
EOXML;
$response = new Response(200, [], $responseBody);
return new RejectedPromise([
'connection_error' => true,
'exception' => new RequestException(
'Error',
$request,
$response
),
]);
}
]);
$commandArgs = [
'Bucket' => 'fuzz',
'Key' => 'bazz'
];
$waiterConfig = [
'delay' => 5,
'operation' => 'headObject',
'maxAttempts' => 20,
'acceptors' => [
[
'expected' => true,
'matcher' => 'error',
'state' => 'success'
]
]
];
$waiter = new Waiter(
$client,
'foo',
$commandArgs,
$waiterConfig
);
$waiter->promise()
->then(function (CommandInterface $_) {
$this->assertTrue(true); // Waiter succeeded
})->wait();
}
}

0 comments on commit fbfa6b7

Please sign in to comment.