|
| 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 | +import { Worker as WorkerInstance } from "./worker"; |
| 9 | + |
| 10 | +// Records every `new Worker(...)` so the factory test can assert how the thread is spawned. |
| 11 | +const constructions: any[][] = []; |
| 12 | + |
| 13 | +// Stand-in for worker_threads.Worker: an EventEmitter exposing the stdout/stderr streams and |
| 14 | +// threadId that Ipc.Subprocess reads, so the real Subprocess wraps it without a real thread. |
| 15 | +class FakeWorker extends EventEmitter { |
| 16 | + public threadId = 1; |
| 17 | + public readonly stdout = new PassThrough(); |
| 18 | + public readonly stderr = new PassThrough(); |
| 19 | + |
| 20 | + public constructor(...arguments_: any[]) { |
| 21 | + super(); |
| 22 | + constructions.push(arguments_); |
| 23 | + } |
| 24 | + |
| 25 | + public postMessage(): void {} |
| 26 | + public async terminate(): Promise<number> { |
| 27 | + return 0; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// Load the provider with worker_threads.Worker swapped for the fake; the real Ipc.Subprocess |
| 32 | +// and ./worker.js stay in place. |
| 33 | +const { ServiceProvider } = await esmock("./service-provider", { |
| 34 | + worker_threads: { Worker: FakeWorker }, |
| 35 | +}); |
| 36 | + |
| 37 | +describe<{ |
| 38 | + app: Application; |
| 39 | + serviceProvider: any; |
| 40 | +}>("ServiceProvider", ({ assert, beforeEach, it, spy }) => { |
| 41 | + beforeEach((context) => { |
| 42 | + constructions.length = 0; |
| 43 | + |
| 44 | + context.app = new Application(); |
| 45 | + context.app.bind(Identifiers.Config.Flags).toConstantValue({ network: "testnet" }); |
| 46 | + // Ipc.Subprocess resolves the logger from the container when the factory runs. |
| 47 | + context.app.bind(Identifiers.Services.Log.Service).toConstantValue({ debug: () => {}, error: () => {} }); |
| 48 | + |
| 49 | + context.serviceProvider = context.app.resolve(ServiceProvider); |
| 50 | + }); |
| 51 | + |
| 52 | + it("register binds the worker instance, worker factory, worker pool and subprocess factory", async (context) => { |
| 53 | + await context.serviceProvider.register(); |
| 54 | + |
| 55 | + assert.true(context.app.isBound(Identifiers.CryptoWorker.Worker.Instance)); |
| 56 | + assert.true(context.app.isBound(Identifiers.CryptoWorker.Worker.Factory)); |
| 57 | + assert.true(context.app.isBound(Identifiers.CryptoWorker.WorkerPool)); |
| 58 | + assert.true(context.app.isBound(Identifiers.CryptoWorker.WorkerSubprocess.Factory)); |
| 59 | + assert.function(context.app.get(Identifiers.CryptoWorker.WorkerSubprocess.Factory)); |
| 60 | + }); |
| 61 | + |
| 62 | + it("the subprocess factory spawns the worker script with piped stdio and wraps it in an Ipc.Subprocess", async (context) => { |
| 63 | + await context.serviceProvider.register(); |
| 64 | + |
| 65 | + const factory = context.app.get(Identifiers.CryptoWorker.WorkerSubprocess.Factory) as () => Ipc.Subprocess; |
| 66 | + const subprocess = factory(); |
| 67 | + |
| 68 | + assert.length(constructions, 1); |
| 69 | + const [scriptPath, options] = constructions[0]; |
| 70 | + assert.true(scriptPath.endsWith("worker-script.js")); |
| 71 | + assert.equal(options, { stderr: true, stdout: true }); |
| 72 | + assert.instance(subprocess, Ipc.Subprocess); |
| 73 | + }); |
| 74 | + |
| 75 | + it("the worker factory resolves a Worker instance", async (context) => { |
| 76 | + await context.serviceProvider.register(); |
| 77 | + |
| 78 | + const factory = context.app.get<() => WorkerInstance>(Identifiers.CryptoWorker.Worker.Factory); |
| 79 | + |
| 80 | + assert.instance(factory(), WorkerInstance); |
| 81 | + }); |
| 82 | + |
| 83 | + it("boot boots the worker pool", async (context) => { |
| 84 | + await context.serviceProvider.register(); |
| 85 | + |
| 86 | + const pool = { boot: async () => {}, dispose: async () => {} }; |
| 87 | + context.app.rebind(Identifiers.CryptoWorker.WorkerPool).toConstantValue(pool); |
| 88 | + const boot = spy(pool, "boot"); |
| 89 | + |
| 90 | + await context.serviceProvider.boot(); |
| 91 | + |
| 92 | + boot.calledOnce(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("dispose disposes the worker pool", async (context) => { |
| 96 | + await context.serviceProvider.register(); |
| 97 | + |
| 98 | + const pool = { boot: async () => {}, dispose: async () => {} }; |
| 99 | + context.app.rebind(Identifiers.CryptoWorker.WorkerPool).toConstantValue(pool); |
| 100 | + const dispose = spy(pool, "dispose"); |
| 101 | + |
| 102 | + await context.serviceProvider.dispose(); |
| 103 | + |
| 104 | + dispose.calledOnce(); |
| 105 | + }); |
| 106 | + |
| 107 | + it("is required", async (context) => { |
| 108 | + assert.true(await context.serviceProvider.required()); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +const importFresh = (moduleName: string) => import(`${moduleName}?${Date.now()}`); |
| 113 | + |
| 114 | +describe<{ |
| 115 | + app: Application; |
| 116 | + serviceProvider: any; |
| 117 | +}>("ServiceProvider.configSchema", ({ assert, beforeEach, it }) => { |
| 118 | + const importDefaults = async () => (await importFresh("../distribution/defaults.js")).defaults; |
| 119 | + |
| 120 | + beforeEach((context) => { |
| 121 | + context.app = new Application(); |
| 122 | + context.serviceProvider = context.app.resolve(ServiceProvider); |
| 123 | + |
| 124 | + for (const key of Object.keys(process.env)) { |
| 125 | + if (key.includes("MAINSAIL_CRYPTO_WORKER")) { |
| 126 | + delete process.env[key]; |
| 127 | + } |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + it("should validate schema using defaults", async ({ serviceProvider }) => { |
| 132 | + const result = serviceProvider.configSchema().validate(await importDefaults()); |
| 133 | + |
| 134 | + assert.undefined(result.error); |
| 135 | + assert.number(result.value.workerCount); |
| 136 | + assert.boolean(result.value.workerLoggingEnabled); |
| 137 | + }); |
| 138 | + |
| 139 | + it("should allow configuration extension", async ({ serviceProvider }) => { |
| 140 | + const defaults = await importDefaults(); |
| 141 | + defaults.customField = "dummy"; |
| 142 | + |
| 143 | + const result = serviceProvider.configSchema().validate(defaults); |
| 144 | + |
| 145 | + assert.undefined(result.error); |
| 146 | + assert.equal(result.value.customField, "dummy"); |
| 147 | + }); |
| 148 | + |
| 149 | + it("should parse process.env.MAINSAIL_CRYPTO_WORKER_COUNT", async ({ serviceProvider }) => { |
| 150 | + process.env.MAINSAIL_CRYPTO_WORKER_COUNT = "1"; |
| 151 | + |
| 152 | + const result = serviceProvider.configSchema().validate(await importDefaults()); |
| 153 | + |
| 154 | + assert.undefined(result.error); |
| 155 | + assert.equal(result.value.workerCount, 1); |
| 156 | + }); |
| 157 | + |
| 158 | + it("should throw if process.env.MAINSAIL_CRYPTO_WORKER_COUNT is below the minimum", async ({ serviceProvider }) => { |
| 159 | + process.env.MAINSAIL_CRYPTO_WORKER_COUNT = "0"; |
| 160 | + |
| 161 | + const result = serviceProvider.configSchema().validate(await importDefaults()); |
| 162 | + |
| 163 | + assert.defined(result.error); |
| 164 | + }); |
| 165 | + |
| 166 | + it("should throw if process.env.MAINSAIL_CRYPTO_WORKER_COUNT is not a number", async ({ serviceProvider }) => { |
| 167 | + process.env.MAINSAIL_CRYPTO_WORKER_COUNT = "not-a-number"; |
| 168 | + |
| 169 | + const result = serviceProvider.configSchema().validate(await importDefaults()); |
| 170 | + |
| 171 | + assert.defined(result.error); |
| 172 | + }); |
| 173 | + |
| 174 | + it("should throw if process.env.MAINSAIL_CRYPTO_WORKER_COUNT is not an integer", async ({ serviceProvider }) => { |
| 175 | + process.env.MAINSAIL_CRYPTO_WORKER_COUNT = "1.5"; |
| 176 | + |
| 177 | + const result = serviceProvider.configSchema().validate(await importDefaults()); |
| 178 | + |
| 179 | + assert.defined(result.error); |
| 180 | + }); |
| 181 | + |
| 182 | + it("should throw if process.env.MAINSAIL_CRYPTO_WORKER_COUNT exceeds the available cpus", async ({ |
| 183 | + serviceProvider, |
| 184 | + }) => { |
| 185 | + process.env.MAINSAIL_CRYPTO_WORKER_COUNT = "9999"; |
| 186 | + |
| 187 | + const result = serviceProvider.configSchema().validate(await importDefaults()); |
| 188 | + |
| 189 | + assert.defined(result.error); |
| 190 | + }); |
| 191 | + |
| 192 | + it("should parse process.env.MAINSAIL_CRYPTO_WORKER_LOGGING_ENABLED", async ({ serviceProvider }) => { |
| 193 | + process.env.MAINSAIL_CRYPTO_WORKER_LOGGING_ENABLED = "true"; |
| 194 | + |
| 195 | + const result = serviceProvider.configSchema().validate(await importDefaults()); |
| 196 | + |
| 197 | + assert.undefined(result.error); |
| 198 | + assert.true(result.value.workerLoggingEnabled); |
| 199 | + }); |
| 200 | +}); |
0 commit comments