diff --git a/packages/workbox-background-sync/src/lib/request-manager.js b/packages/workbox-background-sync/src/lib/request-manager.js index c3a43fb9ef..cc183791b7 100644 --- a/packages/workbox-background-sync/src/lib/request-manager.js +++ b/packages/workbox-background-sync/src/lib/request-manager.js @@ -81,7 +81,9 @@ class RequestManager { } /** - * function to start playing requests in sequence + * This function is to be called to replay all the requests + * in the current queue. It will play all the requests and return a promise + * based on the successfull execution of the requests. * * @return {Promise} Resolves if all requests are played successfully, * rejects if any of the request fails during the replay diff --git a/packages/workbox-background-sync/test/browser/background-sync-queue.js b/packages/workbox-background-sync/test/browser/background-sync-queue.js index e7c239e4d2..65c9b46027 100644 --- a/packages/workbox-background-sync/test/browser/background-sync-queue.js +++ b/packages/workbox-background-sync/test/browser/background-sync-queue.js @@ -15,6 +15,10 @@ /* global chai, workbox */ 'use strict'; +const testServerGen = require('../../../../utils/test-server-generator.js'); +const path = require('path'); +const fs = require('fs'); +const fsExtra = require('fs-extra'); function delay(timeout) { return new Promise((resolve, reject) => { @@ -39,6 +43,30 @@ describe('background sync queue test', () => { }; let backgroundSyncQueue; + let tmpDirectory; + let testServer; + let baseTestUrl; + + before(function() { + tmpDirectory = fs.mkdtempSync( + path.join(__dirname, 'tmp-') + ); + + testServer = testServerGen(); + return testServer.start(tmpDirectory, 5050) + .then((portNumber) => { + baseTestUrl = `http://localhost:${portNumber}`; + }); + }); + + // Kill the web server once all tests are complete. + after(function() { + this.timeout(10 * 1000); + + fsExtra.removeSync(tmpDirectory); + + return testServer.stop(); + }); beforeEach(async ()=>{ responseAchieved = 0; @@ -74,20 +102,20 @@ describe('background sync queue test', () => { }); it('check push proxy', async () => { - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); chai.assert.equal(backgroundSyncQueue._queue.queue.length, 1); }); it('check replay', async function() { - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); await backgroundSyncQueue.replayRequests(); chai.assert.equal(responseAchieved, 2); }); it('check replay failure with rejected promise', async function() { - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__test/404')}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__test/404`)}); try { await backgroundSyncQueue.replayRequests(); throw new Error('Replay should have failed because of invalid URL'); @@ -111,9 +139,9 @@ describe('background sync queue test', () => { await backgroundSyncQueue.cleanupQueue(); await backgroundSyncQueue2.cleanupQueue(); - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); - await backgroundSyncQueue2.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); + await backgroundSyncQueue2.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); const queue1Keys = (await backgroundSyncQueue._queue._idbQDb.getAllKeys()); const queue2Keys = (await backgroundSyncQueue2._queue._idbQDb.getAllKeys()); await delay(100); diff --git a/packages/workbox-background-sync/test/browser/request-manager.js b/packages/workbox-background-sync/test/browser/request-manager.js index bc9dcbeb1f..59f9c52f2c 100644 --- a/packages/workbox-background-sync/test/browser/request-manager.js +++ b/packages/workbox-background-sync/test/browser/request-manager.js @@ -15,9 +15,36 @@ /* global chai, workbox */ 'use strict'; - +const testServerGen = require('../../../../utils/test-server-generator.js'); +const path = require('path'); +const fs = require('fs'); +const fsExtra = require('fs-extra'); +const IDBHelper = require('../../../../lib/idb-helper'); describe('request-manager test', () => { + let tmpDirectory; + let testServer; + let baseTestUrl; let responseAchieved = 0; + before(function() { + tmpDirectory = fs.mkdtempSync( + path.join(__dirname, 'tmp-') + ); + + testServer = testServerGen(); + return testServer.start(tmpDirectory, 5050) + .then((portNumber) => { + baseTestUrl = `http://localhost:${portNumber}`; + }); + }); + + // Kill the web server once all tests are complete. + after(function() { + this.timeout(10 * 1000); + + fsExtra.removeSync(tmpDirectory); + + return testServer.stop(); + }); const callbacks = { onResponse: function() { responseAchieved ++; @@ -27,11 +54,15 @@ describe('request-manager test', () => { let queue; let reqManager; + const idbHelper = new IDBHelper( + 'bgQueueSyncDB', 1, 'QueueStore'); + before( (done) => { const QUEUE_NAME = 'QUEUE_NAME'; const MAX_AGE = 6; queue = new workbox.backgroundSync.test.RequestQueue({ + idbQDb: idbHelper, config: {maxAge: MAX_AGE}, queueName: QUEUE_NAME, }); @@ -57,8 +88,8 @@ describe('request-manager test', () => { = new workbox.backgroundSync.test.BackgroundSyncQueue({ callbacks, }); - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); - await backgroundSyncQueue.pushIntoQueue({request: new Request('http://localhost:3000/__echo/counter')}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); + await backgroundSyncQueue.pushIntoQueue({request: new Request(`${baseTestUrl}/__echo/counter`)}); await backgroundSyncQueue._requestManager.replayRequests(); chai.assert.equal(responseAchieved, 2); }); diff --git a/packages/workbox-background-sync/test/browser/request-queue.js b/packages/workbox-background-sync/test/browser/request-queue.js index ed8b8d8217..368fc1a70f 100644 --- a/packages/workbox-background-sync/test/browser/request-queue.js +++ b/packages/workbox-background-sync/test/browser/request-queue.js @@ -15,12 +15,13 @@ /* global chai, workbox */ 'use strict'; +const IDBHelper = require('../../../../lib/idb-helper'); describe('request-queue tests', () => { const QUEUE_NAME = 'QUEUE_NAME'; const MAX_AGE = 6; - const idbHelper = new workbox.backgroundSync.test.IdbHelper( - 'bgQueueSyncDB', 1, 'QueueStore'); + const idbHelper = new IDBHelper( + 'bgQueueSyncDB', 1, 'QueueStore'); let queue = new workbox.backgroundSync.test.RequestQueue({ idbQDb: idbHelper, @@ -54,8 +55,12 @@ describe('request-queue tests', () => { }); it('default config is correct', () => { - let tempQueue = new workbox.backgroundSync.test.RequestQueue({}); - let tempQueue2 = new workbox.backgroundSync.test.RequestQueue({}); + let tempQueue = new workbox.backgroundSync.test.RequestQueue({ + idbQDb: idbHelper, + }); + let tempQueue2 = new workbox.backgroundSync.test.RequestQueue({ + idbQDb: idbHelper, + }); chai.assert.equal(tempQueue._config, undefined); chai.assert.equal(tempQueue._queueName, workbox.backgroundSync.test.Constants.defaultQueueName + '_0');