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

Throwing error instead of re registering sync on failure #1155

Merged
merged 5 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 5 additions & 4 deletions packages/workbox-background-sync/Queue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,18 @@ class Queue {
replayedRequests.push(replay);
}

await this._runCallback('queueDidReplay', replayedRequests);

// If any requests failed, put the failed requests back in the queue
// and register for another sync.
// and rethrow the failed requests count.
if (failedRequests.length) {
await Promise.all(failedRequests.map((storableRequest) => {
return this._queueStore.addEntry(storableRequest);
}));

await this._registerSync();
throw new WorkboxError('queue-replay-failed',
{name: this._name, count: failedRequests.length});
}

await this._runCallback('queueDidReplay', replayedRequests);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/workbox-core/models/messages/messages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export default {
`registered.`;
},

'queue-replay-failed': ({name, count}) => {
return `${count} requests failed, while trying to replay Queue: ${name}.`;
},

'duplicate-queue-name': ({name}) => {
return `The Queue name '${name}' is already being used. ` +
`All instances of backgroundSync.Queue must be given unique names.`;
Expand Down
46 changes: 29 additions & 17 deletions test/workbox-background-sync/node/lib/test-Queue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {DB_NAME, OBJECT_STORE_NAME} from
import {DBWrapper} from '../../../../packages/workbox-core/_private/DBWrapper.mjs';
import {resetEventListeners} from
'../../../../infra/testing/sw-env-mocks/event-listeners.js';
import {WorkboxError} from '../../../../packages/workbox-core/_private/WorkboxError.mjs';

const getObjectStoreEntries = async () => {
return await new DBWrapper(DB_NAME, 1).getAll(OBJECT_STORE_NAME);
Expand Down Expand Up @@ -293,37 +294,42 @@ describe(`[workbox-background-sync] Queue`, function() {
await queue.addRequest(new Request('/three'));
await queue.addRequest(new Request('/four'));
await queue.addRequest(new Request('/five'));
await queue.replayRequests(); // The 2nd and 4th requests should fail.


const entries = await getObjectStoreEntries();
expect(entries.length).to.equal(2);
expect(entries[0].storableRequest.url).to.equal('/two');
expect(entries[1].storableRequest.url).to.equal('/four');
try {
await queue.replayRequests(); // The 2nd and 4th requests should fail.
} catch (error) {
const entries = await getObjectStoreEntries();
expect(entries.length).to.equal(2);
expect(entries[0].storableRequest.url).to.equal('/two');
expect(entries[1].storableRequest.url).to.equal('/four');
return;
}

throw new Error('should have exit from catch');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use the helper: https://github.com/GoogleChrome/workbox/blob/v3/infra/testing/expectError.js

This will check the error is a WorkboxError with an expected code and it returns a promise, so you can await for the response and check the object store entries after.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

});

it(`should re-register for a sync event if re-fetching fails`,
it(`should throw WorkboxError if re-fetching fails`,
async function() {
sandbox.stub(self.registration, 'sync').value({
register: sinon.stub().resolves(),
});
sandbox.stub(self, 'fetch')
.onCall(1).rejects(new Error())
.callThrough();

const failureURL = '/two';
const queue = new Queue('foo');

// Add requests for both queues to ensure only the requests from
// the matching queue are replayed.
await queue.addRequest(new Request('/one'));
await queue.addRequest(new Request('/two'));
await queue.addRequest(new Request(failureURL));

self.registration.sync.register.reset();
await queue.replayRequests(); // The second request should fail.
try {
await queue.replayRequests(); // The second request should fail.
} catch (error) {
expect(error).to.be.instanceof(WorkboxError);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use expectError helper here as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return;
}

expect(self.registration.sync.register.calledOnce).to.be.true;
expect(self.registration.sync.register.calledWith(
'workbox-background-sync:foo')).to.be.true;
throw new Error('should have exit from catch');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wont be needed after switching to expectError().

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

});

it(`should invoke all replay callbacks`, async function() {
Expand Down Expand Up @@ -376,7 +382,13 @@ describe(`[workbox-background-sync] Queue`, function() {

await queue.addRequest(new Request('/three'));
await queue.addRequest(new Request('/four'));
await queue.replayRequests();
try {
await queue.replayRequests();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use expectError here and make sure the error is what you expect.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} catch (error) {
// Dont do anything as this is expected to be rejected
// but every callback should still be called.
}


expect(requestWillReplay.calledTwice).to.be.true;

Expand Down