Skip to content

Commit

Permalink
fixing log errors
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekbh committed Jun 9, 2017
1 parent 129f4f5 commit 21f41aa
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/workbox-background-sync/src/lib/request-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand Down
37 changes: 34 additions & 3 deletions packages/workbox-background-sync/test/browser/request-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ++;
Expand All @@ -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,
});
Expand All @@ -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);
});
Expand Down
13 changes: 9 additions & 4 deletions packages/workbox-background-sync/test/browser/request-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 21f41aa

Please sign in to comment.