Skip to content

Commit a2ce7ab

Browse files
DahadenDave
andauthored
#17 Modified reclaim to run on original engine with tests (#21)
* #17 Modified reclaim to run on original engine with tests * #17 Removed blank line causing lint error Co-authored-by: Dave <[email protected]>
1 parent a545a59 commit a2ce7ab

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Queue.prototype._checkReclaim = function() {
276276

277277
function findOtherQueues(name) {
278278
var res = [];
279-
var storage = self._store.engine;
279+
var storage = self._store.getOriginalEngine();
280280
for (var i = 0; i < storage.length; i++) {
281281
var k = storage.key(i);
282282
var parts = k.split('.');

lib/store.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function Store(name, id, keys, optionalEngine) {
1515
this.name = name;
1616
this.keys = keys || {};
1717
this.engine = optionalEngine || defaultEngine;
18+
this.originalEngine = this.engine;
1819
}
1920

2021
/**
@@ -52,6 +53,14 @@ Store.prototype.get = function(key) {
5253
}
5354
};
5455

56+
/**
57+
* Get original engine
58+
*/
59+
60+
Store.prototype.getOriginalEngine = function() {
61+
return this.originalEngine;
62+
};
63+
5564
/**
5665
* Remove by Key.
5766
*/

test/index.test.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,97 @@ describe('Queue', function() {
210210
assert(queue.fn.calledWith('b'));
211211
});
212212

213+
describe('while using in memory engine', function() {
214+
beforeEach(function() {
215+
queue._store._swapEngine();
216+
});
217+
218+
it('should take over a queued task if a queue is abandoned', function() {
219+
// a wild queue of interest appears
220+
var foundQueue = new Store('test', 'fake-id', queue.keys);
221+
foundQueue.set(foundQueue.keys.ACK, 0); // fake timers starts at time 0
222+
foundQueue.set(foundQueue.keys.QUEUE, [{
223+
item: 'a',
224+
time: 0,
225+
attemptNumber: 0
226+
}]);
227+
228+
// wait for the queue to expire
229+
clock.tick(queue.timeouts.RECLAIM_TIMEOUT);
230+
231+
queue.start();
232+
233+
// wait long enough for the other queue to expire and be reclaimed
234+
clock.tick(
235+
queue.timeouts.RECLAIM_TIMER
236+
+ queue.timeouts.RECLAIM_WAIT * 2
237+
);
238+
239+
assert(queue.fn.calledOnce);
240+
assert(queue.fn.calledWith('a'));
241+
});
242+
243+
it('should take over an in-progress task if a queue is abandoned', function() {
244+
// set up a fake queue
245+
var foundQueue = new Store('test', 'fake-id', queue.keys);
246+
foundQueue.set(foundQueue.keys.ACK, -15000);
247+
foundQueue.set(foundQueue.keys.IN_PROGRESS, {
248+
'task-id': {
249+
item: 'a',
250+
time: 0,
251+
attemptNumber: 0
252+
}
253+
});
254+
255+
// wait for the queue to expire
256+
clock.tick(queue.timeouts.RECLAIM_TIMEOUT);
257+
258+
queue.start();
259+
260+
// wait long enough for the other queue to expire and be reclaimed
261+
clock.tick(
262+
queue.timeouts.RECLAIM_TIMER
263+
+ queue.timeouts.RECLAIM_WAIT * 2
264+
);
265+
266+
assert(queue.fn.calledOnce);
267+
assert(queue.fn.calledWith('a'));
268+
});
269+
270+
it('should take over multiple tasks if a queue is abandoned', function() {
271+
// set up a fake queue
272+
var foundQueue = new Store('test', 'fake-id', queue.keys);
273+
foundQueue.set(foundQueue.keys.ACK, -15000);
274+
foundQueue.set(foundQueue.keys.QUEUE, [{
275+
item: 'a',
276+
time: 0,
277+
attemptNumber: 0
278+
}]);
279+
foundQueue.set(foundQueue.keys.IN_PROGRESS, {
280+
'task-id': {
281+
item: 'b',
282+
time: 1,
283+
attemptNumber: 0
284+
}
285+
});
286+
287+
// wait for the queue to expire
288+
clock.tick(queue.timeouts.RECLAIM_TIMEOUT);
289+
290+
queue.start();
291+
292+
// wait long enough for the other queue to expire and be reclaimed
293+
clock.tick(
294+
queue.timeouts.RECLAIM_TIMER
295+
+ queue.timeouts.RECLAIM_WAIT * 2
296+
);
297+
298+
assert(queue.fn.calledTwice);
299+
assert(queue.fn.calledWith('a'));
300+
assert(queue.fn.calledWith('b'));
301+
});
302+
});
303+
213304
it('should respect maxAttempts when rejected', function() {
214305
var calls = new Array(100);
215306

test/store.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ describe('Store', function() {
8686
assert.strictEqual(store.engine, inMemoryEngine);
8787
});
8888

89+
it('should not switch the original storage mechanism', function() {
90+
assert.strictEqual(store.getOriginalEngine(), engine);
91+
store._swapEngine();
92+
assert.strictEqual(store.getOriginalEngine(), engine);
93+
});
94+
8995
it('should swap upon quotaExceeded on set', function() {
9096
var lsProxy = {
9197
length: window.localStorage.length,

0 commit comments

Comments
 (0)