|
| 1 | +var |
| 2 | + should = require('should'), |
| 3 | + rewire = require('rewire'), |
| 4 | + Kuzzle = rewire('../../src/kuzzle'); |
| 5 | + |
| 6 | +describe('Kuzzle: offline queue management', () => { |
| 7 | + var sent; |
| 8 | + |
| 9 | + // deactivate socket.io |
| 10 | + before(function () { |
| 11 | + Kuzzle.__set__('io', function () { |
| 12 | + return { |
| 13 | + emit: function () { sent++; }, |
| 14 | + close: function () {}, |
| 15 | + on: function () {}, |
| 16 | + once: function () {} |
| 17 | + }; |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('#cleanQueue', function () { |
| 22 | + var |
| 23 | + cleanQueue = Kuzzle.__get__('cleanQueue'), |
| 24 | + now = Date.now(), |
| 25 | + kuzzle; |
| 26 | + |
| 27 | + beforeEach(function () { |
| 28 | + var |
| 29 | + pastTime = 60000; |
| 30 | + |
| 31 | + kuzzle = new Kuzzle('foo'); |
| 32 | + |
| 33 | + // queuing a bunch of requests from 1min ago to right now, 10s apart |
| 34 | + while (pastTime >= 0) { |
| 35 | + kuzzle.offlineQueue.push({ts: now - pastTime, query: {}, cb: function () {}}); |
| 36 | + pastTime -= 10000; |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it('should remove outdated queued requests', function () { |
| 41 | + // setting the request TTL to 5s |
| 42 | + kuzzle.queueTTL = 5000; |
| 43 | + |
| 44 | + cleanQueue.call(kuzzle); |
| 45 | + |
| 46 | + // should keep only the latest requests, dating from a few ms ago |
| 47 | + should(kuzzle.offlineQueue.length).be.exactly(1); |
| 48 | + should(kuzzle.offlineQueue[0].ts).be.above(now - kuzzle.queueTTL); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should ignore requests timestamps if queueTTL is 0', function () { |
| 52 | + var numRequests = kuzzle.offlineQueue.length; |
| 53 | + |
| 54 | + kuzzle.queueTTL = 0; |
| 55 | + cleanQueue.call(kuzzle); |
| 56 | + should(kuzzle.offlineQueue.length).be.exactly(numRequests); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should remove older requests until the queueMaxSize condition is met', function () { |
| 60 | + var lastRequest = kuzzle.offlineQueue[kuzzle.offlineQueue.length - 1]; |
| 61 | + |
| 62 | + kuzzle.queueMaxSize = 1; |
| 63 | + cleanQueue.call(kuzzle); |
| 64 | + should(kuzzle.offlineQueue.length).be.exactly(1); |
| 65 | + should(kuzzle.offlineQueue[0]).match(lastRequest); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not remove any request if queueMaxSize is 0', function () { |
| 69 | + var numRequests = kuzzle.offlineQueue.length; |
| 70 | + |
| 71 | + kuzzle.queueMaxSize = 0; |
| 72 | + cleanQueue.call(kuzzle); |
| 73 | + should(kuzzle.offlineQueue.length).be.exactly(numRequests); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('#dequeuing', function () { |
| 78 | + var |
| 79 | + dequeue = Kuzzle.__get__('dequeue'), |
| 80 | + now = Date.now(), |
| 81 | + kuzzle; |
| 82 | + |
| 83 | + beforeEach(function () { |
| 84 | + var |
| 85 | + pastTime = 60000; |
| 86 | + |
| 87 | + sent = 0; |
| 88 | + kuzzle = new Kuzzle('foo'); |
| 89 | + kuzzle.socket = { emit: function () { sent++; } }; |
| 90 | + kuzzle.queuing = true; |
| 91 | + |
| 92 | + // queuing a bunch of requests from 1min ago to right now, 10s apart |
| 93 | + while (pastTime >= 0) { |
| 94 | + kuzzle.offlineQueue.push({ts: now - pastTime, query: {}}); |
| 95 | + pastTime -= 10000; |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it('should play all queued requests', function (done) { |
| 100 | + var numRequests = kuzzle.offlineQueue.length; |
| 101 | + |
| 102 | + this.timeout(200); |
| 103 | + dequeue.call(kuzzle); |
| 104 | + |
| 105 | + setTimeout(() => { |
| 106 | + should(sent).be.exactly(numRequests); |
| 107 | + should(kuzzle.offlineQueue).be.an.Array(); |
| 108 | + should(kuzzle.offlineQueue.length).be.exactly(0); |
| 109 | + should(kuzzle.queuing).be.false(); |
| 110 | + done(); |
| 111 | + }, numRequests * kuzzle.replayInterval + 10) |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments