/
hooks.js
77 lines (67 loc) · 1.78 KB
/
hooks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
const sinon = require('sinon');
const config = require('../common/config');
const redisSMQ = require('../../index');
const redisClient = require('./../../src/redis-client');
const Consumer = redisSMQ.Consumer;
const Producer = redisSMQ.Producer;
const client = redisClient.getNewInstance(config);
const consumersList = [];
const producersList = [];
function clean(set, cb) {
if (set.length) {
let item = set.pop();
const onStopped = () => {
item = null;
clean(set, cb);
};
if (item.stop) {
if (item.isRunning()) {
item.on('halt', onStopped);
item.stop();
} else onStopped();
} else {
item.shutdown();
onStopped();
}
} else cb();
}
function getConsumer(queueName = 'test_queue', options = {}) {
const TemplateClass = class extends Consumer {
consume(message, cb) {
cb();
}
};
TemplateClass.queueName = queueName;
const consumer = new TemplateClass(config, options);
consumersList.push(consumer);
return consumer;
}
function getProducer(queueName = 'test_queue') {
const producer = new Producer(queueName, config);
producersList.push(producer);
return producer;
}
before(function (done) {
this.sandbox = sinon.sandbox.create();
this.sandbox.getConsumer = getConsumer;
this.sandbox.getProducer = getProducer;
done();
});
after(function (done) {
client.end(true);
done();
});
beforeEach(function (done) {
this.sandbox.restore();
client.flushall((err) => {
if (err) throw err;
done();
});
});
afterEach(function (done) {
this.timeout(40000);
clean(consumersList, () => {
clean(producersList, done);
});
});