Skip to content

Commit

Permalink
Update package.json. Update ESLint configuration. Switch from timekee…
Browse files Browse the repository at this point in the history
…per to sinon. Delint.
  • Loading branch information
mceachen committed Oct 20, 2024
1 parent 9ce7856 commit ed1064a
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 46 deletions.
4 changes: 0 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ module.exports = {
rules: {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/member-delimiter-style": [
"warn",
{ multiline: { delimiter: "none" } },
],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/await-thenable": ["error"],
Expand Down
3 changes: 3 additions & 0 deletions .ncurc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"reject": [
"@types/chai",
"@types/chai-as-promised",
"chai-as-promised",
"chai-as-promised why: v8 went to ESM",
"chai",
"eslint",
"rimraf",
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"rngseed"
],
"cSpell.words": [
"sinonjs",
"zombification"
]
}
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@
"author": "Matthew McEachen <[email protected]>",
"license": "MIT",
"devDependencies": {
"@sinonjs/fake-timers": "^13.0.3",
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^8.0.1",
"@types/chai-as-promised": "^7",
"@types/chai-string": "^1.4.5",
"@types/chai-subset": "^1.3.5",
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.7",
"@types/sinonjs__fake-timers": "^8.1.5",
"@typescript-eslint/eslint-plugin": "^8.10.0",
"@typescript-eslint/parser": "^8.10.0",
"chai": "^4.3.10",
"chai-as-promised": "^8.0.0",
"chai-as-promised": "^7.1.2",
"chai-string": "^1.5.0",
"chai-subset": "^1.6.0",
"chai-withintoleranceof": "^1.0.1",
Expand All @@ -73,7 +75,6 @@
"serve": "^14.2.4",
"source-map-support": "^0.5.21",
"split2": "^4.2.0",
"timekeeper": "^2.3.1",
"typedoc": "^0.26.10",
"typescript": "~5.6.3"
}
Expand Down
37 changes: 22 additions & 15 deletions src/BatchCluster.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import FakeTimers from "@sinonjs/fake-timers"
import process from "node:process"
import { filterInPlace } from "./Array"
import { delay, until } from "./Async"
import { BatchCluster } from "./BatchCluster"
import { secondMs } from "./BatchClusterOptions"
import { DefaultTestOptions } from "./DefaultTestOptions.spec"
import { map, omit, orElse } from "./Object"
import { isWin } from "./Platform"
import { toS } from "./String"
import { Task } from "./Task"
import { thenOrTimeout } from "./Timeout"
import {
currentTestPids,
expect,
Expand All @@ -24,9 +15,18 @@ import {
times,
unhandledRejections,
} from "./_chai.spec"
import { filterInPlace } from "./Array"
import { delay, until } from "./Async"
import { BatchCluster } from "./BatchCluster"
import { secondMs } from "./BatchClusterOptions"
import { DefaultTestOptions } from "./DefaultTestOptions.spec"
import { map, omit, orElse } from "./Object"
import { isWin } from "./Platform"
import { toS } from "./String"
import { Task } from "./Task"
import { thenOrTimeout } from "./Timeout"

const isCI = process.env.CI === "1"
const tk = require("timekeeper")

function arrayEqualish<T>(a: T[], b: T[], maxAcceptableDiffs: number) {
const common = a.filter((ea) => b.includes(ea))
Expand Down Expand Up @@ -901,11 +901,20 @@ describe("BatchCluster", function () {

describe("maxProcAgeMillis (recycling procs)", () => {
let bc: BatchCluster
let clock: FakeTimers.InstalledClock

beforeEach(() => {
clock = FakeTimers.install({
shouldClearNativeTimers: true,
shouldAdvanceTime: true,
})
})

afterEach(() => {
tk.reset()
clock.uninstall()
return shutdown(bc)
})

for (const { maxProcAgeMillis, ctx, exp } of [
{
maxProcAgeMillis: 0,
Expand All @@ -929,8 +938,6 @@ describe("BatchCluster", function () {
it("(" + maxProcAgeMillis + "): " + ctx, async function () {
// TODO: look into why this fails in CI on windows
if (isWin && isCI) return this.skip()
const start = Date.now()
tk.freeze(start)
setFailratePct(0)

bc = listen(
Expand All @@ -944,7 +951,7 @@ describe("BatchCluster", function () {
)
assertExpectedResults(await Promise.all(runTasks(bc, 2)))
const pidsBefore = bc.pids()
tk.freeze(start + 7000)
clock.tick(7000)
assertExpectedResults(await Promise.all(runTasks(bc, 2)))
const pidsAfter = bc.pids()
console.dir({ maxProcAgeMillis, pidsBefore, pidsAfter })
Expand Down
2 changes: 1 addition & 1 deletion src/BatchClusterEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface TypedEventEmitter<T> {
): this
emit<E extends keyof T>(eventName: E, ...args: Args<T[E]>): boolean

// eslint-disable-next-line @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
listeners<E extends keyof T>(event: E): Function[]

removeAllListeners(eventName?: keyof T): this
Expand Down
2 changes: 1 addition & 1 deletion src/BatchProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ export class BatchProcess {
})
return true
}
} catch (err) {
} catch {
// child process went away. We should too.
this.end(false, "stdin.error")
return false
Expand Down
2 changes: 1 addition & 1 deletion src/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function tryEach(arr: (() => void)[]): void {
for (const f of arr) {
try {
f()
} catch (_) {
} catch {
//
}
}
Expand Down
43 changes: 23 additions & 20 deletions src/Rate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import FakeTimers from "@sinonjs/fake-timers"
import { minuteMs } from "./BatchClusterOptions"
import { Rate } from "./Rate"
import { expect, times } from "./_chai.spec"

const tk = require("timekeeper")

describe("Rate", () => {
const now = Date.now()
const r = new Rate()
let clock: FakeTimers.InstalledClock

beforeEach(() => {
tk.freeze(now)
// clear() must be called _after_ freezing time
clock = FakeTimers.install({ now: now })
// clear() must be called _after_ setting up fake timers
r.clear()
})

after(() => tk.reset())
afterEach(() => {
clock.uninstall()
})

function expectRate(rate: Rate, epm: number, tol = 0.1) {
expect(rate.eventsPerMs).to.be.withinToleranceOf(epm, tol)
Expand All @@ -27,7 +29,7 @@ describe("Rate", () => {
})

it("maintains a rate of 0 after time with no events", () => {
tk.freeze(now + minuteMs)
clock.tick(minuteMs)
expectRate(r, 0)
})

Expand All @@ -37,38 +39,39 @@ describe("Rate", () => {
() => {
times(cnt, () => r.onEvent())
expectRate(r, 0)
tk.freeze(now + 100)
clock.tick(100)
expectRate(r, 0)
tk.freeze(now + r.warmupMs + 1)
clock.tick(r.warmupMs - 100 + 1)
expectRate(r, cnt / r.warmupMs)
tk.freeze(now + 2 * r.warmupMs)
clock.tick(r.warmupMs)
expectRate(r, cnt / (2 * r.warmupMs))
tk.freeze(now + 3 * r.warmupMs)
clock.tick(r.warmupMs)
expectRate(r, cnt / (3 * r.warmupMs))
tk.freeze(now + r.periodMs)
clock.tick(r.periodMs - 3 * r.warmupMs)
expectRate(r, 0)
expect(r.msSinceLastEvent).to.eql(minuteMs)
expect(r.msSinceLastEvent).to.be.closeTo(r.periodMs, 5)
},
)
}

for (const events of [5, 10, 100, 1000]) {
for (const events of [4, 32, 256, 1024]) {
it(
"calculates average rate for " + events + " events, and then decays",
() => {
const period = r.periodMs
times(events, (i) => {
tk.freeze(now + (period * i) / events)
times(events, () => {
clock.tick(r.periodMs / events)
r.onEvent()
})
const tickMs = r.periodMs / 4
expectRate(r, events / period, 0.3)
tk.freeze(now + 1.25 * r.periodMs)
clock.tick(tickMs)
expectRate(r, 0.75 * (events / period), 0.3)
tk.freeze(now + 1.5 * r.periodMs)
clock.tick(tickMs)
expectRate(r, 0.5 * (events / period), 0.3)
tk.freeze(now + 1.75 * r.periodMs)
expectRate(r, 0.25 * (events / period), 0.3)
tk.freeze(now + 2 * r.periodMs)
clock.tick(tickMs)
expectRate(r, 0.25 * (events / period), 0.5)
clock.tick(tickMs)
expectRate(r, 0)
},
)
Expand Down
1 change: 1 addition & 0 deletions src/_chai.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-require-imports */
try {
require("source-map-support").install()
} catch {
Expand Down
4 changes: 3 additions & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ function toF(s: string | undefined) {
const failrate = toF(process.env.failrate) ?? 0
const rng =
process.env.rngseed != null
? require("seedrandom")(process.env.rngseed)
? // eslint-disable-next-line @typescript-eslint/no-require-imports
require("seedrandom")(process.env.rngseed)
: Math.random

async function onLine(line: string): Promise<void> {
Expand Down Expand Up @@ -148,5 +149,6 @@ async function onLine(line: string): Promise<void> {
const m = new Mutex()

process.stdin
// eslint-disable-next-line @typescript-eslint/no-require-imports
.pipe(require("split2")())
.on("data", (ea: string) => m.serial(() => onLine(ea)))

0 comments on commit ed1064a

Please sign in to comment.