@malloy-publisher/server 0.0.198 → 0.0.200
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build.ts +30 -1
- package/dist/app/api-doc.yaml +127 -111
- package/dist/app/assets/{EnvironmentPage-C7rtH4mC.js → EnvironmentPage-CgKNjySu.js} +1 -1
- package/dist/app/assets/HomePage-BPIpMBjW.js +1 -0
- package/dist/app/assets/{MainPage-D38LtZDV.js → MainPage-CAwb8U82.js} +2 -2
- package/dist/app/assets/{ModelPage-DOol8Mz7.js → ModelPage-C0Uevsw9.js} +1 -1
- package/dist/app/assets/{PackagePage-0tgzA_kO.js → PackagePage-Cu-u9k1g.js} +1 -1
- package/dist/app/assets/{RouteError-BaMsOSly.js → RouteError-DVwPh2Ql.js} +1 -1
- package/dist/app/assets/{WorkbookPage-Cx4SePkx.js → WorkbookPage-DW38R2Zv.js} +1 -1
- package/dist/app/assets/{core-CbsC6R_Y.es-Cwf6asf3.js → core-C0vCMRDQ.es-D_ytHhjS.js} +10 -10
- package/dist/app/assets/{index-DL6BZTuw.js → index-BGdcKsFF.js} +1 -1
- package/dist/app/assets/{index-DNofXMxi.js → index-CTx4v4_3.js} +1 -1
- package/dist/app/assets/index-DE6d5jEy.js +452 -0
- package/dist/app/assets/{index.umd-B68wGGkM.js → index.umd-C1Mi1uRm.js} +1 -1
- package/dist/app/index.html +1 -1
- package/dist/instrumentation.mjs +57 -36
- package/dist/package_load_worker.mjs +12213 -0
- package/dist/server.mjs +4198 -3648
- package/package.json +2 -3
- package/src/config.spec.ts +246 -0
- package/src/config.ts +121 -1
- package/src/constants.ts +84 -1
- package/src/controller/compile.controller.ts +3 -1
- package/src/controller/connection.controller.spec.ts +803 -0
- package/src/controller/connection.controller.ts +207 -20
- package/src/controller/model.controller.ts +19 -1
- package/src/controller/query.controller.ts +22 -6
- package/src/controller/watch-mode.controller.ts +11 -2
- package/src/errors.spec.ts +44 -0
- package/src/errors.ts +34 -0
- package/src/health.spec.ts +90 -0
- package/src/health.ts +88 -45
- package/src/heap_check.spec.ts +144 -0
- package/src/heap_check.ts +144 -0
- package/src/instrumentation.ts +50 -0
- package/src/mcp/handler_utils.ts +14 -0
- package/src/mcp/tools/execute_query_tool.ts +52 -10
- package/src/oom_guards.integration.spec.ts +261 -0
- package/src/package_load/package_load_pool.spec.ts +252 -0
- package/src/package_load/package_load_pool.ts +920 -0
- package/src/package_load/package_load_worker.ts +980 -0
- package/src/package_load/protocol.ts +336 -0
- package/src/path_safety.ts +9 -3
- package/src/query_cap_metrics.spec.ts +89 -0
- package/src/query_cap_metrics.ts +115 -0
- package/src/query_concurrency.spec.ts +247 -0
- package/src/query_concurrency.ts +236 -0
- package/src/query_param_utils.ts +18 -0
- package/src/query_timeout.spec.ts +224 -0
- package/src/query_timeout.ts +178 -0
- package/src/server-old.ts +21 -1
- package/src/server.ts +61 -57
- package/src/service/connection.ts +8 -2
- package/src/service/db_utils.spec.ts +1 -1
- package/src/service/environment.ts +85 -4
- package/src/service/environment_admission.spec.ts +165 -1
- package/src/service/environment_store.spec.ts +103 -0
- package/src/service/environment_store.ts +98 -26
- package/src/service/filter_integration.spec.ts +110 -0
- package/src/service/given.ts +80 -0
- package/src/service/givens_integration.spec.ts +192 -0
- package/src/service/model.spec.ts +298 -3
- package/src/service/model.ts +362 -23
- package/src/service/model_limits.spec.ts +181 -0
- package/src/service/model_limits.ts +110 -0
- package/src/service/package.spec.ts +12 -6
- package/src/service/package.ts +263 -146
- package/src/service/package_worker_path.spec.ts +196 -0
- package/src/service/path_injection.spec.ts +39 -0
- package/src/stream_helpers.spec.ts +280 -0
- package/src/stream_helpers.ts +162 -0
- package/src/test_helpers/metrics_harness.ts +126 -0
- package/tests/integration/concurrent_package/concurrent_package.integration.spec.ts +280 -0
- package/dist/app/assets/HomePage-DwkH7OrS.js +0 -1
- package/dist/app/assets/index-U38AyjJL.js +0 -451
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End-to-end integration test for the OOM-mitigation guardrails
|
|
3
|
+
* (Steps 1-6). Mounts a real Express app with the same middleware
|
|
4
|
+
* + controller wiring `server.ts` uses, then exercises each layer
|
|
5
|
+
* of the defense via HTTP requests.
|
|
6
|
+
*
|
|
7
|
+
* What's covered, in firing order:
|
|
8
|
+
* 1. Admission gate — memory governor flags back-pressure →
|
|
9
|
+
* `ConnectionController` short-circuits with 503 BEFORE the
|
|
10
|
+
* connector is touched.
|
|
11
|
+
* 2. Concurrency middleware — when the pod is at
|
|
12
|
+
* `PUBLISHER_MAX_CONCURRENT_QUERIES`, new requests get 503
|
|
13
|
+
* BEFORE the controller is invoked.
|
|
14
|
+
* 3. Timeout — when the underlying driver takes longer than
|
|
15
|
+
* `PUBLISHER_QUERY_TIMEOUT_MS`, the request gets 504 and the
|
|
16
|
+
* abort signal fires.
|
|
17
|
+
* 4. Row cap — when the driver returns more rows than
|
|
18
|
+
* `PUBLISHER_MAX_QUERY_ROWS`, the request gets 413.
|
|
19
|
+
*
|
|
20
|
+
* Mocked: only the Malloy `Connection.runSQL`. Everything else
|
|
21
|
+
* (admission gate, concurrency middleware, timeout helper, cap
|
|
22
|
+
* detection, error-to-HTTP mapping) is real production code.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { Connection, RunSQLOptions } from "@malloydata/malloy";
|
|
26
|
+
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
|
27
|
+
import express from "express";
|
|
28
|
+
import sinon from "sinon";
|
|
29
|
+
import request from "supertest";
|
|
30
|
+
|
|
31
|
+
import { ConnectionController } from "./controller/connection.controller";
|
|
32
|
+
import { internalErrorToHttpError, ServiceUnavailableError } from "./errors";
|
|
33
|
+
import {
|
|
34
|
+
queryConcurrency,
|
|
35
|
+
resetActiveQueryCountForTesting,
|
|
36
|
+
resetQueryConcurrencyTelemetryForTesting,
|
|
37
|
+
} from "./query_concurrency";
|
|
38
|
+
import type { EnvironmentStore } from "./service/environment_store";
|
|
39
|
+
|
|
40
|
+
function buildApp(
|
|
41
|
+
runSQL: sinon.SinonStub,
|
|
42
|
+
opts: { assertCanAdmitQuery?: sinon.SinonStub } = {},
|
|
43
|
+
): {
|
|
44
|
+
app: express.Express;
|
|
45
|
+
assertCanAdmitQuery: sinon.SinonStub;
|
|
46
|
+
runSQL: sinon.SinonStub;
|
|
47
|
+
} {
|
|
48
|
+
const fakeConnection = { runSQL } as unknown as Connection;
|
|
49
|
+
const assertCanAdmitQuery =
|
|
50
|
+
opts.assertCanAdmitQuery ?? sinon.stub().returns(undefined);
|
|
51
|
+
const fakeEnv = { assertCanAdmitQuery };
|
|
52
|
+
const fakeStore = {
|
|
53
|
+
getEnvironment: sinon.stub().resolves(fakeEnv),
|
|
54
|
+
} as unknown as EnvironmentStore;
|
|
55
|
+
const controller = new ConnectionController(fakeStore);
|
|
56
|
+
// Bypass the connector lookup; the test only cares about the
|
|
57
|
+
// guardrail chain on top of a stub.
|
|
58
|
+
sinon
|
|
59
|
+
.stub(
|
|
60
|
+
controller as unknown as {
|
|
61
|
+
getMalloyConnection: (...args: unknown[]) => Promise<Connection>;
|
|
62
|
+
},
|
|
63
|
+
"getMalloyConnection",
|
|
64
|
+
)
|
|
65
|
+
.resolves(fakeConnection);
|
|
66
|
+
|
|
67
|
+
const app = express();
|
|
68
|
+
app.use(express.json());
|
|
69
|
+
|
|
70
|
+
// Same shape as `server.ts` registers for `/sqlQuery` — middleware
|
|
71
|
+
// first, then a thin route handler that delegates to the controller.
|
|
72
|
+
app.post(
|
|
73
|
+
"/api/v0/environments/:env/connections/:conn/sqlQuery",
|
|
74
|
+
queryConcurrency(),
|
|
75
|
+
async (req, res) => {
|
|
76
|
+
try {
|
|
77
|
+
const result = await controller.getConnectionQueryData(
|
|
78
|
+
req.params.env,
|
|
79
|
+
req.params.conn,
|
|
80
|
+
req.body.sqlStatement as string,
|
|
81
|
+
(req.body.options as string | undefined) ?? "",
|
|
82
|
+
);
|
|
83
|
+
res.status(200).json(result);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
const { json, status } = internalErrorToHttpError(error as Error);
|
|
86
|
+
res.status(status).json(json);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// Mirror the global error handler from `server.ts` — required
|
|
92
|
+
// for the concurrency middleware's `next(err)` path to map
|
|
93
|
+
// `ServiceUnavailableError` to a 503 instead of Express's
|
|
94
|
+
// default 500. Without this the integration test would be
|
|
95
|
+
// exercising a different error path than production.
|
|
96
|
+
app.use(
|
|
97
|
+
(
|
|
98
|
+
err: Error,
|
|
99
|
+
_req: express.Request,
|
|
100
|
+
res: express.Response,
|
|
101
|
+
_next: express.NextFunction,
|
|
102
|
+
): void => {
|
|
103
|
+
const { json, status } = internalErrorToHttpError(err);
|
|
104
|
+
res.status(status).json(json);
|
|
105
|
+
},
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
return { app, assertCanAdmitQuery, runSQL };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
describe("OOM guardrails: end-to-end chain", () => {
|
|
112
|
+
const savedEnv = {
|
|
113
|
+
max: process.env.PUBLISHER_MAX_QUERY_ROWS,
|
|
114
|
+
timeout: process.env.PUBLISHER_QUERY_TIMEOUT_MS,
|
|
115
|
+
concurrency: process.env.PUBLISHER_MAX_CONCURRENT_QUERIES,
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
beforeEach(() => {
|
|
119
|
+
resetActiveQueryCountForTesting();
|
|
120
|
+
resetQueryConcurrencyTelemetryForTesting();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
afterEach(() => {
|
|
124
|
+
sinon.restore();
|
|
125
|
+
resetActiveQueryCountForTesting();
|
|
126
|
+
resetQueryConcurrencyTelemetryForTesting();
|
|
127
|
+
// Restore env so later tests don't see this suite's bleed-through.
|
|
128
|
+
const restore = (key: keyof typeof savedEnv, envVar: string): void => {
|
|
129
|
+
if (savedEnv[key] === undefined) delete process.env[envVar];
|
|
130
|
+
else process.env[envVar] = savedEnv[key]!;
|
|
131
|
+
};
|
|
132
|
+
restore("max", "PUBLISHER_MAX_QUERY_ROWS");
|
|
133
|
+
restore("timeout", "PUBLISHER_QUERY_TIMEOUT_MS");
|
|
134
|
+
restore("concurrency", "PUBLISHER_MAX_CONCURRENT_QUERIES");
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("admission gate fires FIRST: 503 before the connector is touched", async () => {
|
|
138
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
139
|
+
const assertCanAdmitQuery = sinon
|
|
140
|
+
.stub()
|
|
141
|
+
.throws(
|
|
142
|
+
new ServiceUnavailableError(
|
|
143
|
+
"Publisher is under memory pressure and cannot accept new queries.",
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
const { app } = buildApp(runSQL, { assertCanAdmitQuery });
|
|
147
|
+
|
|
148
|
+
const res = await request(app)
|
|
149
|
+
.post("/api/v0/environments/e/connections/c/sqlQuery")
|
|
150
|
+
.send({ sqlStatement: "SELECT 1" });
|
|
151
|
+
|
|
152
|
+
expect(res.status).toBe(503);
|
|
153
|
+
// Critical load-shedding invariant: connector must NOT have
|
|
154
|
+
// been called when the gate rejects.
|
|
155
|
+
expect(runSQL.called).toBe(false);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("concurrency cap fires SECOND: 503 before the controller is touched, once the slot pool is full", async () => {
|
|
159
|
+
process.env.PUBLISHER_MAX_CONCURRENT_QUERIES = "1";
|
|
160
|
+
// First request: runSQL takes ~200 ms so the slot stays
|
|
161
|
+
// held while we fire the second. Using a real timer rather
|
|
162
|
+
// than an unresolved deferred keeps the test deterministic
|
|
163
|
+
// even when supertest spawns one ephemeral http server per
|
|
164
|
+
// `request(app)` call — both sockets will eventually close
|
|
165
|
+
// on their own regardless of orchestration order.
|
|
166
|
+
const runSQL = sinon
|
|
167
|
+
.stub()
|
|
168
|
+
.callsFake(
|
|
169
|
+
() =>
|
|
170
|
+
new Promise((resolve) =>
|
|
171
|
+
setTimeout(() => resolve({ rows: [], totalRows: 0 }), 200),
|
|
172
|
+
),
|
|
173
|
+
);
|
|
174
|
+
const { app } = buildApp(runSQL);
|
|
175
|
+
|
|
176
|
+
// Drive both requests in parallel — `Promise.all` keeps the
|
|
177
|
+
// event loop turning on the first request's pending timer
|
|
178
|
+
// while the second runs through the middleware.
|
|
179
|
+
const [first, second] = await Promise.all([
|
|
180
|
+
request(app)
|
|
181
|
+
.post("/api/v0/environments/e/connections/c/sqlQuery")
|
|
182
|
+
.send({ sqlStatement: "SELECT 1" }),
|
|
183
|
+
(async () => {
|
|
184
|
+
// Yield a few ticks so the first request actually
|
|
185
|
+
// enters the middleware and holds the slot before
|
|
186
|
+
// the second one is dispatched. 20 ms is far longer
|
|
187
|
+
// than any scheduler hop but well below the 200 ms
|
|
188
|
+
// runSQL timer, leaving the slot definitely held.
|
|
189
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
190
|
+
return request(app)
|
|
191
|
+
.post("/api/v0/environments/e/connections/c/sqlQuery")
|
|
192
|
+
.send({ sqlStatement: "SELECT 2" });
|
|
193
|
+
})(),
|
|
194
|
+
]);
|
|
195
|
+
|
|
196
|
+
expect(first.status).toBe(200);
|
|
197
|
+
expect(second.status).toBe(503);
|
|
198
|
+
// Connector should still have been called only once — the
|
|
199
|
+
// middleware rejected the second request before it reached
|
|
200
|
+
// the controller.
|
|
201
|
+
expect(runSQL.callCount).toBe(1);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("timeout fires THIRD: 504 when the driver outlasts PUBLISHER_QUERY_TIMEOUT_MS, and the abort signal is delivered to the driver", async () => {
|
|
205
|
+
process.env.PUBLISHER_QUERY_TIMEOUT_MS = "20";
|
|
206
|
+
let observedSignal: AbortSignal | undefined;
|
|
207
|
+
const runSQL = sinon.stub().callsFake(
|
|
208
|
+
(_sql: string, opts: RunSQLOptions) =>
|
|
209
|
+
new Promise((_resolve, reject) => {
|
|
210
|
+
observedSignal = opts.abortSignal;
|
|
211
|
+
opts.abortSignal?.addEventListener("abort", () =>
|
|
212
|
+
// The driver MUST reject when its abort signal
|
|
213
|
+
// fires; otherwise the timeout helper has nothing
|
|
214
|
+
// to convert to 504 (the success-after-timeout
|
|
215
|
+
// race is explicitly allowed).
|
|
216
|
+
reject(new Error("driver: aborted")),
|
|
217
|
+
);
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
const { app } = buildApp(runSQL);
|
|
221
|
+
|
|
222
|
+
const res = await request(app)
|
|
223
|
+
.post("/api/v0/environments/e/connections/c/sqlQuery")
|
|
224
|
+
.send({ sqlStatement: "SELECT pg_sleep(1)" });
|
|
225
|
+
|
|
226
|
+
expect(res.status).toBe(504);
|
|
227
|
+
expect(observedSignal).toBeDefined();
|
|
228
|
+
expect(observedSignal?.aborted).toBe(true);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("row cap fires LAST: 413 when the driver overshoots PUBLISHER_MAX_QUERY_ROWS", async () => {
|
|
232
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "2";
|
|
233
|
+
// Driver returns 3 rows even though we asked for cap+1 = 3
|
|
234
|
+
// — the sentinel pattern catches this and 413s.
|
|
235
|
+
const rows = [{ a: 1 }, { a: 2 }, { a: 3 }];
|
|
236
|
+
const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
|
|
237
|
+
const { app } = buildApp(runSQL);
|
|
238
|
+
|
|
239
|
+
const res = await request(app)
|
|
240
|
+
.post("/api/v0/environments/e/connections/c/sqlQuery")
|
|
241
|
+
.send({ sqlStatement: "SELECT * FROM big_table" });
|
|
242
|
+
|
|
243
|
+
expect(res.status).toBe(413);
|
|
244
|
+
expect(JSON.stringify(res.body)).toContain("more than 2 rows");
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("happy path: all gates pass-through, 200 with the result body", async () => {
|
|
248
|
+
const rows = [{ a: 1 }];
|
|
249
|
+
const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
|
|
250
|
+
const { app } = buildApp(runSQL);
|
|
251
|
+
|
|
252
|
+
const res = await request(app)
|
|
253
|
+
.post("/api/v0/environments/e/connections/c/sqlQuery")
|
|
254
|
+
.send({ sqlStatement: "SELECT 1" });
|
|
255
|
+
|
|
256
|
+
expect(res.status).toBe(200);
|
|
257
|
+
expect(res.body).toEqual({
|
|
258
|
+
data: JSON.stringify({ rows, totalRows: rows.length }),
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
});
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit + light integration tests for the PackageLoadPool.
|
|
3
|
+
*
|
|
4
|
+
* The pool runs real `worker_threads` workers under the hood. These
|
|
5
|
+
* tests intentionally exercise that path so we catch regressions in
|
|
6
|
+
* RPC routing, queueing, lifecycle, and error propagation that
|
|
7
|
+
* wouldn't surface in a pure mock.
|
|
8
|
+
*
|
|
9
|
+
* Pool reuse strategy
|
|
10
|
+
* -------------------
|
|
11
|
+
* The "real worker" tests share a single `PackageLoadPool` across
|
|
12
|
+
* cases via Bun's `beforeAll`/`afterAll`. The worker itself owns no
|
|
13
|
+
* native handles (all duckdb work runs on the main thread); we still
|
|
14
|
+
* share to keep per-test overhead low and to match the production
|
|
15
|
+
* deployment, where the pool spawns workers up-front and reuses them.
|
|
16
|
+
*/
|
|
17
|
+
import {
|
|
18
|
+
afterAll,
|
|
19
|
+
afterEach,
|
|
20
|
+
beforeAll,
|
|
21
|
+
beforeEach,
|
|
22
|
+
describe,
|
|
23
|
+
expect,
|
|
24
|
+
it,
|
|
25
|
+
} from "bun:test";
|
|
26
|
+
import * as fs from "fs";
|
|
27
|
+
import * as os from "os";
|
|
28
|
+
import * as path from "path";
|
|
29
|
+
import {
|
|
30
|
+
PackageLoadPool,
|
|
31
|
+
__setPackageLoadPoolForTests,
|
|
32
|
+
getPackageLoadWorkerCount,
|
|
33
|
+
} from "./package_load_pool";
|
|
34
|
+
|
|
35
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
36
|
+
// getPackageLoadWorkerCount — env var parsing
|
|
37
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
describe("getPackageLoadWorkerCount", () => {
|
|
40
|
+
const ORIGINAL = process.env.PACKAGE_LOAD_WORKERS;
|
|
41
|
+
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
if (ORIGINAL === undefined) {
|
|
44
|
+
delete process.env.PACKAGE_LOAD_WORKERS;
|
|
45
|
+
} else {
|
|
46
|
+
process.env.PACKAGE_LOAD_WORKERS = ORIGINAL;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("defaults to 1 worker when env unset", () => {
|
|
51
|
+
delete process.env.PACKAGE_LOAD_WORKERS;
|
|
52
|
+
expect(getPackageLoadWorkerCount()).toBe(1);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("throws when env value is non-numeric", () => {
|
|
56
|
+
process.env.PACKAGE_LOAD_WORKERS = "not-a-number";
|
|
57
|
+
expect(() => getPackageLoadWorkerCount()).toThrow(/positive integer/);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("throws when env value is negative", () => {
|
|
61
|
+
process.env.PACKAGE_LOAD_WORKERS = "-2";
|
|
62
|
+
expect(() => getPackageLoadWorkerCount()).toThrow(/positive integer/);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("throws when env value is 0 (no in-process fallback)", () => {
|
|
66
|
+
process.env.PACKAGE_LOAD_WORKERS = "0";
|
|
67
|
+
expect(() => getPackageLoadWorkerCount()).toThrow(/positive integer/);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("honors positive overrides", () => {
|
|
71
|
+
process.env.PACKAGE_LOAD_WORKERS = "4";
|
|
72
|
+
expect(getPackageLoadWorkerCount()).toBe(4);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
77
|
+
// PackageLoadPool constructor validation
|
|
78
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
79
|
+
|
|
80
|
+
describe("PackageLoadPool constructor", () => {
|
|
81
|
+
it("throws when maxWorkers is 0", () => {
|
|
82
|
+
expect(() => new PackageLoadPool(0)).toThrow(/maxWorkers >= 1/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("throws when maxWorkers is negative", () => {
|
|
86
|
+
expect(() => new PackageLoadPool(-1)).toThrow(/maxWorkers >= 1/);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
91
|
+
// PackageLoadPool — real worker loads a package
|
|
92
|
+
//
|
|
93
|
+
// One shared pool across the describe; each test gets its own temp
|
|
94
|
+
// package directory and its own DuckDB connection.
|
|
95
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
96
|
+
|
|
97
|
+
describe("PackageLoadPool (real worker)", () => {
|
|
98
|
+
let pool: PackageLoadPool;
|
|
99
|
+
let tempDir: string;
|
|
100
|
+
|
|
101
|
+
beforeAll(() => {
|
|
102
|
+
pool = new PackageLoadPool(1);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
afterAll(async () => {
|
|
106
|
+
await pool.shutdown();
|
|
107
|
+
await __setPackageLoadPoolForTests(null);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
beforeEach(() => {
|
|
111
|
+
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "publisher-compile-"));
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
afterEach(() => {
|
|
115
|
+
if (tempDir) {
|
|
116
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
117
|
+
tempDir = "";
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
function writeManifest(dir: string, name = "pkg"): void {
|
|
122
|
+
fs.writeFileSync(
|
|
123
|
+
path.join(dir, "publisher.json"),
|
|
124
|
+
JSON.stringify({ name, description: "test package" }),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function buildConfig(): Promise<{
|
|
129
|
+
malloyConfig: import("@malloydata/malloy").MalloyConfig;
|
|
130
|
+
duckdb: { close: () => Promise<void> };
|
|
131
|
+
}> {
|
|
132
|
+
const { MalloyConfig, FixedConnectionMap } = await import(
|
|
133
|
+
"@malloydata/malloy"
|
|
134
|
+
);
|
|
135
|
+
const { DuckDBConnection } = await import("@malloydata/db-duckdb");
|
|
136
|
+
const duckdb = new DuckDBConnection("duckdb", ":memory:");
|
|
137
|
+
const connections = new FixedConnectionMap(
|
|
138
|
+
new Map([["duckdb", duckdb]]),
|
|
139
|
+
"duckdb",
|
|
140
|
+
);
|
|
141
|
+
const malloyConfig = new MalloyConfig({ connections: {} });
|
|
142
|
+
malloyConfig.wrapConnections(() => connections);
|
|
143
|
+
return { malloyConfig, duckdb };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
it("loads a trivial single-model package and returns a hydratable modelDef", async () => {
|
|
147
|
+
writeManifest(tempDir);
|
|
148
|
+
fs.writeFileSync(
|
|
149
|
+
path.join(tempDir, "trivial.malloy"),
|
|
150
|
+
`source: nums is duckdb.sql("select 1 as a, 2 as b") extend {
|
|
151
|
+
measure: total is a.sum()
|
|
152
|
+
}`,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
const { malloyConfig, duckdb } = await buildConfig();
|
|
156
|
+
try {
|
|
157
|
+
const outcome = await pool.loadPackage({
|
|
158
|
+
packagePath: tempDir,
|
|
159
|
+
packageName: "pkg",
|
|
160
|
+
malloyConfig,
|
|
161
|
+
defaultConnectionName: "duckdb",
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(outcome.packageMetadata.name).toBe("pkg");
|
|
165
|
+
expect(outcome.models).toHaveLength(1);
|
|
166
|
+
const m = outcome.models[0];
|
|
167
|
+
expect(m.modelPath).toBe("trivial.malloy");
|
|
168
|
+
expect(m.modelType).toBe("model");
|
|
169
|
+
expect(m.compilationError).toBeUndefined();
|
|
170
|
+
expect(m.modelDef).toBeDefined();
|
|
171
|
+
expect(Array.isArray(m.sources)).toBe(true);
|
|
172
|
+
expect(outcome.loadDurationMs).toBeGreaterThan(0);
|
|
173
|
+
} finally {
|
|
174
|
+
await duckdb.close();
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("propagates a per-model compile failure in-band (not as a rejected Promise)", async () => {
|
|
179
|
+
writeManifest(tempDir);
|
|
180
|
+
fs.writeFileSync(
|
|
181
|
+
path.join(tempDir, "broken.malloy"),
|
|
182
|
+
`source: bad is duckdb.sql("select 1 as a") extend {
|
|
183
|
+
measure: oops is THIS_FUNC_DOES_NOT_EXIST(a)
|
|
184
|
+
}`,
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const { malloyConfig, duckdb } = await buildConfig();
|
|
188
|
+
try {
|
|
189
|
+
const outcome = await pool.loadPackage({
|
|
190
|
+
packagePath: tempDir,
|
|
191
|
+
packageName: "pkg",
|
|
192
|
+
malloyConfig,
|
|
193
|
+
defaultConnectionName: "duckdb",
|
|
194
|
+
});
|
|
195
|
+
expect(outcome.models).toHaveLength(1);
|
|
196
|
+
const m = outcome.models[0];
|
|
197
|
+
expect(m.compilationError).toBeDefined();
|
|
198
|
+
expect(m.modelDef).toBeUndefined();
|
|
199
|
+
} finally {
|
|
200
|
+
await duckdb.close();
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("ignores embedded csv/parquet files (database probing is a main-thread concern)", async () => {
|
|
205
|
+
// The worker is pure-CPU: it reads the manifest and compiles
|
|
206
|
+
// .malloy files only. Database probing stays on the main thread
|
|
207
|
+
// (Package.readDatabases) so the worker doesn't need to dlopen
|
|
208
|
+
// duckdb-native. Verify that dropping a .csv next to the model
|
|
209
|
+
// doesn't crash the worker and doesn't show up in the result.
|
|
210
|
+
writeManifest(tempDir);
|
|
211
|
+
fs.writeFileSync(path.join(tempDir, "rows.csv"), "a,b\n1,2\n3,4\n5,6\n");
|
|
212
|
+
fs.writeFileSync(
|
|
213
|
+
path.join(tempDir, "trivial.malloy"),
|
|
214
|
+
`source: nums is duckdb.sql("select 1 as a") extend {\n measure: total is a.sum()\n}`,
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const { malloyConfig, duckdb } = await buildConfig();
|
|
218
|
+
try {
|
|
219
|
+
const outcome = await pool.loadPackage({
|
|
220
|
+
packagePath: tempDir,
|
|
221
|
+
packageName: "pkg",
|
|
222
|
+
malloyConfig,
|
|
223
|
+
defaultConnectionName: "duckdb",
|
|
224
|
+
});
|
|
225
|
+
expect(outcome.models).toHaveLength(1);
|
|
226
|
+
expect(outcome.models[0].compilationError).toBeUndefined();
|
|
227
|
+
} finally {
|
|
228
|
+
await duckdb.close();
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
234
|
+
// PackageLoadPool — shutdown rejects new submissions
|
|
235
|
+
// Separate describe so the shutdown doesn't poison the shared pool above.
|
|
236
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
237
|
+
|
|
238
|
+
describe("PackageLoadPool (shutdown)", () => {
|
|
239
|
+
it("rejects loadPackage() after shutdown()", async () => {
|
|
240
|
+
const { MalloyConfig } = await import("@malloydata/malloy");
|
|
241
|
+
const pool = new PackageLoadPool(1);
|
|
242
|
+
await pool.shutdown();
|
|
243
|
+
await expect(
|
|
244
|
+
pool.loadPackage({
|
|
245
|
+
packagePath: "/tmp/nowhere",
|
|
246
|
+
packageName: "nowhere",
|
|
247
|
+
malloyConfig: new MalloyConfig({ connections: {} }),
|
|
248
|
+
defaultConnectionName: "duckdb",
|
|
249
|
+
}),
|
|
250
|
+
).rejects.toThrow("shutting down");
|
|
251
|
+
});
|
|
252
|
+
});
|