|
| 1 | +import { Identifiers } from "@mainsail/constants"; |
| 2 | +import { Application, Ipc } from "@mainsail/kernel"; |
| 3 | +import { EventEmitter } from "events"; |
| 4 | +import esmock from "esmock"; |
| 5 | +import { PassThrough } from "stream"; |
| 6 | + |
| 7 | +import { describe } from "@mainsail/test-runner"; |
| 8 | + |
| 9 | +// Records every `new Worker(...)` so the factory test can assert how the thread is spawned. |
| 10 | +const constructions: any[][] = []; |
| 11 | + |
| 12 | +// Stand-in for worker_threads.Worker: an EventEmitter exposing the stdout/stderr streams and |
| 13 | +// threadId that Ipc.Subprocess reads, so the real Subprocess wraps it without a real thread. |
| 14 | +class FakeWorker extends EventEmitter { |
| 15 | + public threadId = 1; |
| 16 | + public readonly stdout = new PassThrough(); |
| 17 | + public readonly stderr = new PassThrough(); |
| 18 | + |
| 19 | + public constructor(...arguments_: any[]) { |
| 20 | + super(); |
| 21 | + constructions.push(arguments_); |
| 22 | + } |
| 23 | + |
| 24 | + public postMessage(): void {} |
| 25 | + public async terminate(): Promise<number> { |
| 26 | + return 0; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Load the provider with worker_threads.Worker swapped for the fake; the real Ipc.Subprocess |
| 31 | +// and ./worker.js stay in place. |
| 32 | +const { ServiceProvider } = await esmock("./service-provider", { |
| 33 | + worker_threads: { Worker: FakeWorker }, |
| 34 | +}); |
| 35 | + |
| 36 | +describe<{ |
| 37 | + app: Application; |
| 38 | + serviceProvider: any; |
| 39 | + worker: any; |
| 40 | +}>("ServiceProvider", ({ assert, beforeEach, it, spy, stub }) => { |
| 41 | + beforeEach((context) => { |
| 42 | + constructions.length = 0; |
| 43 | + context.worker = { boot: async () => {}, dispose: async () => {} }; |
| 44 | + |
| 45 | + context.app = new Application(); |
| 46 | + context.app.bind(Identifiers.Config.Flags).toConstantValue({ network: "testnet" }); |
| 47 | + // Ipc.Subprocess resolves the logger from the container when the factory runs. |
| 48 | + context.app.bind(Identifiers.Services.Log.Service).toConstantValue({ debug: () => {}, error: () => {} }); |
| 49 | + |
| 50 | + context.serviceProvider = context.app.resolve(ServiceProvider); |
| 51 | + |
| 52 | + // register() resolves the WorkerInstance, whose @postConstruct would invoke the factory |
| 53 | + // and spawn. Intercept that resolution so only the explicit factory call below runs it. |
| 54 | + stub(context.app, "resolve").returnValue(context.worker); |
| 55 | + }); |
| 56 | + |
| 57 | + it("register binds the worker subprocess factory and the worker", async (context) => { |
| 58 | + assert.false(context.app.isBound(Identifiers.Evm.WorkerSubprocess.Factory)); |
| 59 | + assert.false(context.app.isBound(Identifiers.Evm.Worker)); |
| 60 | + |
| 61 | + await context.serviceProvider.register(); |
| 62 | + |
| 63 | + assert.true(context.app.isBound(Identifiers.Evm.WorkerSubprocess.Factory)); |
| 64 | + assert.true(context.app.isBound(Identifiers.Evm.Worker)); |
| 65 | + assert.function(context.app.get(Identifiers.Evm.WorkerSubprocess.Factory)); |
| 66 | + assert.equal(context.app.get(Identifiers.Evm.Worker), context.worker); |
| 67 | + }); |
| 68 | + |
| 69 | + it("the subprocess factory spawns the worker script with piped stdio and wraps it in an Ipc.Subprocess", async (context) => { |
| 70 | + await context.serviceProvider.register(); |
| 71 | + |
| 72 | + const factory = context.app.get(Identifiers.Evm.WorkerSubprocess.Factory) as () => Ipc.Subprocess; |
| 73 | + const subprocess = factory(); |
| 74 | + |
| 75 | + assert.length(constructions, 1); |
| 76 | + const [scriptPath, options] = constructions[0]; |
| 77 | + assert.true(scriptPath.endsWith("worker-script.js")); |
| 78 | + assert.equal(options, { stderr: true, stdout: true }); |
| 79 | + assert.instance(subprocess, Ipc.Subprocess); |
| 80 | + }); |
| 81 | + |
| 82 | + it("boot delegates to the worker with the flags and the thread name", async (context) => { |
| 83 | + await context.serviceProvider.register(); |
| 84 | + const boot = spy(context.worker, "boot"); |
| 85 | + |
| 86 | + await context.serviceProvider.boot(); |
| 87 | + |
| 88 | + boot.calledOnce(); |
| 89 | + boot.calledWith({ network: "testnet", thread: "evm-api" }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("dispose delegates to the worker", async (context) => { |
| 93 | + await context.serviceProvider.register(); |
| 94 | + const dispose = spy(context.worker, "dispose"); |
| 95 | + |
| 96 | + await context.serviceProvider.dispose(); |
| 97 | + |
| 98 | + dispose.calledOnce(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("is required", async (context) => { |
| 102 | + assert.true(await context.serviceProvider.required()); |
| 103 | + }); |
| 104 | +}); |
0 commit comments