Skip to content

Commit

Permalink
fix: remove unnecessary Math.min on reloadTime
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall committed Aug 16, 2020
1 parent 12dd4d4 commit 51e73ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type State = {
function reload(requestsPerInterval: number, interval: number, state: State): State {
const throughput = requestsPerInterval / interval;
const now = Date.now();
const reloadTime = Math.min(now - state.lastReload, interval);
const reloadTime = now - state.lastReload;
const reloadedRequests = reloadTime * throughput;
const newAvalableRequests = state.availableRequests + reloadedRequests;

Expand Down
9 changes: 6 additions & 3 deletions test/wyt.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ test('throw if taking more turns per take than turnsperInterval', async (t) => {
await t.throwsAsync(() => waitTurn(3));
});

test('2 per second in parallel, twice', async (t) => {
const timer = createTimer();
test('2 per second in parallel, after different timeoutss', async (t) => {
const rpi = 2;
const interval = 1000;
const waitTurn = wyt(rpi, interval);

async function batch() {
const timer = createTimer();
const promises = [
waitTurn(),
waitTurn(),
Expand All @@ -177,6 +177,9 @@ test('2 per second in parallel, twice', async (t) => {
t.true(roughly(t5, 3 * (interval / rpi)));
}
await batch();
await new Promise((resolve) => setTimeout(resolve, 100));
// let enough time for wyt to think the previous batch has enough processed
await new Promise((resolve) => setTimeout(resolve, 1000));
await batch();
// ensure that wyt won't next try to overprocess after a long wait
await new Promise((resolve) => setTimeout(resolve, 3500));
});

0 comments on commit 51e73ce

Please sign in to comment.