simple-in-memory-queue
Version:
A simple in-memory queue, for nodejs and the browser, with consumers for common usecases.
74 lines • 3.52 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const sleep_1 = require("../../utils/sleep");
const createQueueWithBatchConsumer_1 = require("./createQueueWithBatchConsumer");
describe('createQueueWithBatchConsumer', () => {
beforeEach(() => jest.resetAllMocks());
it('should invoke the consumer after time threshold exceeded', () => __awaiter(void 0, void 0, void 0, function* () {
const mockedConsumer = jest.fn();
const queue = (0, createQueueWithBatchConsumer_1.createQueueWithBatchConsumer)({
threshold: { milliseconds: 100, size: 5 },
consumer: mockedConsumer,
});
// add to queue
queue.push('a');
// prove not invoked yet
expect(mockedConsumer).not.toHaveBeenCalled();
// add to queue again
queue.push(['b', 'c', 'd']);
// prove not invoked yet
expect(mockedConsumer).not.toHaveBeenCalled();
// wait 100ms
yield (0, sleep_1.sleep)(110);
// prove invoked
expect(mockedConsumer).toHaveBeenCalled();
expect(mockedConsumer).toHaveBeenCalledTimes(1);
expect(mockedConsumer).toHaveBeenCalledWith({
items: ['a', 'b', 'c', 'd'],
});
// prove not invoked again redundantly, if 3 more items added, for a total historical add of 7
queue.push(['e', 'f', 'g']);
expect(mockedConsumer).toHaveBeenCalledTimes(1); // still only 1 call
// wait 100ms more
yield (0, sleep_1.sleep)(110);
// prove invoked again
expect(mockedConsumer).toHaveBeenCalled();
expect(mockedConsumer).toHaveBeenCalledTimes(2);
expect(mockedConsumer).toHaveBeenCalledWith({
items: ['e', 'f', 'g'],
});
}));
it('should invoke the consumer after size threshold exceeded', () => __awaiter(void 0, void 0, void 0, function* () {
const mockedConsumer = jest.fn();
const queue = (0, createQueueWithBatchConsumer_1.createQueueWithBatchConsumer)({
threshold: { milliseconds: 100, size: 5 },
consumer: mockedConsumer,
});
// add to queue
queue.push(['a', 'b', 'c', 'd']);
// prove not invoked yet
expect(mockedConsumer).not.toHaveBeenCalled();
// add to queue again
queue.push('e');
// prove invoked now
expect(mockedConsumer).toHaveBeenCalled();
expect(mockedConsumer).toHaveBeenCalledTimes(1);
expect(mockedConsumer).toHaveBeenCalledWith({
items: ['a', 'b', 'c', 'd', 'e'],
});
// wait 100ms
yield (0, sleep_1.sleep)(110);
// prove not invoked again redundantly
expect(mockedConsumer).toHaveBeenCalledTimes(1);
}));
});
//# sourceMappingURL=createQueueWithBatchConsumer.test.js.map