forked from goldbergyoni/nodejs-testing-best-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage-queue-client.js
195 lines (166 loc) · 5.62 KB
/
message-queue-client.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const amqplib = require('amqplib');
const { EventEmitter } = require('events');
const { throwIfEmpty } = require('rxjs/operators');
const { AppError, errorHandler } = require('../error-handling');
const { FakeMessageQueueProvider } = require('./fake-message-queue-provider');
// This is a simplistic client for a popular message queue product - RabbitMQ
// It's generic in order to be used by any service in the organization
class MessageQueueClient extends EventEmitter {
constructor(customMessageQueueProvider) {
super();
this.isReady = false;
this.requeue = true; // Tells whether to return failed messages to the queue
// To facilitate testing, the client allows working with a fake MQ provider
// It can get one in the constructor here or even change by environment variables
if (customMessageQueueProvider) {
this.messageQueueProvider = customMessageQueueProvider;
} else if (process.env.USE_FAKE_MQ === 'true') {
this.messageQueueProvider = new FakeMessageQueueProvider();
} else {
this.messageQueueProvider = new FakeMessageQueueProvider();
}
this.countEvents();
}
// This function stores all the MQ events in a local data structure so later
// one query this
countEvents() {
if (this.recordingStarted === true) {
return; // Already initialized and set up
}
this.records = {
ack: { count: 0, lastEventData: null },
nack: { count: 0, lastEventData: null },
publish: { count: 0, lastEventData: null },
consume: { count: 0, lastEventData: null },
};
['nack', 'ack', 'publish'].forEach((eventToListenTo) => {
this.on(eventToListenTo, (eventData) => {
this.records[eventToListenTo].count++;
this.records[eventToListenTo].lastEventData = eventData;
this.emit('message-queue-event', {
name: eventToListenTo,
eventsRecorder: this.recordingStarted,
});
});
});
}
// Helper methods for testing - Resolves/fires when some event happens
async waitFor(eventName, howMuch) {
return new Promise((resolve, reject) => {
// The first resolve is for cases where the caller has approached AFTER the event has already happen
this.resolveIfEventExceededThreshold(eventName, howMuch, resolve);
this.on('message-queue-event', (eventInfo) => {
this.resolveIfEventExceededThreshold(eventName, howMuch, resolve);
});
});
}
async connect() {
const connectionProperties = {
protocol: 'amqp',
hostname: 'localhost',
port: 5672,
username: 'guest',
password: 'guest', // This is a demo app, no security considerations. This is the password for the local dev server
locale: 'en_US',
frameMax: 0,
heartbeat: 0,
vhost: '/',
};
this.connection = await this.messageQueueProvider.connect(
connectionProperties
);
this.channel = await this.connection.createChannel();
}
async close() {
if (this.connection) {
await this.connection.close();
}
return;
}
async sendMessage(queueName, message) {
if (!this.channel) {
await this.connect();
}
await this.channel.assertQueue(queueName);
const sendResponse = await this.channel.sendToQueue(
queueName,
Buffer.from(JSON.stringify(message))
);
return sendResponse;
}
async publish(exchangeName, routingKey, message, messageId) {
if (!this.channel) {
await this.connect();
}
const sendResponse = await this.channel.publish(
exchangeName,
routingKey,
Buffer.from(JSON.stringify(message)),
{ messageId }
);
this.emit('publish', { exchangeName, routingKey, message });
return sendResponse;
}
async deleteQueue(queueName) {
if (!this.channel) {
await this.connect();
}
const queueDeletionResult = await this.channel.deleteQueue(queueName);
return;
}
async assertQueue(queueName) {
if (!this.channel) {
await this.connect();
}
await this.channel.assertQueue(queueName);
return;
}
async assertExchange(name, type) {
if (!this.channel) {
await this.connect();
}
await this.channel.assertExchange(name, type, { durable: false });
return;
}
async bindQueue(queueToBind, exchangeToBindTo, bindingPattern) {
if (!this.channel) {
await this.connect();
}
await this.channel.bindQueue(queueToBind, exchangeToBindTo, bindingPattern);
return;
}
async consume(queueName, onMessageCallback) {
if (!this.channel) {
await this.connect();
}
this.channel.assertQueue(queueName);
await this.channel.consume(queueName, async (theNewMessage) => {
//Not awaiting because some MQ client implementation get back to fetch messages again only after handling a message
onMessageCallback(theNewMessage.content.toString())
.then(() => {
this.emit('ack', theNewMessage);
this.channel.ack(theNewMessage);
})
.catch((error) => {
this.channel.nack(theNewMessage, false, this.requeue);
this.emit('nack', theNewMessage);
error.isTrusted = true; //Since it's related to a single message, there is no reason to let the process crash
//errorHandler.handleError(error);
});
});
return;
}
setRequeue(newValue) {
this.requeue = newValue;
}
resolveIfEventExceededThreshold(eventName, threshold, resolve) {
if (this.records[eventName].count >= threshold) {
resolve({
name: eventName,
lastEventData: this.records[eventName].lastEventData,
count: this.records[eventName].count,
});
}
}
}
module.exports = MessageQueueClient;