Skip to content

Commit 3d83c56

Browse files
ivan-tymoshenkobelochub
authored andcommitted
Fix asyncMap function test
PR-URL: #394
1 parent 4fe06de commit 3d83c56

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

test/array.asyncMap.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,26 @@ const doSmth = time => {
2121
};
2222

2323
metatests.test('Non-blocking', test => {
24+
const ITEM_TIME = 1;
25+
const TIMER_TIME = 9;
2426
const ARRAY_SIZE = 1000;
25-
const MIN_IO_CALLS = 90;
26-
const MAX_IO_CALLS = 110;
27+
const EXPECTED_PERCENT = 0.5;
28+
const EXPECTED_DEVIATION = 0.2;
2729

28-
let ioCallsCount = 0;
2930
const arr = new Array(ARRAY_SIZE).fill(1);
3031

31-
const timer = setInterval(() => {
32-
doSmth(9);
33-
ioCallsCount++;
34-
}, 1);
32+
const timer = setInterval(() => doSmth(TIMER_TIME), 1);
3533

36-
metasync.asyncMap(arr, () => doSmth(1),
37-
{ percent: 0.5 }, () => {
34+
const begin = Date.now();
35+
metasync.asyncMap(arr, () => doSmth(ITEM_TIME),
36+
{ percent: EXPECTED_PERCENT }, () => {
3837
clearInterval(timer);
39-
test.assert(ioCallsCount >= MIN_IO_CALLS);
40-
test.assert(ioCallsCount <= MAX_IO_CALLS);
38+
39+
const mapTime = ITEM_TIME * ARRAY_SIZE;
40+
const allTime = Date.now() - begin;
41+
const actualPercent = mapTime / allTime;
42+
const actualDeviation = Math.abs(actualPercent - EXPECTED_PERCENT);
43+
test.assert(actualDeviation <= EXPECTED_DEVIATION);
4144
test.end();
4245
});
4346
});

0 commit comments

Comments
 (0)