-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
76 lines (58 loc) · 1.64 KB
/
test.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
'use strict'
const test = require('tape')
const crypto = require('crypto')
const plugin = require('.')
test('basic', async function (t) {
t.plan(9)
const collector = plugin()
await start(collector)
await slowTicks(10)
const metrics1 = await collect(collector)
t.is(metrics1.length, 1, 'emits one metric')
t.ok(metrics1[0].stats.sum > 0, 'has sum')
t.ok(metrics1[0].stats.max > 0, 'has max')
t.ok(metrics1[0].stats.count > 0, 'has count')
await slowTicks(10)
const metrics2 = await collect(collector)
t.is(metrics2.length, 1, 'emits one metric on second ping')
t.ok(metrics2[0] !== metrics1[0], 'metric is a new object')
t.ok(metrics2[0].stats.sum > 0, 'has sum on second ping')
t.ok(metrics2[0].stats.max > 0, 'has max on second ping')
t.ok(metrics2[0].stats.count > 0, 'has count on second ping')
})
function start (collector) {
return new Promise((resolve, reject) => {
collector.start((err) => {
if (err) reject(err)
else resolve()
})
})
}
function collect (collector) {
return new Promise((resolve, reject) => {
const metrics = []
const push = metrics.push.bind(metrics)
collector.on('metric', push)
collector.ping((err) => {
collector.removeListener('metric', push)
if (err) reject(err)
else resolve(metrics.map(simplify))
})
})
}
function slowTicks (ticks) {
let n = 0
return new Promise((resolve) => {
function next () {
if (n++ >= ticks) return resolve()
crypto.randomFillSync(Buffer.alloc(1e6))
setImmediate(next)
}
next()
})
}
function simplify (metric) {
delete metric.date
delete metric.statistic
return metric
}