-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-real.js
More file actions
54 lines (41 loc) · 1.03 KB
/
test-real.js
File metadata and controls
54 lines (41 loc) · 1.03 KB
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
'use strict'
const test = require('tape')
const crypto = require('crypto')
const plugin = require('.')
test('real', async function (t) {
t.plan(3)
const collector = plugin()
await slowTicks(10)
const metric = (await collect(collector))[0]
t.ok(metric.stats.sum > 0, 'has sum')
t.ok(metric.stats.max > 0, 'has max')
t.ok(metric.stats.count > 0, 'has count')
})
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
}