-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·111 lines (93 loc) · 3.53 KB
/
test.js
File metadata and controls
executable file
·111 lines (93 loc) · 3.53 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const test = require('tape');
const profiler = require('./index.js');
test('Should profile a callback based function', (t) => {
t.plan(3);
const tolerance = 3;
const testDuration = 50;
const testResponse = {};
const testArgument = {};
const testFn = (arg, callback) => {
t.equal(arg, testArgument, 'Arguments are forwarded')
setTimeout(() => {callback(null, testResponse)}, testDuration);
};
const testCallback = (err, result) => {
t.equal(result, testResponse, 'Original callback is executed')
}
const reporter = (duration) => {
t.assert(Math.abs(duration - testDuration) <= tolerance, 'Duration measurement is within tolerance');
}
const profiledFn = profiler(testFn, reporter);
profiledFn(testArgument, testCallback);
});
test('Should profile a promise based function', (t) => {
t.plan(3);
const tolerance = 3;
const testDuration = 50;
const testResponse = {};
const testArgument = {};
const testFn = (arg) => {
t.equal(arg, testArgument, 'Arguments are forwarded')
return new Promise(resolve => {
setTimeout(() => {resolve(testResponse)}, testDuration);
});
};
const reporter = (duration) => {
t.assert(Math.abs(duration - testDuration) <= tolerance, 'Duration measurement is within tolerance');
}
Promise.resolve(testArgument)
.then(profiler(testFn, reporter))
.then(result => t.equal(result, testResponse, 'Promise chain is continued'));
});
test('Should profile a promise based function that rejects', (t) => {
t.plan(2);
const tolerance = 3;
const testDuration = 50;
const testReason = {};
const testFn = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {reject(testReason)}, testDuration);
});
};
const reporter = (duration) => {
t.assert(Math.abs(duration - testDuration) <= tolerance, 'Duration measurement is within tolerance');
}
Promise.resolve()
.then(profiler(testFn, reporter))
.catch(() => t.pass('Promise chain is rejected'));
});
test('Should profile a normal function', (t) => {
t.plan(3);
const tolerance = 3;
const testDuration = 50;
const testResponse = {};
const testArgument = {};
const testFn = function (arg) {
t.equal(arg, testArgument, 'Arguments are forwarded')
let start = Date.now();
while(Date.now() < start + testDuration ) {};
return testResponse;
};
const reporter = (duration) => {
t.assert(Math.abs(duration - testDuration) <= tolerance, 'Duration measurement is within tolerance');
}
const profiledFn = profiler(testFn, reporter)
t.equal(profiledFn(testArgument), testResponse, 'Returns correct value');
});
test('Should profile an object method', (t) => {
t.plan(2);
const tolerance = 3;
const testDuration = 50;
const testObj = {
testResponse:{},
testFn: function () {
let start = Date.now();
while(Date.now() < start + testDuration ) {};
return this.testResponse;
}
};
const reporter = (duration) => {
t.assert(Math.abs(duration - testDuration) <= tolerance, 'Duration measurement is within tolerance');
}
testObj.profiledFn = profiler(testObj.testFn, reporter)
t.equal(testObj.profiledFn(), testObj.testResponse, 'Returns correct value');
});