Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Retry logic #808

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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