Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various failing tests #624

Merged
merged 11 commits into from
Jun 17, 2024
23 changes: 18 additions & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
{
"plugins": ["prettier"],
"extends": ["plugin:prettier/recommended", "eslint-config-atomic"],
"plugins": [
"prettier"
],
"extends": [
"eslint-config-atomic"
],
"rules": {
"@typescript-eslint/quotes": ["error", "double"],
"require-await": "off"
"@typescript-eslint/quotes": [
"error",
"double"
],
"require-await": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-await-in-loop": "off",
"class-methods-use-this": "off"
},
"ignorePatterns": [
"node_modules/",
Expand All @@ -15,6 +26,8 @@
"script/*.js",
"script/*.d.ts",
"docs/",
"docs-raw/"
"docs-raw/",
"test/unit/compat/",
"test/bench/"
]
}
16 changes: 12 additions & 4 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
"use strict"

module.exports = {
/**
* @type {import('mocha').MochaOptions}
*/
const config = {
require: ["ts-node/register", "rocha"],
spec: ["test/unit/*-test.ts", "test/unit/compat/*-test.{ts,js}"],
spec: [
"test/unit/*-test.ts",
"test/unit/compat/*-test.js",
],
"expose-gc": true,
"v8-expose-gc": true,
"experimental-worker": true,
recursive: true,
exit: true,
parallel: true,
}

module.exports = config
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"recommendations": [
"hbenl.vscode-mocha-test-adapter",
"hbenl.vscode-test-explorer",
"llvm-vs-code-extensions.vscode-clangd",
"xadillax.gyp",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mochaExplorer.parallel": true,
"mochaExplorer.globImplementation": "vscode"
}
12 changes: 7 additions & 5 deletions examples/majordomo/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
await this.socket.bind(this.address)

const loop = async () => {
for await (const [sender, blank, header, ...rest] of this.socket) {
for await (const [sender, _blank, header, ...rest] of this.socket) {
switch (header.toString()) {
case Header.Client:
this.handleClient(sender, ...rest)

Check warning on line 24 in examples/majordomo/broker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Unsafe argument of type `Router` assigned to a parameter of type `Buffer`

Check warning on line 24 in examples/majordomo/broker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Unsafe spread of an `any` type
break
case Header.Worker:
this.handleWorker(sender, ...rest)

Check warning on line 27 in examples/majordomo/broker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Unsafe argument of type `Router` assigned to a parameter of type `Buffer`

Check warning on line 27 in examples/majordomo/broker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Unsafe spread of an `any` type
break
default:
console.error(`invalid message header: ${header}`)
Expand All @@ -32,7 +32,7 @@
}
}

loop()
return loop()
}

async stop() {
Expand All @@ -56,8 +56,10 @@
}

case Message.Reply: {
const [client, blank, ...rep] = rest
this.dispatchReply(worker, client, ...rep)
const [client, _blank, ...rep] = rest
this.dispatchReply(worker, client, ...rep).catch(err => {
console.error(err)
})
break
}

Expand All @@ -76,21 +78,21 @@

register(worker: Buffer, service: Buffer) {
this.setWorkerService(worker, service)
this.getService(service).register(worker)

Check warning on line 81 in examples/majordomo/broker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}

dispatchRequest(client: Buffer, service: Buffer, ...req: Buffer[]) {
this.getService(service).dispatchRequest(client, ...req)

Check warning on line 85 in examples/majordomo/broker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}

dispatchReply(worker: Buffer, client: Buffer, ...rep: Buffer[]) {
const service = this.getWorkerService(worker)
this.getService(service).dispatchReply(worker, client, ...rep)
return this.getService(service).dispatchReply(worker, client, ...rep)
}

deregister(worker: Buffer) {
const service = this.getWorkerService(worker)
this.getService(service).deregister(worker)

Check warning on line 95 in examples/majordomo/broker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}

getService(name: Buffer): Service {
Expand Down
14 changes: 8 additions & 6 deletions examples/majordomo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {Broker} from "./broker"
import {Worker} from "./worker"

async function sleep(msec: number) {
return new Promise(resolve => setTimeout(resolve, msec))
return new Promise(resolve => {
setTimeout(resolve, msec)
})
}

class TeaWorker extends Worker {
Expand Down Expand Up @@ -40,7 +42,7 @@ async function request(
await socket.send(["MDPC01", service, ...req])

try {
const [blank, header, ...res] = await socket.receive()
const [_blank, _header, ...res] = await socket.receive()
console.log(`received '${res.join(", ")}' from '${service}'`)
return res
} catch (err) {
Expand All @@ -50,9 +52,9 @@ async function request(

async function main() {
for (const worker of workers) {
worker.start()
await worker.start()
}
broker.start()
await broker.start()

/* Requests are issued in parallel. */
await Promise.all([
Expand All @@ -68,9 +70,9 @@ async function main() {
])

for (const worker of workers) {
worker.stop()
await worker.stop()
}
broker.stop()
await broker.stop()
}

main().catch(err => {
Expand Down
14 changes: 9 additions & 5 deletions examples/majordomo/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class Service {

dispatchRequest(client: Buffer, ...req: Buffer[]) {
this.requests.push([client, req])
this.dispatchPending()
return this.dispatchPending()
}

async dispatchReply(worker: Buffer, client: Buffer, ...rep: Buffer[]) {
Expand All @@ -28,12 +28,15 @@ export class Service {

await this.socket.send([client, null, Header.Client, this.name, ...rep])

this.dispatchPending()
return this.dispatchPending()
}

async dispatchPending() {
while (this.workers.size && this.requests.length) {
const [key, worker] = this.workers.entries().next().value!
const [key, worker] = this.workers.entries().next().value as [
string,
Buffer,
]
this.workers.delete(key)
const [client, req] = this.requests.shift()!

Expand All @@ -42,6 +45,7 @@ export class Service {
`${client.toString("hex")} req -> ${worker.toString("hex")}`,
)

// eslint-disable-next-line no-await-in-loop
await this.socket.send([
worker,
null,
Expand All @@ -59,14 +63,14 @@ export class Service {
`registered worker ${worker.toString("hex")} for '${this.name}'`,
)
this.workers.set(worker.toString("hex"), worker)
this.dispatchPending()
return this.dispatchPending()
}

deregister(worker: Buffer) {
console.log(
`deregistered worker ${worker.toString("hex")} for '${this.name}'`,
)
this.workers.delete(worker.toString("hex"))
this.dispatchPending()
return this.dispatchPending()
}
}
12 changes: 9 additions & 3 deletions examples/majordomo/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
await this.socket.send([null, Header.Worker, Message.Ready, this.service])

const loop = async () => {
for await (const [blank1, header, type, client, blank2, ...req] of this
.socket) {
for await (const [
_blank1,
_header,
_type,
client,
_blank2,
...req
] of this.socket) {
const rep = await this.process(...req)

Check warning on line 27 in examples/majordomo/worker.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Unsafe spread of an `any` type
try {
await this.socket.send([
null,
Expand All @@ -34,7 +40,7 @@
}
}

loop()
return loop()
}

async stop() {
Expand Down
4 changes: 3 additions & 1 deletion examples/threaded-worker/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
this.input.bind("inproc://input"),
this.output.bind("inproc://output"),
this.signal.bind("inproc://signal"),
new Promise(resolve => setTimeout(resolve, 100)),
new Promise(resolve => {
setTimeout(resolve, 100)
}),
])

this.exit = Promise.all([ThreadedWorker.spawn(this.threads)])
Expand All @@ -38,7 +40,7 @@

const output: string[] = Array.from({length: input.length})
for await (const [pos, res] of this.output) {
output[parseInt(pos.toString(), 10)] = res.toString()

Check warning on line 43 in examples/threaded-worker/processor.ts

View workflow job for this annotation

GitHub Actions / Build (ubuntu-20.04, 18, x64, x64, x64, false)

Unsafe argument of type `any` assigned to a parameter of type `string`
if (output.every(el => el !== undefined)) {
break
}
Expand Down
6 changes: 4 additions & 2 deletions examples/threaded-worker/threaded-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ export class ThreadedWorker {
const listen = async () => {
for await (const [sig] of this.signal) {
if (sig.toString() === "stop") {
this.stop()
await this.stop()
}
}
}

listen()
listen().catch(err => {
throw err
})
}

async stop() {
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@types/fs-extra": "^9.0.13",
"@types/mocha": "^10.0.6",
"@types/node": "^18.19.34",
"@types/proper-lockfile": "^4.1.4",
"@types/semver": "^7.5.8",
"@types/shelljs": "^0.8.15",
"@types/which": "^2.0.2",
Expand All @@ -49,6 +50,8 @@
"npm-run-all2": "^6.2.0",
"prebuildify": "^6.0.1",
"prettier": "^2.8.8",
"proper-lockfile": "^4.1.2",
"random-words": "^1",
"rocha": "^2.5.10",
"semver": "^7.6.2",
"ts-node": "~10.9.2",
Expand Down Expand Up @@ -100,7 +103,7 @@
"format": "prettier --write .",
"test.electron.renderer": "run-s build && electron-mocha --renderer",
"lint.clang-format": "clang-format -i -style=file ./src/*.cc ./src/*.h ./src/util/*.h",
"lint-test.eslint": "eslint **/*.{ts,tsx,js,jsx,cjs,mjs,json,yaml} --no-error-on-unmatched-pattern --cache --cache-location ./.cache/eslint/",
"lint-test.eslint": "eslint ./**/*.{ts,tsx,js,jsx,cjs,mjs,json,yaml} --no-error-on-unmatched-pattern --cache --cache-location ./.cache/eslint/",
"lint.eslint": "pnpm run lint-test.eslint --fix",
"lint": "run-p lint.eslint lint.clang-format",
"lint-test": "run-s lint-test.eslint",
Expand Down
Loading
Loading