|
19 | 19 |
|
20 | 20 | use Google\Cloud\Core\Exception\BadRequestException; |
21 | 21 | use Google\Cloud\Core\Exception\ConflictException; |
| 22 | +use Google\Cloud\Core\Exception\FailedPreconditionException; |
| 23 | +use Google\Cloud\Core\Exception\ServiceException; |
22 | 24 | use Google\Cloud\Core\LongRunning\LongRunningOperation; |
23 | 25 | use Google\Cloud\Core\Testing\System\SystemTestCase; |
24 | 26 | use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient; |
@@ -78,13 +80,17 @@ public static function setUpTestFixtures(): void |
78 | 80 | self::$deletionQueue->add(function () { |
79 | 81 | self::getDatabaseInstance(self::$dbName1)->drop(); |
80 | 82 | }); |
| 83 | + } else { |
| 84 | + self::cleanUpPendingBackups(self::$dbName1); |
81 | 85 | } |
82 | 86 |
|
83 | 87 | if (!self::$dbName2 = getenv('GOOGLE_CLOUD_SPANNER_TEST_BACKUP_DATABASE_2')) { |
84 | 88 | self::$dbName2 = uniqid(self::TESTING_PREFIX); |
85 | 89 | self::$deletionQueue->add(function () { |
86 | 90 | self::getDatabaseInstance(self::$dbName2)->drop(); |
87 | 91 | }); |
| 92 | + } else { |
| 93 | + self::cleanUpPendingBackups(self::$dbName2); |
88 | 94 | } |
89 | 95 |
|
90 | 96 | $db1 = self::getDatabaseInstance(self::$dbName1); |
@@ -179,6 +185,9 @@ public function testCreateBackup() |
179 | 185 | $this->assertNotNull($metadata); |
180 | 186 | } |
181 | 187 |
|
| 188 | + /** |
| 189 | + * @depends testCreateBackup |
| 190 | + */ |
182 | 191 | public function testCreateBackupRequestFailed() |
183 | 192 | { |
184 | 193 | $backupId = uniqid(self::BACKUP_PREFIX); |
@@ -209,6 +218,9 @@ public function testCreateBackupRequestFailed() |
209 | 218 | $this->assertFalse($backup->exists()); |
210 | 219 | } |
211 | 220 |
|
| 221 | + /** |
| 222 | + * @depends testCreateBackup |
| 223 | + */ |
212 | 224 | public function testCreateBackupInvalidArgument() |
213 | 225 | { |
214 | 226 | $backupId = uniqid(self::BACKUP_PREFIX); |
@@ -734,4 +746,26 @@ private function pollWithExtendedTimeout($op) |
734 | 746 |
|
735 | 747 | return $op; |
736 | 748 | } |
| 749 | + |
| 750 | + private static function cleanUpPendingBackups($dbName) |
| 751 | + { |
| 752 | + $dbFullName = self::getDatabaseInstance($dbName)->name(); |
| 753 | + try { |
| 754 | + foreach (self::$instance->backupOperations() as $op) { |
| 755 | + if (!$op->done()) { |
| 756 | + $metadata = $op->info()['metadata'] ?? []; |
| 757 | + if (isset($metadata['database']) && $metadata['database'] === $dbFullName) { |
| 758 | + try { |
| 759 | + $op->cancel(); |
| 760 | + $op->pollUntilComplete(['maxPollingDurationSeconds' => 120]); |
| 761 | + } catch (\Exception $e) { |
| 762 | + // Ignore exceptions from cancelled operations |
| 763 | + } |
| 764 | + } |
| 765 | + } |
| 766 | + } |
| 767 | + } catch (\Exception $e) { |
| 768 | + // Ignore errors |
| 769 | + } |
| 770 | + } |
737 | 771 | } |
0 commit comments