Skip to content

Commit 8afd213

Browse files
committed
refactor(spanner): DRY up extended polling loop in BackupTest
1 parent b0ba492 commit 8afd213

1 file changed

Lines changed: 22 additions & 52 deletions

File tree

Spanner/tests/System/BackupTest.php

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,7 @@ public function testCreateBackup()
154154
$this->assertArrayHasKey('startTime', $metadata['progress']);
155155

156156
// Poll for completion with the extended timeout
157-
$timeout = time() + self::LONG_TIMEOUT_SECONDS;
158-
while (time() < $timeout) {
159-
try {
160-
$op->pollUntilComplete([
161-
'timeoutMillis' => ($timeout - time()) * 1000
162-
]);
163-
break;
164-
} catch (\Google\ApiCore\ApiException $e) {
165-
if ($e->getStatus() !== 'DEADLINE_EXCEEDED') {
166-
throw $e;
167-
}
168-
}
169-
}
157+
$this->pollWithExtendedTimeout($op);
170158

171159
self::$deletionQueue->add(function () use ($backup) {
172160
$backup->delete();
@@ -292,19 +280,7 @@ public function testCreateBackup2()
292280
$backup = self::$instance->backup(self::$backupId2);
293281

294282
$op = $backup->create(self::$dbName2, $expireTime);
295-
$timeout = time() + self::LONG_TIMEOUT_SECONDS;
296-
while (time() < $timeout) {
297-
try {
298-
$op->pollUntilComplete([
299-
'maxPollingDurationSeconds' => $timeout - time()
300-
]);
301-
break;
302-
} catch (\Google\ApiCore\ApiException $e) {
303-
if ($e->getStatus() !== 'DEADLINE_EXCEEDED') {
304-
throw $e;
305-
}
306-
}
307-
}
283+
$this->pollWithExtendedTimeout($op);
308284

309285
self::$deletionQueue->add(function () use ($backup) {
310286
$backup->delete();
@@ -336,19 +312,7 @@ public function testCreateBackupCopy()
336312
$this->assertArrayHasKey('progressPercent', $metadata['progress']);
337313
$this->assertArrayHasKey('startTime', $metadata['progress']);
338314

339-
$timeout = time() + self::LONG_TIMEOUT_SECONDS;
340-
while (time() < $timeout) {
341-
try {
342-
$op->pollUntilComplete([
343-
'maxPollingDurationSeconds' => $timeout - time()
344-
]);
345-
break;
346-
} catch (\Google\ApiCore\ApiException $e) {
347-
if ($e->getStatus() !== 'DEADLINE_EXCEEDED') {
348-
throw $e;
349-
}
350-
}
351-
}
315+
$this->pollWithExtendedTimeout($op);
352316

353317
self::$deletionQueue->add(function () use ($newBackup) {
354318
$newBackup->delete();
@@ -645,19 +609,7 @@ public function testRestoreToNewDatabase()
645609
$this->assertArrayHasKey('startTime', $metadata['progress']);
646610

647611
// Poll for completion with the extended timeout
648-
$timeout = time() + self::LONG_TIMEOUT_SECONDS;
649-
while (time() < $timeout) {
650-
try {
651-
$op->pollUntilComplete([
652-
'maxPollingDurationSeconds' => $timeout - time()
653-
]);
654-
break;
655-
} catch (\Google\ApiCore\ApiException $e) {
656-
if ($e->getStatus() !== 'DEADLINE_EXCEEDED') {
657-
throw $e;
658-
}
659-
}
660-
}
612+
$this->pollWithExtendedTimeout($op);
661613
$restoredDb = $this::$instance->database($restoreDbName);
662614

663615
self::$deletionQueue->add(function () use ($restoredDb) {
@@ -763,4 +715,22 @@ private static function parseName($name, $id)
763715
{
764716
return DatabaseAdminClient::parseName($name)[$id];
765717
}
718+
private function pollWithExtendedTimeout($op)
719+
{
720+
$timeout = time() + self::LONG_TIMEOUT_SECONDS;
721+
while (time() < $timeout) {
722+
try {
723+
$op->pollUntilComplete([
724+
'maxPollingDurationSeconds' => $timeout - time()
725+
]);
726+
break;
727+
} catch (\Google\ApiCore\ApiException $e) {
728+
if ($e->getStatus() !== 'DEADLINE_EXCEEDED') {
729+
throw $e;
730+
}
731+
}
732+
}
733+
734+
return $op;
735+
}
766736
}

0 commit comments

Comments
 (0)