/
AmqpConnectionManagerTest.ts
356 lines (292 loc) · 13.1 KB
/
AmqpConnectionManagerTest.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import origAmqp from 'amqplib';
import chai from 'chai';
import chaiString from 'chai-string';
import { once } from 'events';
import * as promiseTools from 'promise-tools';
import AmqpConnectionManager from '../src/AmqpConnectionManager';
import { FakeAmqp, FakeConnection } from './fixtures';
chai.use(chaiString);
const { expect } = chai;
const amqplib = new FakeAmqp();
describe('AmqpConnectionManager', function () {
let amqp: AmqpConnectionManager | undefined;
beforeEach(() => {
jest.spyOn(origAmqp, 'connect').mockImplementation(((url: string) =>
amqplib.connect(url)) as any);
amqplib.reset();
});
afterEach(() => {
amqp?.close();
jest.restoreAllMocks();
});
it('should establish a connection to a broker', async () => {
amqp = new AmqpConnectionManager('amqp://localhost');
amqp.connect();
const [{ connection, url }] = await once(amqp, 'connect');
expect(url, 'url').to.equal('amqp://localhost');
expect(connection.url, 'connection.url').to.equal('amqp://localhost?heartbeat=5');
});
it('should establish a connection to a broker, using an object as the URL', async () => {
amqp = new AmqpConnectionManager({
protocol: 'amqp',
hostname: 'localhost',
});
amqp.connect();
const [{ connection, url }] = await once(amqp, 'connect');
expect(url, 'url').to.eql({
protocol: 'amqp',
hostname: 'localhost',
});
expect((connection as any).url, 'connection.url').to.eql({
protocol: 'amqp',
hostname: 'localhost',
heartbeat: 5,
});
});
it('should establish a url object based connection to a broker', async () => {
amqp = new AmqpConnectionManager({ url: 'amqp://localhost' });
amqp.connect();
const [{ connection, url }] = await once(amqp, 'connect');
expect(url, 'url').to.equal('amqp://localhost');
expect(connection.url, 'connection.url').to.equal('amqp://localhost?heartbeat=5');
});
it('should establish a connection to a broker disabling heartbeat', async () => {
amqp = new AmqpConnectionManager('amqp://localhost', {
heartbeatIntervalInSeconds: 0,
});
amqp.connect();
const [{ connection, url }] = await once(amqp, 'connect');
expect(url, 'url').to.equal('amqp://localhost');
expect(connection.url, 'connection.url').to.equal('amqp://localhost?heartbeat=0');
});
it('should close connection to a broker', async () => {
amqp = new AmqpConnectionManager('amqp://localhost');
amqp.connect();
const [{ connection, url }] = await once(amqp, 'connect');
expect(url, 'url').to.equal('amqp://localhost');
expect((connection as any).url, 'connection.url').to.equal('amqp://localhost?heartbeat=5');
const conn = amqp.connection;
await amqp?.close();
expect(amqp?.connection, 'current connection').to.be.undefined;
expect((conn as any)._closed, 'connection closed').to.be.true;
});
// /**
// * When close() was called before _connect() finished, the connection was remaining established indefinitely
// */
it('should close pending connection to a broker', async () => {
let closed = false;
let connected = false;
amqp = new AmqpConnectionManager('amqp://localhost');
amqp.connect();
// Connection should not yet be established
expect(amqp.connection, 'current connection').to.equal(undefined);
// Connection should be pending though
expect((amqp as any)._connectPromise).to.be.an.instanceof(Promise);
// Call close before the connection is established
const closePromise = amqp.close().then(() => {
closed = true;
// Connection should not present after close
expect(amqp?.connection, 'current connection').to.be.undefined;
// Connection promise should not be present anymore
expect((amqp as any)._connectPromise).to.be.undefined;
// Connect should resolve before close
expect(connected).to.equal(true);
});
// This prevents double call to close()
expect((amqp as any)._closed).to.equal(true);
// Wait for connect before checking amqp?.connection
const connectPromise = new Promise((resolve, reject) => {
// I tried to use once helper from events module but
// does not work with babel for some reason
amqp?.once('connect', resolve);
amqp?.once('error', reject);
}).then(() => {
connected = true;
// Connection should be present right after connect
expect(amqp?.connection, 'current connection').to.be.an.instanceof(FakeConnection);
// Connection promise should not be present anymore
expect((amqp as any)._connectPromise).to.be.undefined;
// Connect should resolve before close
expect(closed).to.equal(false);
});
await Promise.all([closePromise, connectPromise]);
});
it('should establish a connection to a broker using findServers', async () => {
amqp = new AmqpConnectionManager(null, {
findServers() {
return Promise.resolve('amqp://localhost');
},
});
amqp.connect();
const [{ connection, url }] = await once(amqp, 'connect');
expect(url, 'url').to.equal('amqp://localhost');
expect(connection.url, 'connection.url').to.equal('amqp://localhost?heartbeat=5');
});
it('should establish a url object based connection to a broker using findServers', async () => {
amqp = new AmqpConnectionManager(null, {
findServers() {
return Promise.resolve({ url: 'amqp://localhost' });
},
});
amqp.connect();
const [{ connection, url }] = await once(amqp, 'connect');
expect(url, 'url').to.equal('amqp://localhost');
expect(connection.url, 'connection.url').to.equal('amqp://localhost?heartbeat=5');
});
it('should fail to connect if findServers returns no servers', async () => {
amqp = new AmqpConnectionManager(null, {
findServers() {
return Promise.resolve(null);
},
});
amqp.connect();
const [{ err }] = await once(amqp, 'connectFailed');
expect(err.message).to.contain('No servers found');
return amqp?.close();
});
it('should timeout connect', async () => {
jest.spyOn(origAmqp, 'connect').mockImplementation((): any => {
return promiseTools.delay(200);
});
amqp = new AmqpConnectionManager('amqp://localhost');
let err;
try {
await amqp.connect({ timeout: 0.1 });
} catch (error: any) {
err = error;
}
expect(err.message).to.equal('amqp-connection-manager: connect timeout');
});
it('should work with a URL with a query', async () => {
amqp = new AmqpConnectionManager('amqp://localhost?frameMax=0x1000');
amqp.connect();
const [{ connection }] = await once(amqp, 'connect');
expect(connection.url, 'connection.url').to.equal(
'amqp://localhost?frameMax=0x1000&heartbeat=5'
);
});
it('should throw an error if no url and no `findServers` option are provided', async () => {
expect(() => new (AmqpConnectionManager as any)()).to.throw(
'Must supply either `urls` or `findServers`'
);
});
it("should reconnect to the broker if it can't connect in the first place", async () => {
amqplib.deadServers = ['amqp://rabbit1'];
// Should try to connect to rabbit1 first and be refused, and then successfully connect to rabbit2.
amqp = new AmqpConnectionManager(['amqp://rabbit1', 'amqp://rabbit2'], {
heartbeatIntervalInSeconds: 0.01,
});
amqp.connect();
let connectFailedSeen = 0;
amqp.on('connectFailed', function () {
connectFailedSeen++;
amqplib.failConnections = false;
});
const [{ connection, url }] = await once(amqp, 'connect');
expect(connectFailedSeen).to.equal(1);
// Verify that we round-robined to the next server, since the first was unavailable.
expect(url, 'url').to.equal('amqp://rabbit2');
if (typeof url !== 'string') {
throw new Error('url is not a string');
}
expect((connection as any).url, 'connection.url').to.startWith(url);
});
it('should reconnect to the broker if the broker disconnects', async () => {
amqp = new AmqpConnectionManager('amqp://localhost', {
heartbeatIntervalInSeconds: 0.01,
});
let disconnectsSeen = 0;
amqp.on('disconnect', () => disconnectsSeen++);
await amqp.connect();
amqplib.kill();
await amqp.connect();
expect(disconnectsSeen).to.equal(1);
});
it('should reconnect to the broker if the broker closed connection', async () => {
amqp = new AmqpConnectionManager('amqp://localhost', {
heartbeatIntervalInSeconds: 0.01,
});
let disconnectsSeen = 0;
amqp.on('disconnect', () => disconnectsSeen++);
await amqp.connect();
// Close the connection nicely
amqplib.simulateRemoteClose();
await once(amqp, 'connect');
expect(disconnectsSeen).to.equal(1);
});
it('should know if it is connected or not', async () => {
amqp = new AmqpConnectionManager('amqp://localhost');
amqp.connect();
expect(amqp.isConnected()).to.be.false;
await once(amqp, 'connect');
expect(amqp?.isConnected()).to.be.true;
});
it('should be able to manually reconnect', async () => {
amqp = new AmqpConnectionManager('amqp://localhost');
await amqp.connect();
amqp.reconnect();
await once(amqp, 'disconnect');
await once(amqp, 'connect');
});
it('should throw on manual reconnect after close', async () => {
amqp = new AmqpConnectionManager('amqp://localhost');
await amqp.connect();
await amqp.close();
expect(amqp.reconnect).to.throw();
});
it('should create and clean up channel wrappers', async function () {
amqp = new AmqpConnectionManager('amqp://localhost');
await amqp.connect();
const channel = amqp.createChannel({ name: 'test-chan' });
// Channel should register with connection manager
expect(amqp.channelCount, 'registered channels').to.equal(1);
expect(amqp.listeners('connect').length, 'connect listners').to.equal(1);
expect(amqp.listeners('disconnect').length, 'disconnect listners').to.equal(1);
// Closing the channel should remove all listeners and de-register the channel
await channel.close();
expect(amqp.channelCount, 'registered channels after close').to.equal(0);
expect(amqp.listeners('connect').length, 'connect listners after close').to.equal(0);
expect(amqp.listeners('disconnect').length, 'disconnect listners after close').to.equal(0);
});
it('should clean up channels on close', async function () {
amqp = new AmqpConnectionManager('amqp://localhost');
await amqp.connect();
amqp.createChannel({ name: 'test-chan' });
// Channel should register with connection manager
expect(amqp.channelCount, 'registered channels').to.equal(1);
expect(amqp.listeners('connect').length, 'connect listners').to.equal(1);
expect(amqp.listeners('disconnect').length, 'disconnect listners').to.equal(1);
// Closing the connection should remove all listeners and de-register the channel
await amqp.close();
expect(amqp.channelCount, 'registered channels after close').to.equal(0);
expect(amqp.listeners('connect').length, 'connect listners after close').to.equal(0);
expect(amqp.listeners('disconnect').length, 'disconnect listners after close').to.equal(0);
});
it('should not reconnect after close', async () => {
amqp = new AmqpConnectionManager('amqp://localhost', {
heartbeatIntervalInSeconds: 0.01,
});
let connectsSeen = 0;
amqp.on('connect', () => connectsSeen++);
await amqp.connect();
// Close the manager
await amqp?.close();
// Murder the broker on the first connect
amqplib.kill();
await promiseTools.delay(50);
expect(connectsSeen).to.equal(1);
});
it('should detect connection block/unblock', async () => {
amqp = new AmqpConnectionManager('amqp://localhost');
let blockSeen = 0;
let unblockSeen = 0;
amqp.on('blocked', () => blockSeen++);
amqp.on('unblocked', () => unblockSeen++);
await amqp.connect();
// Close the connection nicely
amqplib.simulateRemoteBlock();
amqplib.simulateRemoteUnblock();
expect(blockSeen).to.equal(1);
expect(unblockSeen).to.equal(1);
});
});