Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlup committed Nov 25, 2021
1 parent b75cf96 commit be09f96
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions tests/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const test = require('ava');
const sinon = require('sinon');
const { performance } = require('perf_hooks');
const { hrtime2ms } = require('@dnlup/hrtime-utils');
const { StatsdMock } = require('./helpers/statsd');
const { gte16 } = require('../lib/utils');
Expand Down Expand Up @@ -598,10 +599,13 @@ test.serial('timerify custom onSend on Node < 16', async (t) => {
t.log('skipping test');
return t.pass();
}
const clock = sinon.useFakeTimers();
t.teardown(() => clock.restore());
const onSend = sinon.spy();
const asyncFunc = async () => {
await sleep(100);
};
const syncFunc = () => {};

const server = await setup({
host: `udp://127.0.0.1:${t.context.address.port}`,
Expand All @@ -613,9 +617,25 @@ test.serial('timerify custom onSend on Node < 16', async (t) => {
health: false,
},
});
const timerified = server.timerify('asyncFunc', asyncFunc, onSend);
await timerified();
const asyncTimerified = server.timerify('asyncFunc', asyncFunc, onSend);
const syncTimerified = server.timerify('syncFunc', syncFunc, onSend);

let start = performance.now();
const ret = asyncTimerified();
clock.tick(100);
await ret;
let end = performance.now();

t.is(100, end - start);
t.true(onSend.calledOnce);
t.is('asyncFunc', onSend.firstCall.firstArg);
t.is('number', typeof onSend.firstCall.lastArg);

start = performance.now();
syncTimerified();
end = performance.now();
t.is(0, end - start);
t.true(onSend.calledTwice);
t.is('syncFunc', onSend.lastCall.firstArg);
t.is('number', typeof onSend.lastCall.lastArg);
});

0 comments on commit be09f96

Please sign in to comment.