@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,803 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Connection,
|
|
3
|
+
QueryRecord,
|
|
4
|
+
RunSQLOptions,
|
|
5
|
+
StreamingConnection,
|
|
6
|
+
} from "@malloydata/malloy";
|
|
7
|
+
import { afterEach, describe, expect, it } from "bun:test";
|
|
8
|
+
import sinon from "sinon";
|
|
9
|
+
|
|
10
|
+
import { BadRequestError, PayloadTooLargeError } from "../errors";
|
|
11
|
+
import type { EnvironmentStore } from "../service/environment_store";
|
|
12
|
+
import { ConnectionController } from "./connection.controller";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Tests for the row-cap path inside
|
|
16
|
+
* {@link ConnectionController.getConnectionQueryData}. Mocks the
|
|
17
|
+
* underlying Malloy `Connection.runSQL` so the test runs without a
|
|
18
|
+
* live database; the EnvironmentStore is similarly stubbed via
|
|
19
|
+
* sinon — we only exercise the controller's request-shaping and
|
|
20
|
+
* overflow-detection logic.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Build a controller whose `getMalloyConnection` resolves to a
|
|
24
|
+
* fake `Connection` with a sinon-stubbed `runSQL`. We bypass the
|
|
25
|
+
* normal EnvironmentStore lookup entirely so the test stays a
|
|
26
|
+
* single-file unit test.
|
|
27
|
+
*
|
|
28
|
+
* `assertCanAdmitQuery` defaults to a no-op (controller is never
|
|
29
|
+
* back-pressured); pass an overridden stub via
|
|
30
|
+
* `{ assertCanAdmitQuery }` to drive the 503 path.
|
|
31
|
+
*
|
|
32
|
+
* Hoisted to module scope so the admission-gate describe can reuse it.
|
|
33
|
+
*/
|
|
34
|
+
function buildController(
|
|
35
|
+
runSQL: sinon.SinonStub,
|
|
36
|
+
opts: { assertCanAdmitQuery?: sinon.SinonStub } = {},
|
|
37
|
+
): {
|
|
38
|
+
controller: ConnectionController;
|
|
39
|
+
runSQL: sinon.SinonStub;
|
|
40
|
+
assertCanAdmitQuery: sinon.SinonStub;
|
|
41
|
+
} {
|
|
42
|
+
const fakeConnection = { runSQL } as unknown as Connection;
|
|
43
|
+
const assertCanAdmitQuery =
|
|
44
|
+
opts.assertCanAdmitQuery ?? sinon.stub().returns(undefined);
|
|
45
|
+
const fakeEnv = { assertCanAdmitQuery };
|
|
46
|
+
const fakeStore = {
|
|
47
|
+
getEnvironment: sinon.stub().resolves(fakeEnv),
|
|
48
|
+
} as unknown as EnvironmentStore;
|
|
49
|
+
const controller = new ConnectionController(fakeStore);
|
|
50
|
+
// `getMalloyConnection` is private; cast through `unknown` so we
|
|
51
|
+
// can swap it without exposing internals on the public surface.
|
|
52
|
+
sinon
|
|
53
|
+
.stub(
|
|
54
|
+
controller as unknown as {
|
|
55
|
+
getMalloyConnection: (...args: unknown[]) => Promise<Connection>;
|
|
56
|
+
},
|
|
57
|
+
"getMalloyConnection",
|
|
58
|
+
)
|
|
59
|
+
.resolves(fakeConnection);
|
|
60
|
+
return { controller, runSQL, assertCanAdmitQuery };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
describe("ConnectionController.getConnectionQueryData row cap", () => {
|
|
64
|
+
const originalEnv = process.env.PUBLISHER_MAX_QUERY_ROWS;
|
|
65
|
+
|
|
66
|
+
afterEach(() => {
|
|
67
|
+
sinon.restore();
|
|
68
|
+
if (originalEnv === undefined) {
|
|
69
|
+
delete process.env.PUBLISHER_MAX_QUERY_ROWS;
|
|
70
|
+
} else {
|
|
71
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = originalEnv;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("passes the SQL through verbatim and asks the connector for cap+1 rows", async () => {
|
|
76
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "5";
|
|
77
|
+
const runSQL = sinon.stub().resolves({ rows: [{ a: 1 }], totalRows: 1 });
|
|
78
|
+
const { controller } = buildController(runSQL);
|
|
79
|
+
|
|
80
|
+
const result = await controller.getConnectionQueryData(
|
|
81
|
+
"env",
|
|
82
|
+
"conn",
|
|
83
|
+
"SELECT a FROM t",
|
|
84
|
+
"",
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
expect(runSQL.calledOnce).toBe(true);
|
|
88
|
+
expect(runSQL.firstCall.args[0]).toBe("SELECT a FROM t");
|
|
89
|
+
const opts = runSQL.firstCall.args[1] as { rowLimit?: number };
|
|
90
|
+
expect(opts.rowLimit).toBe(6);
|
|
91
|
+
const parsed = JSON.parse(result.data ?? "") as {
|
|
92
|
+
rows: Array<{ a: number }>;
|
|
93
|
+
totalRows: number;
|
|
94
|
+
};
|
|
95
|
+
expect(parsed.rows).toEqual([{ a: 1 }]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("returns the result when row count equals the cap exactly", async () => {
|
|
99
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "3";
|
|
100
|
+
const rows = [{ a: 1 }, { a: 2 }, { a: 3 }];
|
|
101
|
+
const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
|
|
102
|
+
const { controller } = buildController(runSQL);
|
|
103
|
+
|
|
104
|
+
const result = await controller.getConnectionQueryData(
|
|
105
|
+
"env",
|
|
106
|
+
"conn",
|
|
107
|
+
"SELECT a FROM t",
|
|
108
|
+
"",
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const parsed = JSON.parse(result.data ?? "") as { rows: unknown[] };
|
|
112
|
+
expect(parsed.rows.length).toBe(3);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("throws PayloadTooLargeError when the connection returns cap+1 rows", async () => {
|
|
116
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "3";
|
|
117
|
+
const rows = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }];
|
|
118
|
+
const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
|
|
119
|
+
const { controller } = buildController(runSQL);
|
|
120
|
+
|
|
121
|
+
await expect(
|
|
122
|
+
controller.getConnectionQueryData(
|
|
123
|
+
"env",
|
|
124
|
+
"conn",
|
|
125
|
+
"SELECT a FROM t",
|
|
126
|
+
"",
|
|
127
|
+
),
|
|
128
|
+
).rejects.toBeInstanceOf(PayloadTooLargeError);
|
|
129
|
+
await expect(
|
|
130
|
+
controller.getConnectionQueryData(
|
|
131
|
+
"env",
|
|
132
|
+
"conn",
|
|
133
|
+
"SELECT a FROM t",
|
|
134
|
+
"",
|
|
135
|
+
),
|
|
136
|
+
).rejects.toThrow("more than 3 rows");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("forwards non-SELECT statements verbatim with rowLimit applied", async () => {
|
|
140
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "10";
|
|
141
|
+
const rows = [{ table_name: "a" }, { table_name: "b" }];
|
|
142
|
+
const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
|
|
143
|
+
const { controller } = buildController(runSQL);
|
|
144
|
+
|
|
145
|
+
await controller.getConnectionQueryData("env", "conn", "SHOW TABLES", "");
|
|
146
|
+
|
|
147
|
+
expect(runSQL.firstCall.args[0]).toBe("SHOW TABLES");
|
|
148
|
+
const opts = runSQL.firstCall.args[1] as { rowLimit?: number };
|
|
149
|
+
expect(opts.rowLimit).toBe(11);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("disables rowLimit when PUBLISHER_MAX_QUERY_ROWS=0", async () => {
|
|
153
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "0";
|
|
154
|
+
const rows = Array.from({ length: 100 }, (_, i) => ({ a: i }));
|
|
155
|
+
const runSQL = sinon.stub().resolves({ rows, totalRows: rows.length });
|
|
156
|
+
const { controller } = buildController(runSQL);
|
|
157
|
+
|
|
158
|
+
const result = await controller.getConnectionQueryData(
|
|
159
|
+
"env",
|
|
160
|
+
"conn",
|
|
161
|
+
"SELECT a FROM t",
|
|
162
|
+
"",
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
expect(runSQL.firstCall.args[0]).toBe("SELECT a FROM t");
|
|
166
|
+
const opts = runSQL.firstCall.args[1] as { rowLimit?: number };
|
|
167
|
+
expect(opts.rowLimit).toBeUndefined();
|
|
168
|
+
const parsed = JSON.parse(result.data ?? "") as { rows: unknown[] };
|
|
169
|
+
expect(parsed.rows.length).toBe(100);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("uses the default cap when PUBLISHER_MAX_QUERY_ROWS is unset", async () => {
|
|
173
|
+
delete process.env.PUBLISHER_MAX_QUERY_ROWS;
|
|
174
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
175
|
+
const { controller } = buildController(runSQL);
|
|
176
|
+
|
|
177
|
+
await controller.getConnectionQueryData("env", "conn", "SELECT 1", "");
|
|
178
|
+
|
|
179
|
+
const opts = runSQL.firstCall.args[1] as { rowLimit?: number };
|
|
180
|
+
// Default cap is 100_000, so we request cap+1 = 100_001.
|
|
181
|
+
expect(opts.rowLimit).toBe(100_001);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("preserves a caller-supplied rowLimit when it is below the cap", async () => {
|
|
185
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "100";
|
|
186
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
187
|
+
const { controller } = buildController(runSQL);
|
|
188
|
+
|
|
189
|
+
await controller.getConnectionQueryData(
|
|
190
|
+
"env",
|
|
191
|
+
"conn",
|
|
192
|
+
"SELECT 1",
|
|
193
|
+
JSON.stringify({ rowLimit: 5 }),
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
const opts = runSQL.firstCall.args[1] as { rowLimit?: number };
|
|
197
|
+
expect(opts.rowLimit).toBe(5);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("clamps a caller-supplied rowLimit that exceeds the cap+1 sentinel and replaces any caller-supplied abortSignal with the publisher's timeout signal", async () => {
|
|
201
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "10";
|
|
202
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
203
|
+
const { controller } = buildController(runSQL);
|
|
204
|
+
|
|
205
|
+
await controller.getConnectionQueryData(
|
|
206
|
+
"env",
|
|
207
|
+
"conn",
|
|
208
|
+
"SELECT 1",
|
|
209
|
+
JSON.stringify({ rowLimit: 50, abortSignal: {} }),
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
const opts = runSQL.firstCall.args[1] as {
|
|
213
|
+
rowLimit?: number;
|
|
214
|
+
abortSignal?: AbortSignal;
|
|
215
|
+
};
|
|
216
|
+
expect(opts.rowLimit).toBe(11);
|
|
217
|
+
// Step 5: the controller always installs its own AbortSignal
|
|
218
|
+
// (sourced from runWithQueryTimeout) so a hung driver call can
|
|
219
|
+
// be canceled. The caller-supplied placeholder is dropped at
|
|
220
|
+
// the JSON-parse boundary — it could never be a real
|
|
221
|
+
// AbortSignal anyway — and replaced with a live one. Prior to
|
|
222
|
+
// Step 5 the assertion was `toBeUndefined`; that behavior is
|
|
223
|
+
// gone on purpose.
|
|
224
|
+
expect(opts.abortSignal).toBeInstanceOf(AbortSignal);
|
|
225
|
+
expect(opts.abortSignal?.aborted).toBe(false);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Express parses repeated query parameters and array-shaped JSON bodies
|
|
230
|
+
* as `string[]`, not `string`. The route handlers up-cast for TypeScript,
|
|
231
|
+
* so we re-validate at the controller boundary. CodeQL flagged the
|
|
232
|
+
* unvalidated dataflow as
|
|
233
|
+
* `js/type-confusion-through-parameter-tampering`.
|
|
234
|
+
*/
|
|
235
|
+
it.each([
|
|
236
|
+
["array sqlStatement", ["SELECT 1", "SELECT 2"]],
|
|
237
|
+
["object sqlStatement", { evil: true }],
|
|
238
|
+
["number sqlStatement", 42],
|
|
239
|
+
["null sqlStatement", null],
|
|
240
|
+
["undefined sqlStatement", undefined],
|
|
241
|
+
])(
|
|
242
|
+
"rejects non-string sqlStatement (%s) with BadRequestError",
|
|
243
|
+
async (_label, sqlStatement) => {
|
|
244
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
245
|
+
const { controller } = buildController(runSQL);
|
|
246
|
+
|
|
247
|
+
await expect(
|
|
248
|
+
controller.getConnectionQueryData(
|
|
249
|
+
"env",
|
|
250
|
+
"conn",
|
|
251
|
+
sqlStatement as unknown as string,
|
|
252
|
+
"",
|
|
253
|
+
),
|
|
254
|
+
).rejects.toBeInstanceOf(BadRequestError);
|
|
255
|
+
expect(runSQL.called).toBe(false);
|
|
256
|
+
},
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
it.each([
|
|
260
|
+
["array options", ["{}", "{}"]],
|
|
261
|
+
["object options", { foo: "bar" }],
|
|
262
|
+
["number options", 42],
|
|
263
|
+
])(
|
|
264
|
+
"rejects non-string options (%s) with BadRequestError",
|
|
265
|
+
async (_label, options) => {
|
|
266
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
267
|
+
const { controller } = buildController(runSQL);
|
|
268
|
+
|
|
269
|
+
await expect(
|
|
270
|
+
controller.getConnectionQueryData(
|
|
271
|
+
"env",
|
|
272
|
+
"conn",
|
|
273
|
+
"SELECT 1",
|
|
274
|
+
options as unknown as string,
|
|
275
|
+
),
|
|
276
|
+
).rejects.toBeInstanceOf(BadRequestError);
|
|
277
|
+
expect(runSQL.called).toBe(false);
|
|
278
|
+
},
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
it("accepts undefined / null options as 'no options'", async () => {
|
|
282
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
283
|
+
const { controller } = buildController(runSQL);
|
|
284
|
+
|
|
285
|
+
await controller.getConnectionQueryData(
|
|
286
|
+
"env",
|
|
287
|
+
"conn",
|
|
288
|
+
"SELECT 1",
|
|
289
|
+
undefined as unknown as string,
|
|
290
|
+
);
|
|
291
|
+
await controller.getConnectionQueryData(
|
|
292
|
+
"env",
|
|
293
|
+
"conn",
|
|
294
|
+
"SELECT 1",
|
|
295
|
+
null as unknown as string,
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
expect(runSQL.callCount).toBe(2);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* `JSON.parse("null")` / `'"foo"'` / `'42'` / `'[1,2,3]'` all parse to
|
|
303
|
+
* non-objects. Without a guard, `runSQLOptions.abortSignal` would crash
|
|
304
|
+
* on `null` (→ 500) or `runSQLOptions.rowLimit = ...` would mutate the
|
|
305
|
+
* caller's array / coerce a primitive and pass that to the connector.
|
|
306
|
+
* Reject at the controller boundary alongside the other type guards.
|
|
307
|
+
*/
|
|
308
|
+
it.each([
|
|
309
|
+
["JSON null", "null"],
|
|
310
|
+
["JSON string", '"hello"'],
|
|
311
|
+
["JSON number", "42"],
|
|
312
|
+
["JSON boolean", "true"],
|
|
313
|
+
["JSON array", "[1,2,3]"],
|
|
314
|
+
])(
|
|
315
|
+
"rejects non-object JSON options (%s) with BadRequestError",
|
|
316
|
+
async (_label, options) => {
|
|
317
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
318
|
+
const { controller } = buildController(runSQL);
|
|
319
|
+
|
|
320
|
+
await expect(
|
|
321
|
+
controller.getConnectionQueryData(
|
|
322
|
+
"env",
|
|
323
|
+
"conn",
|
|
324
|
+
"SELECT 1",
|
|
325
|
+
options,
|
|
326
|
+
),
|
|
327
|
+
).rejects.toBeInstanceOf(BadRequestError);
|
|
328
|
+
expect(runSQL.called).toBe(false);
|
|
329
|
+
},
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
it("rejects malformed JSON options with BadRequestError", async () => {
|
|
333
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
334
|
+
const { controller } = buildController(runSQL);
|
|
335
|
+
|
|
336
|
+
await expect(
|
|
337
|
+
controller.getConnectionQueryData(
|
|
338
|
+
"env",
|
|
339
|
+
"conn",
|
|
340
|
+
"SELECT 1",
|
|
341
|
+
"{not json",
|
|
342
|
+
),
|
|
343
|
+
).rejects.toBeInstanceOf(BadRequestError);
|
|
344
|
+
expect(runSQL.called).toBe(false);
|
|
345
|
+
});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Tests for the streaming branch of
|
|
350
|
+
* {@link ConnectionController.getConnectionQueryData}. When the
|
|
351
|
+
* connection implements `canStream`, the controller routes through
|
|
352
|
+
* `streamSqlWithBudget` so the byte cap can fire mid-stream. The
|
|
353
|
+
* row cap is still enforced via `RunSQLOptions.rowLimit = cap+1` on
|
|
354
|
+
* the way in, plus an overflow check on the way out (same sentinel
|
|
355
|
+
* pattern as the non-streaming path).
|
|
356
|
+
*/
|
|
357
|
+
describe("ConnectionController.getConnectionQueryData streaming", () => {
|
|
358
|
+
const originalRowsEnv = process.env.PUBLISHER_MAX_QUERY_ROWS;
|
|
359
|
+
const originalBytesEnv = process.env.PUBLISHER_MAX_RESPONSE_BYTES;
|
|
360
|
+
|
|
361
|
+
afterEach(() => {
|
|
362
|
+
sinon.restore();
|
|
363
|
+
if (originalRowsEnv === undefined) {
|
|
364
|
+
delete process.env.PUBLISHER_MAX_QUERY_ROWS;
|
|
365
|
+
} else {
|
|
366
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = originalRowsEnv;
|
|
367
|
+
}
|
|
368
|
+
if (originalBytesEnv === undefined) {
|
|
369
|
+
delete process.env.PUBLISHER_MAX_RESPONSE_BYTES;
|
|
370
|
+
} else {
|
|
371
|
+
process.env.PUBLISHER_MAX_RESPONSE_BYTES = originalBytesEnv;
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Build a controller backed by a fake `StreamingConnection`.
|
|
377
|
+
* `seenSql` and `seenOptions` let tests assert the controller
|
|
378
|
+
* passed the SQL straight through and forwarded the budget-derived
|
|
379
|
+
* `rowLimit` to the driver.
|
|
380
|
+
*/
|
|
381
|
+
function buildStreamingController(opts: {
|
|
382
|
+
rows: QueryRecord[];
|
|
383
|
+
honorRowLimit?: boolean;
|
|
384
|
+
}): {
|
|
385
|
+
controller: ConnectionController;
|
|
386
|
+
seenSql: { value: string | undefined };
|
|
387
|
+
seenOptions: { value: RunSQLOptions | undefined };
|
|
388
|
+
} {
|
|
389
|
+
const { rows, honorRowLimit = true } = opts;
|
|
390
|
+
const seenSql = { value: undefined as string | undefined };
|
|
391
|
+
const seenOptions = { value: undefined as RunSQLOptions | undefined };
|
|
392
|
+
const fakeConnection = {
|
|
393
|
+
canStream(): true {
|
|
394
|
+
return true;
|
|
395
|
+
},
|
|
396
|
+
async *runSQLStream(
|
|
397
|
+
sql: string,
|
|
398
|
+
options?: RunSQLOptions,
|
|
399
|
+
): AsyncIterableIterator<QueryRecord> {
|
|
400
|
+
seenSql.value = sql;
|
|
401
|
+
seenOptions.value = options;
|
|
402
|
+
const limit =
|
|
403
|
+
honorRowLimit && typeof options?.rowLimit === "number"
|
|
404
|
+
? options.rowLimit
|
|
405
|
+
: rows.length;
|
|
406
|
+
for (let i = 0; i < Math.min(rows.length, limit); i += 1) {
|
|
407
|
+
yield rows[i];
|
|
408
|
+
}
|
|
409
|
+
},
|
|
410
|
+
} as unknown as StreamingConnection;
|
|
411
|
+
const fakeEnv = {
|
|
412
|
+
assertCanAdmitQuery: sinon.stub().returns(undefined),
|
|
413
|
+
};
|
|
414
|
+
const fakeStore = {
|
|
415
|
+
getEnvironment: sinon.stub().resolves(fakeEnv),
|
|
416
|
+
} as unknown as EnvironmentStore;
|
|
417
|
+
const controller = new ConnectionController(fakeStore);
|
|
418
|
+
sinon
|
|
419
|
+
.stub(
|
|
420
|
+
controller as unknown as {
|
|
421
|
+
getMalloyConnection: (...args: unknown[]) => Promise<Connection>;
|
|
422
|
+
},
|
|
423
|
+
"getMalloyConnection",
|
|
424
|
+
)
|
|
425
|
+
.resolves(fakeConnection as unknown as Connection);
|
|
426
|
+
return { controller, seenSql, seenOptions };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
it("routes through runSQLStream and asks the driver for cap+1 rows", async () => {
|
|
430
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "5";
|
|
431
|
+
const rows = [{ a: 1 }, { a: 2 }];
|
|
432
|
+
const { controller, seenSql, seenOptions } = buildStreamingController({
|
|
433
|
+
rows,
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
const result = await controller.getConnectionQueryData(
|
|
437
|
+
"env",
|
|
438
|
+
"conn",
|
|
439
|
+
"SELECT a FROM t",
|
|
440
|
+
"",
|
|
441
|
+
);
|
|
442
|
+
|
|
443
|
+
expect(seenSql.value).toBe("SELECT a FROM t");
|
|
444
|
+
expect(seenOptions.value?.rowLimit).toBe(6);
|
|
445
|
+
const parsed = JSON.parse(result.data ?? "") as { rows: QueryRecord[] };
|
|
446
|
+
expect(parsed.rows).toEqual(rows);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it("preserves a caller-supplied rowLimit when it is below the cap+1 ceiling", async () => {
|
|
450
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "100";
|
|
451
|
+
const { controller, seenOptions } = buildStreamingController({
|
|
452
|
+
rows: [{ a: 1 }],
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
await controller.getConnectionQueryData(
|
|
456
|
+
"env",
|
|
457
|
+
"conn",
|
|
458
|
+
"SELECT a FROM t",
|
|
459
|
+
JSON.stringify({ rowLimit: 10 }),
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
expect(seenOptions.value?.rowLimit).toBe(10);
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it("throws PayloadTooLargeError when the stream yields more than the row cap", async () => {
|
|
466
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "2";
|
|
467
|
+
const rows = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }];
|
|
468
|
+
const { controller } = buildStreamingController({
|
|
469
|
+
rows,
|
|
470
|
+
honorRowLimit: false,
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
await expect(
|
|
474
|
+
controller.getConnectionQueryData(
|
|
475
|
+
"env",
|
|
476
|
+
"conn",
|
|
477
|
+
"SELECT a FROM t",
|
|
478
|
+
"",
|
|
479
|
+
),
|
|
480
|
+
).rejects.toBeInstanceOf(PayloadTooLargeError);
|
|
481
|
+
await expect(
|
|
482
|
+
controller.getConnectionQueryData(
|
|
483
|
+
"env",
|
|
484
|
+
"conn",
|
|
485
|
+
"SELECT a FROM t",
|
|
486
|
+
"",
|
|
487
|
+
),
|
|
488
|
+
).rejects.toThrow("more than 2 rows");
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
it("throws PayloadTooLargeError when the stream exceeds the byte cap", async () => {
|
|
492
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "1000";
|
|
493
|
+
process.env.PUBLISHER_MAX_RESPONSE_BYTES = "60";
|
|
494
|
+
const big = "x".repeat(40);
|
|
495
|
+
const rows = [{ s: big }, { s: big }, { s: big }];
|
|
496
|
+
const { controller } = buildStreamingController({
|
|
497
|
+
rows,
|
|
498
|
+
honorRowLimit: false,
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
await expect(
|
|
502
|
+
controller.getConnectionQueryData(
|
|
503
|
+
"env",
|
|
504
|
+
"conn",
|
|
505
|
+
"SELECT s FROM t",
|
|
506
|
+
"",
|
|
507
|
+
),
|
|
508
|
+
).rejects.toBeInstanceOf(PayloadTooLargeError);
|
|
509
|
+
await expect(
|
|
510
|
+
controller.getConnectionQueryData(
|
|
511
|
+
"env",
|
|
512
|
+
"conn",
|
|
513
|
+
"SELECT s FROM t",
|
|
514
|
+
"",
|
|
515
|
+
),
|
|
516
|
+
).rejects.toThrow("exceeded 60 bytes");
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it("disables byte cap when PUBLISHER_MAX_RESPONSE_BYTES=0", async () => {
|
|
520
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "1000";
|
|
521
|
+
process.env.PUBLISHER_MAX_RESPONSE_BYTES = "0";
|
|
522
|
+
const big = "x".repeat(10_000);
|
|
523
|
+
const rows: QueryRecord[] = Array.from({ length: 3 }, () => ({ s: big }));
|
|
524
|
+
const { controller } = buildStreamingController({
|
|
525
|
+
rows,
|
|
526
|
+
honorRowLimit: false,
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
const result = await controller.getConnectionQueryData(
|
|
530
|
+
"env",
|
|
531
|
+
"conn",
|
|
532
|
+
"SELECT s FROM t",
|
|
533
|
+
"",
|
|
534
|
+
);
|
|
535
|
+
|
|
536
|
+
const parsed = JSON.parse(result.data ?? "") as { rows: QueryRecord[] };
|
|
537
|
+
expect(parsed.rows.length).toBe(3);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it("omits rowLimit when PUBLISHER_MAX_QUERY_ROWS=0 and no caller limit is given", async () => {
|
|
541
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "0";
|
|
542
|
+
const rows = [{ a: 1 }, { a: 2 }, { a: 3 }];
|
|
543
|
+
const { controller, seenOptions } = buildStreamingController({
|
|
544
|
+
rows,
|
|
545
|
+
honorRowLimit: false,
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
const result = await controller.getConnectionQueryData(
|
|
549
|
+
"env",
|
|
550
|
+
"conn",
|
|
551
|
+
"SELECT a FROM t",
|
|
552
|
+
"",
|
|
553
|
+
);
|
|
554
|
+
|
|
555
|
+
expect(seenOptions.value?.rowLimit).toBeUndefined();
|
|
556
|
+
const parsed = JSON.parse(result.data ?? "") as { rows: QueryRecord[] };
|
|
557
|
+
expect(parsed.rows.length).toBe(3);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it("replaces caller-supplied abortSignal with a real AbortSignal", async () => {
|
|
561
|
+
process.env.PUBLISHER_MAX_QUERY_ROWS = "10";
|
|
562
|
+
const { controller, seenOptions } = buildStreamingController({
|
|
563
|
+
rows: [{ a: 1 }],
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
await controller.getConnectionQueryData(
|
|
567
|
+
"env",
|
|
568
|
+
"conn",
|
|
569
|
+
"SELECT 1",
|
|
570
|
+
JSON.stringify({ abortSignal: {} }),
|
|
571
|
+
);
|
|
572
|
+
|
|
573
|
+
// The controller clears the caller-supplied abortSignal (which
|
|
574
|
+
// arrives over the wire as a JSON-shaped placeholder, not a
|
|
575
|
+
// real AbortSignal), and the streaming helper installs its own
|
|
576
|
+
// so it can abort the iterator on overflow.
|
|
577
|
+
const signal = seenOptions.value?.abortSignal;
|
|
578
|
+
expect(signal).toBeInstanceOf(AbortSignal);
|
|
579
|
+
expect(signal?.aborted).toBe(false);
|
|
580
|
+
});
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Tests that the controller calls {@link Environment.assertCanAdmitQuery}
|
|
585
|
+
* *before* doing any disk / DB work. Under back-pressure the publisher
|
|
586
|
+
* must shed load immediately with HTTP 503 — not start a query and
|
|
587
|
+
* crash partway through.
|
|
588
|
+
*/
|
|
589
|
+
describe("ConnectionController.getConnectionQueryData admission gate", () => {
|
|
590
|
+
afterEach(() => {
|
|
591
|
+
sinon.restore();
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
it("re-throws the ServiceUnavailableError without ever calling the connector", async () => {
|
|
595
|
+
const runSQL = sinon.stub().resolves({ rows: [], totalRows: 0 });
|
|
596
|
+
const { ServiceUnavailableError } = await import("../errors");
|
|
597
|
+
const assertCanAdmitQuery = sinon
|
|
598
|
+
.stub()
|
|
599
|
+
.throws(
|
|
600
|
+
new ServiceUnavailableError(
|
|
601
|
+
'Publisher is under memory pressure and cannot accept new queries (environment "env").',
|
|
602
|
+
),
|
|
603
|
+
);
|
|
604
|
+
const { controller, runSQL: capturedRunSQL } = buildController(runSQL, {
|
|
605
|
+
assertCanAdmitQuery,
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
await expect(
|
|
609
|
+
controller.getConnectionQueryData("env", "conn", "SELECT 1", ""),
|
|
610
|
+
).rejects.toBeInstanceOf(ServiceUnavailableError);
|
|
611
|
+
|
|
612
|
+
// The gate must fire *before* the connector — no SQL should ever
|
|
613
|
+
// have been issued. This is the load-shedding invariant.
|
|
614
|
+
expect(assertCanAdmitQuery.called).toBe(true);
|
|
615
|
+
expect(capturedRunSQL.called).toBe(false);
|
|
616
|
+
});
|
|
617
|
+
|
|
618
|
+
it("proceeds normally when the gate is a no-op", async () => {
|
|
619
|
+
const runSQL = sinon.stub().resolves({ rows: [{ a: 1 }], totalRows: 1 });
|
|
620
|
+
const { controller, assertCanAdmitQuery } = buildController(runSQL);
|
|
621
|
+
|
|
622
|
+
await controller.getConnectionQueryData("env", "conn", "SELECT 1", "");
|
|
623
|
+
|
|
624
|
+
expect(assertCanAdmitQuery.called).toBe(true);
|
|
625
|
+
expect(runSQL.called).toBe(true);
|
|
626
|
+
});
|
|
627
|
+
});
|
|
628
|
+
|
|
629
|
+
describe("ConnectionController.getConnectionQueryData query timeout", () => {
|
|
630
|
+
const originalTimeout = process.env.PUBLISHER_QUERY_TIMEOUT_MS;
|
|
631
|
+
|
|
632
|
+
afterEach(() => {
|
|
633
|
+
sinon.restore();
|
|
634
|
+
if (originalTimeout === undefined) {
|
|
635
|
+
delete process.env.PUBLISHER_QUERY_TIMEOUT_MS;
|
|
636
|
+
} else {
|
|
637
|
+
process.env.PUBLISHER_QUERY_TIMEOUT_MS = originalTimeout;
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
it("surfaces QueryTimeoutError when the driver outlasts PUBLISHER_QUERY_TIMEOUT_MS, with the publisher's AbortSignal delivered to runSQL", async () => {
|
|
642
|
+
process.env.PUBLISHER_QUERY_TIMEOUT_MS = "20";
|
|
643
|
+
// The driver "hangs" until the publisher's abort signal
|
|
644
|
+
// fires; on abort it rejects with a driver-shaped error. The
|
|
645
|
+
// controller's runWithQueryTimeout wrapper converts that into
|
|
646
|
+
// QueryTimeoutError (mapped to HTTP 504 by `errors.ts`).
|
|
647
|
+
let observedSignal: AbortSignal | undefined;
|
|
648
|
+
const runSQL = sinon.stub().callsFake(
|
|
649
|
+
(_sql: string, opts: RunSQLOptions) =>
|
|
650
|
+
new Promise((_resolve, reject) => {
|
|
651
|
+
observedSignal = opts.abortSignal;
|
|
652
|
+
opts.abortSignal?.addEventListener("abort", () =>
|
|
653
|
+
reject(new Error("driver aborted")),
|
|
654
|
+
);
|
|
655
|
+
}),
|
|
656
|
+
);
|
|
657
|
+
const { controller } = buildController(runSQL);
|
|
658
|
+
|
|
659
|
+
const { QueryTimeoutError } = await import("../errors");
|
|
660
|
+
await expect(
|
|
661
|
+
controller.getConnectionQueryData("env", "conn", "SELECT 1", ""),
|
|
662
|
+
).rejects.toBeInstanceOf(QueryTimeoutError);
|
|
663
|
+
|
|
664
|
+
// Critical invariant: the abort signal MUST actually fire
|
|
665
|
+
// — without this the driver call would leak past the 504
|
|
666
|
+
// response.
|
|
667
|
+
expect(observedSignal).toBeDefined();
|
|
668
|
+
expect(observedSignal?.aborted).toBe(true);
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* Mirrors `buildController` but injects a `manifestTemporaryTable`
|
|
674
|
+
* stub instead of `runSQL`. `getConnectionTemporaryTable` casts the
|
|
675
|
+
* Malloy connection to `PersistSQLResults`, so the fake connection
|
|
676
|
+
* needs that method on its surface for the test to exercise the real
|
|
677
|
+
* code path through admission and the timeout wrapper.
|
|
678
|
+
*/
|
|
679
|
+
function buildTemporaryTableController(
|
|
680
|
+
manifestTemporaryTable: sinon.SinonStub,
|
|
681
|
+
opts: { assertCanAdmitQuery?: sinon.SinonStub } = {},
|
|
682
|
+
): {
|
|
683
|
+
controller: ConnectionController;
|
|
684
|
+
manifestTemporaryTable: sinon.SinonStub;
|
|
685
|
+
assertCanAdmitQuery: sinon.SinonStub;
|
|
686
|
+
} {
|
|
687
|
+
const fakeConnection = { manifestTemporaryTable } as unknown as Connection;
|
|
688
|
+
const assertCanAdmitQuery =
|
|
689
|
+
opts.assertCanAdmitQuery ?? sinon.stub().returns(undefined);
|
|
690
|
+
const fakeEnv = { assertCanAdmitQuery };
|
|
691
|
+
const fakeStore = {
|
|
692
|
+
getEnvironment: sinon.stub().resolves(fakeEnv),
|
|
693
|
+
} as unknown as EnvironmentStore;
|
|
694
|
+
const controller = new ConnectionController(fakeStore);
|
|
695
|
+
sinon
|
|
696
|
+
.stub(
|
|
697
|
+
controller as unknown as {
|
|
698
|
+
getMalloyConnection: (...args: unknown[]) => Promise<Connection>;
|
|
699
|
+
},
|
|
700
|
+
"getMalloyConnection",
|
|
701
|
+
)
|
|
702
|
+
.resolves(fakeConnection);
|
|
703
|
+
return { controller, manifestTemporaryTable, assertCanAdmitQuery };
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* `getConnectionTemporaryTable` issues a real `CREATE TEMPORARY TABLE
|
|
708
|
+
* AS (<sql>)` against the connector via `manifestTemporaryTable`, so
|
|
709
|
+
* the same three OOM guards as `getConnectionQueryData` must apply:
|
|
710
|
+
* sqlStatement shape, admission, and wall-clock timeout. The
|
|
711
|
+
* per-pod concurrency cap is wired at the route layer in `server.ts`
|
|
712
|
+
* / `server-old.ts` and exercised by `oom_guards.integration.spec.ts`.
|
|
713
|
+
*/
|
|
714
|
+
describe("ConnectionController.getConnectionTemporaryTable guards", () => {
|
|
715
|
+
const originalTimeout = process.env.PUBLISHER_QUERY_TIMEOUT_MS;
|
|
716
|
+
|
|
717
|
+
afterEach(() => {
|
|
718
|
+
sinon.restore();
|
|
719
|
+
if (originalTimeout === undefined) {
|
|
720
|
+
delete process.env.PUBLISHER_QUERY_TIMEOUT_MS;
|
|
721
|
+
} else {
|
|
722
|
+
process.env.PUBLISHER_QUERY_TIMEOUT_MS = originalTimeout;
|
|
723
|
+
}
|
|
724
|
+
});
|
|
725
|
+
|
|
726
|
+
it("rejects non-string sqlStatement at the boundary (CodeQL type guard)", async () => {
|
|
727
|
+
const manifestTemporaryTable = sinon.stub().resolves("temp_table_name");
|
|
728
|
+
const { controller } = buildTemporaryTableController(
|
|
729
|
+
manifestTemporaryTable,
|
|
730
|
+
);
|
|
731
|
+
|
|
732
|
+
await expect(
|
|
733
|
+
controller.getConnectionTemporaryTable("env", "conn", [
|
|
734
|
+
"SELECT 1",
|
|
735
|
+
"SELECT 2",
|
|
736
|
+
] as unknown as string),
|
|
737
|
+
).rejects.toBeInstanceOf(BadRequestError);
|
|
738
|
+
|
|
739
|
+
expect(manifestTemporaryTable.called).toBe(false);
|
|
740
|
+
});
|
|
741
|
+
|
|
742
|
+
it("re-throws ServiceUnavailableError under back-pressure without touching the connector", async () => {
|
|
743
|
+
const manifestTemporaryTable = sinon.stub().resolves("temp_table_name");
|
|
744
|
+
const { ServiceUnavailableError } = await import("../errors");
|
|
745
|
+
const assertCanAdmitQuery = sinon
|
|
746
|
+
.stub()
|
|
747
|
+
.throws(
|
|
748
|
+
new ServiceUnavailableError(
|
|
749
|
+
'Publisher is under memory pressure and cannot accept new queries (environment "env").',
|
|
750
|
+
),
|
|
751
|
+
);
|
|
752
|
+
const { controller } = buildTemporaryTableController(
|
|
753
|
+
manifestTemporaryTable,
|
|
754
|
+
{ assertCanAdmitQuery },
|
|
755
|
+
);
|
|
756
|
+
|
|
757
|
+
await expect(
|
|
758
|
+
controller.getConnectionTemporaryTable("env", "conn", "SELECT 1"),
|
|
759
|
+
).rejects.toBeInstanceOf(ServiceUnavailableError);
|
|
760
|
+
|
|
761
|
+
// Load-shedding invariant: the gate must fire BEFORE any
|
|
762
|
+
// connector work. `CREATE TEMPORARY TABLE` is a real DDL —
|
|
763
|
+
// we must not start it under memory pressure.
|
|
764
|
+
expect(assertCanAdmitQuery.called).toBe(true);
|
|
765
|
+
expect(manifestTemporaryTable.called).toBe(false);
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
it("happy path: admission passes through and the connector is called", async () => {
|
|
769
|
+
const manifestTemporaryTable = sinon.stub().resolves("temp_table_name");
|
|
770
|
+
const { controller, assertCanAdmitQuery } = buildTemporaryTableController(
|
|
771
|
+
manifestTemporaryTable,
|
|
772
|
+
);
|
|
773
|
+
|
|
774
|
+
const result = await controller.getConnectionTemporaryTable(
|
|
775
|
+
"env",
|
|
776
|
+
"conn",
|
|
777
|
+
"SELECT 1",
|
|
778
|
+
);
|
|
779
|
+
|
|
780
|
+
expect(assertCanAdmitQuery.called).toBe(true);
|
|
781
|
+
expect(manifestTemporaryTable.calledOnceWith("SELECT 1")).toBe(true);
|
|
782
|
+
expect(JSON.parse(result.table as string)).toBe("temp_table_name");
|
|
783
|
+
});
|
|
784
|
+
|
|
785
|
+
it("surfaces QueryTimeoutError when manifestTemporaryTable outlasts PUBLISHER_QUERY_TIMEOUT_MS", async () => {
|
|
786
|
+
process.env.PUBLISHER_QUERY_TIMEOUT_MS = "20";
|
|
787
|
+
// `manifestTemporaryTable` does not accept an abortSignal, so
|
|
788
|
+
// the timeout is a pure wall-clock guard — the DDL keeps
|
|
789
|
+
// running inside the DB but the HTTP response unblocks as
|
|
790
|
+
// QueryTimeoutError → 504, releasing the slot.
|
|
791
|
+
const manifestTemporaryTable = sinon
|
|
792
|
+
.stub()
|
|
793
|
+
.callsFake(() => new Promise(() => undefined /* never resolves */));
|
|
794
|
+
const { controller } = buildTemporaryTableController(
|
|
795
|
+
manifestTemporaryTable,
|
|
796
|
+
);
|
|
797
|
+
|
|
798
|
+
const { QueryTimeoutError } = await import("../errors");
|
|
799
|
+
await expect(
|
|
800
|
+
controller.getConnectionTemporaryTable("env", "conn", "SELECT 1"),
|
|
801
|
+
).rejects.toBeInstanceOf(QueryTimeoutError);
|
|
802
|
+
});
|
|
803
|
+
});
|