-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from x0k/workers
Move tests execution to the workers
- Loading branch information
Showing
25 changed files
with
572 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { | ||
Actor, | ||
MessageType, | ||
WorkerConnection, | ||
startRemote, | ||
type Connection, | ||
type EventMessage, | ||
type IncomingMessage, | ||
type OutgoingMessage, | ||
} from "@/lib/actor"; | ||
import { createLogger } from "@/lib/logger"; | ||
import type { TestRunner, TestRunnerFactory } from "@/lib/testing"; | ||
|
||
interface Handlers<I, O> { | ||
[key: string]: any; | ||
init: (code: string) => Promise<void>; | ||
run: (data: I) => Promise<O>; | ||
} | ||
|
||
type Incoming<I, O> = IncomingMessage<Handlers<I, O>>; | ||
|
||
interface WriteEventMessage extends EventMessage<"write", string> {} | ||
interface WritelnEventMessage extends EventMessage<"writeln", string> {} | ||
|
||
type TestingActorEvent = WriteEventMessage | WritelnEventMessage; | ||
|
||
type Outgoing<I, O> = OutgoingMessage<Handlers<I, O>, string> | TestingActorEvent; | ||
|
||
class TestRunnerActor<I, O> extends Actor<Handlers<I, O>, string> { | ||
private runner: TestRunner<I, O> | null = null; | ||
|
||
constructor( | ||
connection: Connection<Incoming<I, O>, Outgoing<I, O>>, | ||
factory: TestRunnerFactory<I, O> | ||
) { | ||
const handlers: Handlers<I, O> = { | ||
init: async (code) => { | ||
this.runner = await factory({ | ||
code, | ||
out: { | ||
write(text) { | ||
connection.send({ | ||
type: MessageType.Event, | ||
event: "write", | ||
payload: text, | ||
}); | ||
}, | ||
writeln(text) { | ||
connection.send({ | ||
type: MessageType.Event, | ||
event: "writeln", | ||
payload: text, | ||
}); | ||
}, | ||
}, | ||
}); | ||
}, | ||
run: (input) => { | ||
if (!this.runner) { | ||
const err = new Error("Test runner not initialized"); | ||
connection.send({ | ||
type: MessageType.Event, | ||
event: "error", | ||
payload: err.message, | ||
}); | ||
throw err; | ||
} | ||
return this.runner.run(input); | ||
}, | ||
}; | ||
super(connection, handlers, String); | ||
} | ||
} | ||
|
||
export function startTestRunnerActor<I, O>(factory: TestRunnerFactory<I, O>) { | ||
const connection = new WorkerConnection<Incoming<I, O>, Outgoing<I, O>>( | ||
self as unknown as Worker | ||
); | ||
const actor = new TestRunnerActor(connection, factory); | ||
const stopConnection = connection.start(); | ||
const stopActor = actor.start(); | ||
return () => { | ||
stopActor(); | ||
stopConnection(); | ||
}; | ||
} | ||
|
||
interface WorkerConstructor { | ||
new (): Worker; | ||
} | ||
|
||
export function makeRemoteTestRunnerFactory<I, O>( | ||
Worker: WorkerConstructor | ||
): TestRunnerFactory<I, O> { | ||
return async ({ code, out }) => { | ||
const worker = new Worker(); | ||
const connection = new WorkerConnection<Outgoing<I, O>, Incoming<I, O>>( | ||
worker | ||
); | ||
const stopConnection = connection.start(); | ||
const log = createLogger(out); | ||
const remote = startRemote<Handlers<I, O>, string, TestingActorEvent>( | ||
log, | ||
connection, | ||
{ | ||
error: (err) => log.error(err), | ||
write: (text) => out.write(text), | ||
writeln: (text) => out.writeln(text), | ||
} | ||
); | ||
await remote.init(code); | ||
return { | ||
async run(input) { | ||
return remote.run(input); | ||
}, | ||
[Symbol.dispose]() { | ||
remote[Symbol.dispose](); | ||
stopConnection(); | ||
worker.terminate(); | ||
}, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as jsCode } from "./code.js?raw"; | ||
|
||
export { default as JsWorker } from './worker?worker' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createLogger } from "@/lib/logger"; | ||
import type { TestRunnerConfig } from "@/lib/testing"; | ||
import { JsTestRunner } from "@/lib/testing/js"; | ||
import { startTestRunnerActor } from "@/adapters/testing-actor"; | ||
|
||
import { type Input, type Output } from "../tests-data"; | ||
import type { PaymentSystemType } from "../reference"; | ||
|
||
interface TestingModule { | ||
payment(type: PaymentSystemType, base: number, amount: number): number; | ||
} | ||
|
||
class SimpleJsTestRunner extends JsTestRunner<TestingModule, Input, Output> { | ||
async executeTest(m: TestingModule, input: Input): Promise<Output> { | ||
return m.payment(input.paymentSystem, input.base, input.amount); | ||
} | ||
} | ||
|
||
startTestRunnerActor( | ||
async ({ code, out }: TestRunnerConfig) => | ||
new SimpleJsTestRunner(createLogger(out), code) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as phpCode } from './code.php?raw' | ||
|
||
export { default as PhpWorker } from './worker?worker' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as pyCode } from "./code.py?raw" | ||
|
||
export { default as PyWorker } from "./worker?worker" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { default as tsCode } from './code.ts?raw' | ||
|
||
export { default as TsWorker } from './worker?worker' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createLogger } from "@/lib/logger"; | ||
import type { TestRunnerConfig } from "@/lib/testing"; | ||
import { TsTestRunner } from "@/lib/testing/js"; | ||
import { startTestRunnerActor } from "@/adapters/testing-actor"; | ||
|
||
import { type Input, type Output } from "../tests-data"; | ||
import type { PaymentSystemType } from "../reference"; | ||
|
||
interface TestingModule { | ||
payment(type: PaymentSystemType, base: number, amount: number): number; | ||
} | ||
|
||
class SimpleTsTestRunner extends TsTestRunner<TestingModule, Input, Output> { | ||
async executeTest(m: TestingModule, input: Input): Promise<Output> { | ||
return m.payment(input.paymentSystem, input.base, input.amount); | ||
} | ||
} | ||
|
||
startTestRunnerActor( | ||
async ({ code, out }: TestRunnerConfig) => | ||
new SimpleTsTestRunner(createLogger(out), code) | ||
); |
Oops, something went wrong.