-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecorder.spec.ts
90 lines (74 loc) · 3.49 KB
/
recorder.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
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
import { Metric, MetricType, Metrics } from '../src/metrics';
import { Recorder, WithPeriodicFlushRecorder } from '../src/recorder';
import { Sink } from '../src/sink';
class testSink implements Sink {
constructor(
public metrics: Metrics = [],
public calledTimes: number = 0,
) {}
async send(metrics: Metrics): Promise<void> {
this.calledTimes++;
this.metrics.push(...metrics);
}
}
describe('Recorder', function() {
it('record() + flush()', async function() {
const sink = new testSink();
const recorderMetricsSpy = [];
const recorder = new Recorder('test', sink, recorderMetricsSpy);
const metric = new Metric('test_metric', MetricType.Counter, 1, { foo: 'bar' });
await recorder.record(metric);
expect(recorderMetricsSpy).toHaveLength(1);
expect(recorderMetricsSpy[0]).toEqual(metric);
expect(sink.metrics).toHaveLength(0);
await recorder.flush();
expect(recorderMetricsSpy).toHaveLength(0);
expect(sink.metrics).toHaveLength(1);
expect(sink.metrics[0]).toEqual(metric);
});
it('count()', async function() {
const recorderMetricsSpy = [];
const recorder = new Recorder('test', new testSink(), recorderMetricsSpy);
const expectedMetric = new Metric('test.test_metric', MetricType.Counter, 1, { foo: 'bar' });
await recorder.count('test_metric', 1, { foo: 'bar' });
expect(recorderMetricsSpy).toHaveLength(1);
expect(recorderMetricsSpy[0]).toEqual(expectedMetric);
});
it('gauge()', async function() {
const recorderMetricsSpy = [];
const recorder = new Recorder('test', new testSink(), recorderMetricsSpy);
const expectedMetric = new Metric('test.test_metric', MetricType.Gauge, 937.5551819, { foo: 'bar' });
await recorder.gauge('test_metric', 937.5551819, { foo: 'bar' });
expect(recorderMetricsSpy).toHaveLength(1);
expect(recorderMetricsSpy[0]).toEqual(expectedMetric);
});
it('recordActionFinished()', async function() {
const recorderMetricsSpy = [];
const recorder = new Recorder('test', new testSink(), recorderMetricsSpy);
const expectedMetric = new Metric('test.action.finished', MetricType.Counter, 1, { action: 'validate', success: true });
await recorder.recordActionFinished('validate', { success: true });
expect(recorderMetricsSpy).toHaveLength(1);
expect(recorderMetricsSpy[0]).toEqual(expectedMetric);
});
it('recordActionInvoked()', async function() {
const recorderMetricsSpy = [];
const recorder = new Recorder('test', new testSink(), recorderMetricsSpy);
const expectedMetric = new Metric('test.action.invoked', MetricType.Counter, 1, { action: 'convert' });
await recorder.recordActionInvoked('convert');
expect(recorderMetricsSpy).toHaveLength(1);
expect(recorderMetricsSpy[0]).toEqual(expectedMetric);
});
it('WithPeriodicFlushRecorder()', async function() {
jest.useFakeTimers(); // this mocks setTimeout global function
const sink = new testSink();
const recorderMetricsSpy = [];
const {recorder, stop} = WithPeriodicFlushRecorder(new Recorder('test', sink, recorderMetricsSpy), 100);
const expectedMetric = new Metric('test.action.finished', MetricType.Counter, 1, { action: 'validate', success: true });
await recorder.recordActionFinished('validate', { success: true });
expect(recorderMetricsSpy).toHaveLength(1);
expect(recorderMetricsSpy[0]).toEqual(expectedMetric);
jest.advanceTimersByTime(200);
stop();
expect(sink.calledTimes).toEqual(3);
});
});