@@ -154,9 +154,19 @@ public function testCreateBackup()
154154 $ this ->assertArrayHasKey ('startTime ' , $ metadata ['progress ' ]);
155155
156156 // Poll for completion with the extended timeout
157- $ op ->pollUntilComplete ([
158- 'timeoutMillis ' => self ::LONG_TIMEOUT_SECONDS * 1000 // GAX expects milliseconds
159- ]);
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+ }
160170
161171 self ::$ deletionQueue ->add (function () use ($ backup ) {
162172 $ backup ->delete ();
@@ -189,10 +199,20 @@ public function testCreateBackupRequestFailed()
189199 $ backup = self ::$ instance ->backup ($ backupId );
190200
191201 $ e = null ;
192- try {
193- $ backup ->create (self ::$ dbName1 , $ expireTime );
194- } catch (BadRequestException $ e ) {
195- } catch (FailedPreconditionException $ e ) {
202+ for ($ i = 0 ; $ i < 3 ; $ i ++) {
203+ try {
204+ $ backup ->create (self ::$ dbName1 , $ expireTime );
205+ break ;
206+ } catch (BadRequestException $ e ) {
207+ break ;
208+ } catch (FailedPreconditionException $ e ) {
209+ break ;
210+ } catch (\Google \Cloud \Core \Exception \ServiceException $ ex ) {
211+ if ($ i === 2 || !in_array ($ ex ->getStatus (), ['UNAVAILABLE ' , 'DEADLINE_EXCEEDED ' ])) {
212+ throw $ ex ;
213+ }
214+ sleep (2 );
215+ }
196216 }
197217
198218 $ this ->assertNotNull ($ e );
@@ -241,7 +261,22 @@ public function testCancelBackupOperation()
241261 self ::$ createTime2 = gmdate ('"Y-m-d\TH:i:s\Z" ' );
242262 $ op = $ backup ->create (self ::$ dbName2 , $ expireTime );
243263
244- $ op ->cancel ();
264+ try {
265+ $ op ->cancel ();
266+ } catch (\Google \ApiCore \ApiException $ e ) {
267+ if ($ e ->getStatus () !== 'DEADLINE_EXCEEDED ' ) {
268+ throw $ e ;
269+ }
270+ }
271+
272+ // Wait until the operation is done so we free up the pending backup slot for self::$dbName2.
273+ // We catch any exception here because the operation might fail (which is expected if cancelled)
274+ // or timeout during polling.
275+ try {
276+ $ op ->pollUntilComplete (['maxPollingDurationSeconds ' => 120 ]);
277+ } catch (\Exception $ e ) {
278+ // Ignore
279+ }
245280
246281 // Cancellation usually drops the backup. We don't assert exists()
247282 // to avoid flakiness with asynchronous deletion.
@@ -586,9 +621,19 @@ public function testRestoreToNewDatabase()
586621 $ this ->assertArrayHasKey ('startTime ' , $ metadata ['progress ' ]);
587622
588623 // Poll for completion with the extended timeout
589- $ op ->pollUntilComplete ([
590- 'maxPollingDurationSeconds ' => self ::LONG_TIMEOUT_SECONDS
591- ]);
624+ $ timeout = time () + self ::LONG_TIMEOUT_SECONDS ;
625+ while (time () < $ timeout ) {
626+ try {
627+ $ op ->pollUntilComplete ([
628+ 'maxPollingDurationSeconds ' => $ timeout - time ()
629+ ]);
630+ break ;
631+ } catch (\Google \ApiCore \ApiException $ e ) {
632+ if ($ e ->getStatus () !== 'DEADLINE_EXCEEDED ' ) {
633+ throw $ e ;
634+ }
635+ }
636+ }
592637 $ restoredDb = $ this ::$ instance ->database ($ restoreDbName );
593638
594639 self ::$ deletionQueue ->add (function () use ($ restoredDb ) {
0 commit comments