Skip to content

Commit

Permalink
add tests for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Jun 18, 2024
1 parent 23ee785 commit bb6e587
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Bigtable/src/BigtableClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Bigtable\V2\BigtableClient as GapicClient;
use Google\Cloud\Core\ArrayTrait;
use Psr\Log\LoggerInterface;

/**
* Google Cloud Bigtable is Google's NoSQL Big Data database service.
Expand Down Expand Up @@ -144,6 +145,7 @@ public function __construct(array $config = [])
* @type string $appProfileId This value specifies routing for
* replication. **Defaults to** the "default" application profile.
* @type array $headers Headers to be passed with each request.
* @type LoggerInterface $logger
* }
* @return Table
*/
Expand Down
3 changes: 2 additions & 1 deletion Bigtable/src/ChunkFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ function ($ex) {
}
return false;
},
$this->pluck('retries', $this->options, false)
$this->pluck('retries', $this->options, false),
$this->pluck('logger', $this->options, false)
);
}

Expand Down
5 changes: 4 additions & 1 deletion Bigtable/src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Google\Cloud\Bigtable\V2\RowSet;
use Google\Cloud\Core\ArrayTrait;
use Google\Rpc\Code;
use Psr\Log\LoggerInterface;

/**
* A table instance can be used to read rows and to perform insert, update, and
Expand Down Expand Up @@ -83,6 +84,7 @@ class Table
* This settings only applies to {@see \Google\Cloud\Bigtable\Table::mutateRows()},
* {@see \Google\Cloud\Bigtable\Table::upsert()} and
* {@see \Google\Cloud\Bigtable\Table::readRows()}.
* @type LoggerInterface $logger
* }
*/
public function __construct(
Expand Down Expand Up @@ -519,7 +521,8 @@ private function mutateRowsWithEntries(array $entries, array $options = [])
[$this->gapicClient, 'mutateRows'],
$argumentFunction,
$retryFunction,
$this->pluck('retries', $options, false)
$this->pluck('retries', $options, false),
$this->pluck('logger', $options, false)
);
$message = 'partial failure';
try {
Expand Down
59 changes: 58 additions & 1 deletion Bigtable/tests/Unit/SmartRetriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;

/**
* @group bigtable
Expand Down Expand Up @@ -195,7 +196,6 @@ public function testReadRowsContainsAttemptHeader()
$attempt = 0;
$expectedArgs = $this->options;
$retryingApiException = $this->retryingApiException;
$phpunit = $this;
$this->bigtableClient->readRows(
self::TABLE_NAME,
Argument::that(function ($callOptions) use (&$attempt) {
Expand Down Expand Up @@ -747,6 +747,63 @@ public function testMutateRowsShouldNotRetryIfAnyMutationIsNotRetryable()
}
}

public function testReadRowsShouldLogRetryableExeception()
{
$attempt = 0;
$expectedArgs = $this->options;
$retryingApiException = $this->retryingApiException;
$logger = $this->prophesize(LoggerInterface::class);
$logger->error('DEADLINE_EXCEEDED')
->shouldBeCalledOnce();
$this->bigtableClient->readRows(
self::TABLE_NAME,
Argument::that(function ($callOptions) use (&$attempt) {
$attemptHeader = $callOptions['headers']['bigtable-attempt'][0] ?? null;
($attempt === 0)
? $this->assertNull($attemptHeader)
: $this->assertSame('1', $attemptHeader);

return true;
})
)->shouldBeCalledTimes(2)
->willReturn(
$this->serverStream->reveal()
);

$this->serverStream->readAll()
->will(function () use (&$attempt, $retryingApiException) {
// throw a retriable exception on the first call
return 0 === $attempt++ ? throw $retryingApiException : [];
});

$iterator = $this->table->readRows(['logger' => $logger->reveal()]);
$iterator->getIterator()->current();
}

public function testReadRowsShouldLogNonRetryableExeception()
{
$this->expectException(ApiException::class);
$this->expectExceptionMessage('UNAUTHENTICATED');

$logger = $this->prophesize(LoggerInterface::class);
$logger->error('UNAUTHENTICATED')
->shouldBeCalledOnce();

$this->bigtableClient->readRows(
self::TABLE_NAME,
$this->options
)->shouldBeCalledOnce()
->willReturn(
$this->serverStream->reveal()
);

$this->serverStream->readAll()
->willThrow($this->nonRetryingApiException);

$iterator = $this->table->readRows(['logger' => $logger->reveal()]);
$iterator->getIterator()->current();
}

private function generateRowsResponse($from, $to)
{
$rows = [];
Expand Down

0 comments on commit bb6e587

Please sign in to comment.