@@ -35,6 +35,7 @@ import { canceled, retryLimitExceeded } from '../../src/implementation/error';
35
35
import { SinonFakeTimers , useFakeTimers } from 'sinon' ;
36
36
import * as sinon from 'sinon' ;
37
37
import sinonChai from 'sinon-chai' ;
38
+ import { DEFAULT_MAX_UPLOAD_RETRY_TIME } from '../../src/implementation/constants' ;
38
39
39
40
use ( sinonChai ) ;
40
41
@@ -365,7 +366,7 @@ describe('Firebase Storage > Upload Task', () => {
365
366
function handleStateChange (
366
367
requestHandler : RequestHandler ,
367
368
blob : FbsBlob
368
- ) : { promise : Promise < TotalState > ; task : UploadTask } {
369
+ ) : { taskPromise : Promise < TotalState > ; task : UploadTask } {
369
370
const storageService = storageServiceWithHandler ( requestHandler ) ;
370
371
const task = new UploadTask (
371
372
new Reference ( storageService , testLocation ) ,
@@ -414,7 +415,7 @@ describe('Firebase Storage > Upload Task', () => {
414
415
}
415
416
) ;
416
417
417
- return { promise : deferred . promise , task } ;
418
+ return { taskPromise : deferred . promise , task } ;
418
419
}
419
420
420
421
it ( 'Calls callback sequences for small uploads correctly' , ( ) => {
@@ -426,13 +427,13 @@ describe('Firebase Storage > Upload Task', () => {
426
427
it ( 'properly times out if large blobs returns a 503 when finalizing' , async ( ) => {
427
428
clock = useFakeTimers ( ) ;
428
429
// Kick off upload
429
- const { promise } = handleStateChange (
430
+ const { taskPromise } = handleStateChange (
430
431
fake503ForFinalizeServerHandler ( ) ,
431
432
bigBlob
432
433
) ;
433
434
// Run all timers
434
435
await clock . runAllAsync ( ) ;
435
- const { events, progress } = await promise ;
436
+ const { events, progress } = await taskPromise ;
436
437
expect ( events . length ) . to . equal ( 2 ) ;
437
438
expect ( events [ 0 ] ) . to . deep . equal ( { type : 'resume' } ) ;
438
439
expect ( events [ 1 ] . type ) . to . deep . equal ( 'error' ) ;
@@ -464,13 +465,13 @@ describe('Firebase Storage > Upload Task', () => {
464
465
it ( 'properly times out if large blobs returns a 503 when uploading' , async ( ) => {
465
466
clock = useFakeTimers ( ) ;
466
467
// Kick off upload
467
- const { promise } = handleStateChange (
468
+ const { taskPromise } = handleStateChange (
468
469
fake503ForUploadServerHandler ( ) ,
469
470
bigBlob
470
471
) ;
471
472
// Run all timers
472
473
await clock . runAllAsync ( ) ;
473
- const { events, progress } = await promise ;
474
+ const { events, progress } = await taskPromise ;
474
475
expect ( events . length ) . to . equal ( 2 ) ;
475
476
expect ( events [ 0 ] ) . to . deep . equal ( { type : 'resume' } ) ;
476
477
expect ( events [ 1 ] . type ) . to . deep . equal ( 'error' ) ;
@@ -485,34 +486,42 @@ describe('Firebase Storage > Upload Task', () => {
485
486
} ) ;
486
487
clock . restore ( ) ;
487
488
} ) ;
488
- function resumeCancelSetup ( ) : any {
489
+
490
+ /**
491
+ * Starts upload, finds the first instance of an exponential backoff, and resolves `readyToCancel` when done.
492
+ * @returns readyToCancel, taskPromise and task
493
+ */
494
+ function resumeCancelSetup ( ) : {
495
+ readyToCancel : Promise < null > ;
496
+ taskPromise : Promise < TotalState > ;
497
+ task : UploadTask ;
498
+ } {
489
499
clock = useFakeTimers ( ) ;
490
500
const fakeSetTimeout = clock . setTimeout ;
501
+
491
502
let gotFirstEvent = false ;
492
503
493
- const stub = sinon . stub ( global , 'setTimeout' ) ;
504
+ const stub = sinon . stub ( window , 'setTimeout' ) ;
494
505
495
- const timers : { [ key : number ] : any } = { } ;
496
- const readyToCancel = new Promise ( resolve => {
506
+ // Function that notifies when we are in the middle of an exponential backoff
507
+ const readyToCancel = new Promise < null > ( resolve => {
508
+ // @ts -ignore The types for `stub.callsFake` is incompatible with types of `clock.setTimeout`
497
509
stub . callsFake ( ( fn , timeout ) => {
498
- // @ts -ignore
499
510
const res = fakeSetTimeout ( fn , timeout ) ;
500
- //@ts -ignore
501
- timers [ res . id ] = {
502
- data : res ,
503
- timeout
504
- } ;
505
- if ( ( ! gotFirstEvent || timeout === 0 ) && timeout !== 600000 ) {
506
- clock . tick ( timeout as number ) ;
507
- } else if ( timeout !== 600000 ) {
508
- resolve ( null ) ;
511
+ if ( timeout !== DEFAULT_MAX_UPLOAD_RETRY_TIME ) {
512
+ if ( ! gotFirstEvent || timeout === 0 ) {
513
+ clock . tick ( timeout as number ) ;
514
+ } else {
515
+ // If the timeout isn't 0 and it isn't the max upload retry time, it's most likely due to exponential backoff.
516
+ resolve ( null ) ;
517
+ }
509
518
}
510
519
return res ;
511
520
} ) ;
512
521
} ) ;
513
522
readyToCancel . then (
514
523
( ) => stub . restore ( ) ,
515
- ( ) => { }
524
+ ( ) => stub . restore ( )
516
525
) ;
517
526
return {
518
527
...handleStateChange (
@@ -524,12 +533,11 @@ describe('Firebase Storage > Upload Task', () => {
524
533
}
525
534
it ( 'properly errors with a cancel StorageError if a pending timeout remains' , async ( ) => {
526
535
// Kick off upload
527
- const { readyToCancel, promise, task } = resumeCancelSetup ( ) ;
536
+ const { readyToCancel, taskPromise : promise , task } = resumeCancelSetup ( ) ;
528
537
529
538
await readyToCancel ;
530
539
task . cancel ( ) ;
531
540
532
- // Run all timers
533
541
const { events, progress } = await promise ;
534
542
expect ( events . length ) . to . equal ( 2 ) ;
535
543
expect ( events [ 0 ] ) . to . deep . equal ( { type : 'resume' } ) ;
@@ -548,7 +556,7 @@ describe('Firebase Storage > Upload Task', () => {
548
556
} ) ;
549
557
it ( 'properly errors with a pause StorageError if a pending timeout remains' , async ( ) => {
550
558
// Kick off upload
551
- const { readyToCancel, promise, task } = resumeCancelSetup ( ) ;
559
+ const { readyToCancel, taskPromise : promise , task } = resumeCancelSetup ( ) ;
552
560
553
561
await readyToCancel ;
554
562
@@ -587,13 +595,13 @@ describe('Firebase Storage > Upload Task', () => {
587
595
it ( 'tests if small requests that respond with 500 retry correctly' , async ( ) => {
588
596
clock = useFakeTimers ( ) ;
589
597
// Kick off upload
590
- const { promise } = handleStateChange (
598
+ const { taskPromise } = handleStateChange (
591
599
fakeOneShot503ServerHandler ( ) ,
592
600
smallBlob
593
601
) ;
594
602
// Run all timers
595
603
await clock . runAllAsync ( ) ;
596
- const { events, progress } = await promise ;
604
+ const { events, progress } = await taskPromise ;
597
605
expect ( events . length ) . to . equal ( 2 ) ;
598
606
expect ( events [ 0 ] ) . to . deep . equal ( { type : 'resume' } ) ;
599
607
expect ( events [ 1 ] . type ) . to . deep . equal ( 'error' ) ;
0 commit comments