-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmqtt-source.spec.ts
56 lines (48 loc) · 1.33 KB
/
mqtt-source.spec.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
import { Job, MqttProcessor } from '../src';
import { CustomDestination } from './helpers';
let interval: any;
let server: any;
let aedesInstance: any;
beforeAll(async () => {
await startServer();
interval = setInterval(function () {
aedesInstance.publish({
topic: 'presence',
payload: 'test',
qos: 0,
retain: false,
}, () => { });
}, 1000);
});
beforeEach(async () => {
});
afterAll(async () => {
clearInterval(interval);
await closeServer();
})
async function closeServer() {
return new Promise<any>(async (resolve, reject) => {
server.close(() => { aedesInstance.close(resolve) });
});
}
async function startServer() {
return new Promise<any>(async (resolve, reject) => {
aedesInstance = require('aedes')();
server = require('net').createServer(aedesInstance.handle);
server.listen(1883, resolve);
});
}
it('gets messages from broker', async () => {
const destination = new CustomDestination();
const processor = new MqttProcessor({
topics: ['presence'],
url: 'mqtt://localhost:1883',
rowLimit: 1,
});
const job = new Job(processor, [destination]);
await job.run();
expect(destination.getData()).toEqual([{
message: 'test',
topic: 'presence',
}]);
});