-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_msg_binary_headers.js
53 lines (43 loc) · 1.18 KB
/
test_msg_binary_headers.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
import {check, sleep} from 'k6';
import {Nats} from 'k6/x/nats';
const natsConfig = {
servers: [`nats://${__ENV.NATS_HOSTNAME}:4222`],
unsafe: true,
};
const publisher = new Nats(natsConfig);
const subscriber = new Nats(natsConfig);
const responses = [];
const subscription = subscriber.subscribe('topic', (msg) => {
responses.push(msg);
});
export default function () {
const binaryData = [1, 2, 3, 4, 5, 6, 7, 8, 9];
publisher.publishMsg({
topic: 'topic',
raw: binaryData,
header: { 'x-custom-header': 'hello' },
});
sleep(1)
const message = responses.pop();
check(message, {
'Is expected message': (m) => sameBinaryArray(m.raw, [1, 2, 3, 4, 5, 6, 7, 8, 9]),
'Is expected topic': (m) => m.topic === 'topic',
'Is expected header': (m) => m.header['x-custom-header'] === 'hello',
})
}
export function teardown() {
subscription.close();
publisher.close();
subscriber.close();
}
function sameBinaryArray(a, b) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}