Skip to content

Commit

Permalink
Fix: Retry logic (#808)
Browse files Browse the repository at this point in the history
- Remove incorrect short-circuit-on-retry logic. This short circuit was being called any time you called preview on the same file twice and subsequent loads would always fail because the short circuit happened before an access token was assigned
- Set up fake clock in beforeEach of tests for handleFetchError() - not resetting the timer was causing weird issues
  • Loading branch information
tonyjin authored Jun 19, 2018
1 parent 8791f4e commit 906d47a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
19 changes: 8 additions & 11 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,23 +754,26 @@ class Preview extends EventEmitter {
);
}

// Retry up to RETRY_COUNT if we are reloading same file. If load is called during a preview when file version
// ID has been specified, count as a retry only if the current file verison ID matches that specified file
// version ID
// If file version ID is specified, increment retry count if it matches current file version ID
if (fileVersionId) {
if (fileVersionId === currentFileVersionId) {
this.retryCount += 1;
} else {
this.retryCount = 0;
}

// Otherwise, count this as a retry if the file ID we are trying to load matches the current file ID
// Otherwise, increment retry count if file ID to load matches current file ID
} else if (this.file.id === currentFileId) {
this.retryCount += 1;

// Otherwise, reset retry count
} else {
this.retryCount = 0;
}

// @TODO: This may not be the best way to detect if we are offline. Make sure this works well if we decided to
// combine Box Elements + Preview. This could potentially break if we have Box Elements fetch the file object
// and pass the well-formed file object directly to the preview library to render.
const isPreviewOffline = typeof fileIdOrFile === 'object' && this.options.skipServerUpdate;
if (isPreviewOffline) {
this.handleTokenResponse({});
Expand All @@ -790,12 +793,6 @@ class Preview extends EventEmitter {
* @return {void}
*/
handleTokenResponse(tokenMap) {
// If this is a retry, short-circuit and load from server
if (this.retryCount > 0) {
this.loadFromServer();
return;
}

// Set the authorization token
this.options.token = tokenMap[this.file.id];

Expand Down Expand Up @@ -1379,7 +1376,7 @@ class Preview extends EventEmitter {
// Nuke the cache
uncacheFile(this.cache, this.file);

// Check if hit the retry limit
// Check if we hit the retry limit for fetching file info
if (this.retryCount > RETRY_COUNT) {
let errorCode = ERROR_CODE.EXCEEDED_RETRY_LIMIT;
let errorMessage = __('error_refresh');
Expand Down
17 changes: 7 additions & 10 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,13 +1092,6 @@ describe('lib/Preview', () => {
stubs.cacheFile = sandbox.stub(file, 'cacheFile');
});

it('should short circuit and load from server if it is a retry', () => {
preview.retryCount = 1;

preview.handleTokenResponse({});
expect(stubs.loadFromServer).to.be.called;
});

it('should set the token option', () => {
preview.retryCount = 0;
const TOKEN = 'bar';
Expand Down Expand Up @@ -1999,7 +1992,10 @@ describe('lib/Preview', () => {
});

describe('handleFetchError()', () => {
let clock;

beforeEach(() => {
clock = sandbox.useFakeTimers();
stubs.uncacheFile = sandbox.stub(file, 'uncacheFile');
stubs.triggerError = sandbox.stub(preview, 'triggerError');
stubs.load = sandbox.stub(preview, 'load');
Expand All @@ -2010,6 +2006,10 @@ describe('lib/Preview', () => {
};
});

afterEach(() => {
clock.restore();
});

it('should do nothing if the preview is closed', () => {
preview.file = {
id: '0'
Expand Down Expand Up @@ -2061,7 +2061,6 @@ describe('lib/Preview', () => {
preview.file = {
id: '0'
};
const clock = sinon.useFakeTimers();
preview.open = true;
preview.retryCount = 1;
preview.file.id = 1;
Expand All @@ -2077,7 +2076,6 @@ describe('lib/Preview', () => {
preview.file = {
id: '0'
};
const clock = sinon.useFakeTimers();
preview.open = true;
preview.retryCount = 3;

Expand All @@ -2097,7 +2095,6 @@ describe('lib/Preview', () => {
.withArgs('Retry-After')
.returns(5)
};
const clock = sinon.useFakeTimers();
preview.open = true;
preview.retryCount = 1;

Expand Down

0 comments on commit 906d47a

Please sign in to comment.