/
05-topics.test.ts
96 lines (85 loc) · 2.59 KB
/
05-topics.test.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
// tslint:disable:no-console
import minimatch from 'minimatch';
import { createConsumer, createProducer } from '../src';
import getMockConnector from './helpers/getMockConnector';
it('should handle topics', done => {
const patterns = ['*.exe', '*.jpg', 'cat.*'];
const { createAmqpConnector, channel: engine } = getMockConnector({
onPublish: ({ exchange, routingKey, content, onMessage }) => {
// Simulate rabbit behaviour match routing patterns
const matches = patterns.reduce(
(bl, pat) => bl || minimatch(routingKey, pat),
false
);
if (matches) {
onMessage({
content,
fields: { exchange, routingKey },
properties: {}
});
}
}
});
const { sender, receiver } = createAmqpConnector({
declarations: {
exchanges: [
{
durable: false,
name: 'topic_logs',
type: 'topic'
}
]
},
uri: 'amqp://somerabbitserver'
});
const producer = createProducer(sender());
const consumer = createConsumer(
receiver({
bindings: patterns.map(pattern => ({
pattern,
source: 'topic_logs'
})),
noAck: true
})
);
const output: any[] = [];
consumer.subscribe(msg => {
output.push(`${msg.to.key}: '${msg.body}'`);
if (output.length >= 2) {
expect(engine.jestSpyCalls.mock.calls).toEqual([
['assertExchange', 'topic_logs', 'topic', { durable: false }],
['assertExchange', 'topic_logs', 'topic', { durable: false }],
['assertQueue', '', { exclusive: true }],
['bindQueue', 'server-queue', 'topic_logs', '*.exe', undefined],
['bindQueue', 'server-queue', 'topic_logs', '*.jpg', undefined],
['bindQueue', 'server-queue', 'topic_logs', 'cat.*', undefined],
['consume', 'server-queue', '_FUNCTION_', { noAck: true }],
['publish', 'topic_logs', 'cat.jpg', Buffer.from('"I am a JPG image"')],
[
'publish',
'topic_logs',
'fish.png',
Buffer.from('"I am a Fish image"')
],
['publish', 'topic_logs', 'dog.exe', Buffer.from('"I am a Dog exe"')]
]);
expect(output).toEqual([
"cat.jpg: 'I am a JPG image'",
"dog.exe: 'I am a Dog exe'"
]);
done();
}
});
producer.next({
body: 'I am a JPG image',
to: { exchange: 'topic_logs', key: 'cat.jpg' }
});
producer.next({
body: 'I am a Fish image',
to: { exchange: 'topic_logs', key: 'fish.png' }
});
producer.next({
body: 'I am a Dog exe',
to: { exchange: 'topic_logs', key: 'dog.exe' }
});
});