Skip to content

Commit 595b36f

Browse files
committed
Cleanup
1 parent ac3c2b4 commit 595b36f

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

packages/storage/src/task.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ export class UploadTask {
9494
private _metadataErrorHandler: (p1: StorageError) => void;
9595
private _resolve?: (p1: UploadTaskSnapshot) => void = undefined;
9696
private _reject?: (p1: StorageError) => void = undefined;
97-
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ // This has to be an "any" type due to incompatibility between Node and browser timers.
98-
private pendingTimeout: any = null;
97+
private pendingTimeout: ReturnType<typeof setTimeout> | null = null;
9998
private _promise: Promise<UploadTaskSnapshot>;
10099

101100
private sleepTime: number;
@@ -415,6 +414,7 @@ export class UploadTask {
415414
}
416415
switch (state) {
417416
case InternalTaskState.CANCELING:
417+
case InternalTaskState.PAUSING:
418418
// TODO(andysoto):
419419
// assert(this.state_ === InternalTaskState.RUNNING ||
420420
// this.state_ === InternalTaskState.PAUSING);
@@ -427,18 +427,6 @@ export class UploadTask {
427427
this.completeTransitions_();
428428
}
429429
break;
430-
case InternalTaskState.PAUSING:
431-
// TODO(andysoto):
432-
// assert(this.state_ === InternalTaskState.RUNNING);
433-
this._state = state;
434-
if (this._request !== undefined) {
435-
this._request.cancel();
436-
} else if (this.pendingTimeout) {
437-
clearTimeout(this.pendingTimeout);
438-
this.pendingTimeout = null;
439-
this.completeTransitions_();
440-
}
441-
break;
442430
case InternalTaskState.RUNNING:
443431
// TODO(andysoto):
444432
// assert(this.state_ === InternalTaskState.PAUSED ||

packages/storage/test/unit/task.test.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { canceled, retryLimitExceeded } from '../../src/implementation/error';
3535
import { SinonFakeTimers, useFakeTimers } from 'sinon';
3636
import * as sinon from 'sinon';
3737
import sinonChai from 'sinon-chai';
38+
import { DEFAULT_MAX_UPLOAD_RETRY_TIME } from '../../src/implementation/constants';
3839

3940
use(sinonChai);
4041

@@ -365,7 +366,7 @@ describe('Firebase Storage > Upload Task', () => {
365366
function handleStateChange(
366367
requestHandler: RequestHandler,
367368
blob: FbsBlob
368-
): { promise: Promise<TotalState>; task: UploadTask } {
369+
): { taskPromise: Promise<TotalState>; task: UploadTask } {
369370
const storageService = storageServiceWithHandler(requestHandler);
370371
const task = new UploadTask(
371372
new Reference(storageService, testLocation),
@@ -414,7 +415,7 @@ describe('Firebase Storage > Upload Task', () => {
414415
}
415416
);
416417

417-
return { promise: deferred.promise, task };
418+
return { taskPromise: deferred.promise, task };
418419
}
419420

420421
it('Calls callback sequences for small uploads correctly', () => {
@@ -426,13 +427,13 @@ describe('Firebase Storage > Upload Task', () => {
426427
it('properly times out if large blobs returns a 503 when finalizing', async () => {
427428
clock = useFakeTimers();
428429
// Kick off upload
429-
const { promise } = handleStateChange(
430+
const { taskPromise } = handleStateChange(
430431
fake503ForFinalizeServerHandler(),
431432
bigBlob
432433
);
433434
// Run all timers
434435
await clock.runAllAsync();
435-
const { events, progress } = await promise;
436+
const { events, progress } = await taskPromise;
436437
expect(events.length).to.equal(2);
437438
expect(events[0]).to.deep.equal({ type: 'resume' });
438439
expect(events[1].type).to.deep.equal('error');
@@ -464,13 +465,13 @@ describe('Firebase Storage > Upload Task', () => {
464465
it('properly times out if large blobs returns a 503 when uploading', async () => {
465466
clock = useFakeTimers();
466467
// Kick off upload
467-
const { promise } = handleStateChange(
468+
const { taskPromise } = handleStateChange(
468469
fake503ForUploadServerHandler(),
469470
bigBlob
470471
);
471472
// Run all timers
472473
await clock.runAllAsync();
473-
const { events, progress } = await promise;
474+
const { events, progress } = await taskPromise;
474475
expect(events.length).to.equal(2);
475476
expect(events[0]).to.deep.equal({ type: 'resume' });
476477
expect(events[1].type).to.deep.equal('error');
@@ -485,34 +486,42 @@ describe('Firebase Storage > Upload Task', () => {
485486
});
486487
clock.restore();
487488
});
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+
} {
489499
clock = useFakeTimers();
490500
const fakeSetTimeout = clock.setTimeout;
501+
491502
let gotFirstEvent = false;
492503

493-
const stub = sinon.stub(global, 'setTimeout');
504+
const stub = sinon.stub(window, 'setTimeout');
494505

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`
497509
stub.callsFake((fn, timeout) => {
498-
// @ts-ignore
499510
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+
}
509518
}
510519
return res;
511520
});
512521
});
513522
readyToCancel.then(
514523
() => stub.restore(),
515-
() => {}
524+
() => stub.restore()
516525
);
517526
return {
518527
...handleStateChange(
@@ -524,12 +533,11 @@ describe('Firebase Storage > Upload Task', () => {
524533
}
525534
it('properly errors with a cancel StorageError if a pending timeout remains', async () => {
526535
// Kick off upload
527-
const { readyToCancel, promise, task } = resumeCancelSetup();
536+
const { readyToCancel, taskPromise: promise, task } = resumeCancelSetup();
528537

529538
await readyToCancel;
530539
task.cancel();
531540

532-
// Run all timers
533541
const { events, progress } = await promise;
534542
expect(events.length).to.equal(2);
535543
expect(events[0]).to.deep.equal({ type: 'resume' });
@@ -548,7 +556,7 @@ describe('Firebase Storage > Upload Task', () => {
548556
});
549557
it('properly errors with a pause StorageError if a pending timeout remains', async () => {
550558
// Kick off upload
551-
const { readyToCancel, promise, task } = resumeCancelSetup();
559+
const { readyToCancel, taskPromise: promise, task } = resumeCancelSetup();
552560

553561
await readyToCancel;
554562

@@ -587,13 +595,13 @@ describe('Firebase Storage > Upload Task', () => {
587595
it('tests if small requests that respond with 500 retry correctly', async () => {
588596
clock = useFakeTimers();
589597
// Kick off upload
590-
const { promise } = handleStateChange(
598+
const { taskPromise } = handleStateChange(
591599
fakeOneShot503ServerHandler(),
592600
smallBlob
593601
);
594602
// Run all timers
595603
await clock.runAllAsync();
596-
const { events, progress } = await promise;
604+
const { events, progress } = await taskPromise;
597605
expect(events.length).to.equal(2);
598606
expect(events[0]).to.deep.equal({ type: 'resume' });
599607
expect(events[1].type).to.deep.equal('error');

0 commit comments

Comments
 (0)