Skip to content

Commit ddc3c41

Browse files
committed
test(server): fix the typecheck errors the project's own CI found
The server typecheck was never actually run locally: `vp run --filter @t3tools/server typecheck` matches no package, so it exits 0 having done nothing. The package is `t3`, and `tsc --noEmit` in apps/server reports twelve errors in the two new test files, which the project's CI caught and this commit fixes. - `ModelSelection` was used but never imported. - `withWriteLockHeld` declared neither the `uncommittedRows` it returns nor the `SqlError` its queries can fail with. - The span attribute is read as `{} | null`, so it needs narrowing before it can be used as a connection name. - `console.log` inside Effect code is rejected by the effect language service, and `Effect.log` is swallowed by the test runner, so the measurement is printed through a suppressed `console.log` instead. - `Fiber.join` propagates the writer's failure rather than returning it, so a row that did not insert now fails the test instead of passing as write load. The writer also releases the reader if the write fails, instead of leaving it to time out. - The two layer merges that provided `NodeServices.layer` alongside other layers raised a warning and made `tsc` exit non-zero with no errors at all. They now provide it with `Layer.provideMerge`, the way the rest of the repository does. `tsc --noEmit` in apps/server now exits 0 with no errors and no warnings, and the seven tests in the two files pass.
1 parent 095dece commit ddc3c41

2 files changed

Lines changed: 50 additions & 19 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStoreLockWait.test.ts‎

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { assert, it } from "@effect/vitest";
2-
import { EventId, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
2+
import {
3+
EventId,
4+
ProjectId,
5+
ProviderInstanceId,
6+
ThreadId,
7+
type ModelSelection,
8+
} from "@t3tools/contracts";
39
import * as Cause from "effect/Cause";
410
import * as Clock from "effect/Clock";
511
import * as DateTime from "effect/DateTime";
612
import * as Deferred from "effect/Deferred";
713
import * as Effect from "effect/Effect";
814
import * as Exit from "effect/Exit";
9-
import * as Result from "effect/Result";
1015
import * as Fiber from "effect/Fiber";
1116
import * as FileSystem from "effect/FileSystem";
1217
import * as Layer from "effect/Layer";
18+
import * as Option from "effect/Option";
1319
import * as Path from "effect/Path";
1420
import * as SqlClient from "effect/sql/SqlClient";
21+
import type { SqlError } from "effect/sql/SqlError";
1522
import * as Statement from "effect/sql/Statement";
1623
import * as Tracer from "effect/Tracer";
1724
import * as TestClock from "effect/testing/TestClock";
@@ -39,6 +46,19 @@ const FIXED_EPOCH = "2026-01-01T00:00:00.000Z";
3946
/** How long a blocked read is given before it is called blocked. */
4047
const BLOCKED_TIMEOUT = "3 seconds";
4148

49+
/**
50+
* Print a measurement to the test output.
51+
*
52+
* `Effect.log` on its own is swallowed by the test runner, and the effect
53+
* language service rejects `console.log` inside Effect code. The measurement is
54+
* the point of the test, so it has to be readable in the output.
55+
*/
56+
const printMeasurement = (message: string) =>
57+
Effect.sync(() => {
58+
// @effect-diagnostics-next-line globalConsoleInEffect:off - the measurement is the test's output, and the test runner captures the console.
59+
console.log(message);
60+
});
61+
4262
/**
4363
* The scope the writer's row is written under.
4464
*
@@ -64,7 +84,11 @@ const HELD_WRITE_SCOPE = "lockwait-held-write";
6484
*/
6585
const withWriteLockHeld = <A, E, R>(
6686
effect: Effect.Effect<A, E, R>,
67-
): Effect.Effect<{ readonly value: A; readonly heldForMs: number }, E, R | SqlClient.SqlClient> =>
87+
): Effect.Effect<
88+
{ readonly value: A; readonly heldForMs: number; readonly uncommittedRows: number },
89+
E | SqlError,
90+
R | SqlClient.SqlClient
91+
> =>
6892
Effect.gen(function* () {
6993
const sql = yield* SqlClient.SqlClient;
7094
const lockHeld = yield* Deferred.make<void>();
@@ -73,19 +97,26 @@ const withWriteLockHeld = <A, E, R>(
7397
.withTransaction(
7498
Effect.gen(function* () {
7599
// A real write, with the table's own columns. A row that does not
76-
// insert is not write load.
100+
// insert is not write load. The id is fixed because the database is
101+
// created for this test alone, so it is unique in it.
77102
yield* sql`INSERT INTO orchestration_v2_projection_checkpoints (
78103
checkpoint_id, thread_id, scope_id, run_id, node_id, ordinal_within_scope,
79104
status, captured_at, payload_json
80105
) VALUES (
81-
${crypto.randomUUID()}, ${"lockwait-thread"}, ${HELD_WRITE_SCOPE}, ${"run"}, ${"node"},
106+
${"lockwait-checkpoint"}, ${"lockwait-thread"}, ${HELD_WRITE_SCOPE}, ${"run"}, ${"node"},
82107
1, 'captured', ${FIXED_EPOCH}, '{}'
83108
)`;
84109
yield* Deferred.succeed(lockHeld, undefined);
85110
yield* Deferred.await(release);
86111
}),
87112
)
88-
.pipe(Effect.forkChild({ startImmediately: true }));
113+
.pipe(
114+
// If the write fails, the lock is never held and the read would be
115+
// measuring nothing. Release the reader instead of leaving it to time
116+
// out, and let the writer's own exit report the failure below.
117+
Effect.tapError(() => Deferred.succeed(lockHeld, undefined)),
118+
Effect.forkChild({ startImmediately: true }),
119+
);
89120
yield* Deferred.await(lockHeld);
90121
// The write lock is held from here until the effect finishes.
91122
const started = yield* Clock.currentTimeMillis;
@@ -100,6 +131,8 @@ const withWriteLockHeld = <A, E, R>(
100131
WHERE scope_id = ${HELD_WRITE_SCOPE}
101132
`.pipe(NodeSqliteClient.readOnly);
102133
yield* Deferred.succeed(release, undefined);
134+
// Joining the writer propagates its failure, so a row that did not insert
135+
// fails the test rather than passing as write load.
103136
yield* Fiber.join(writer);
104137
return { value, heldForMs, uncommittedRows: uncommittedRows[0]?.count ?? -1 };
105138
});
@@ -157,7 +190,7 @@ const measureReadUnderWriteLock = Effect.fn("measureReadUnderWriteLock")(functio
157190
span.end = (endTime, exit) => {
158191
end(endTime, exit);
159192
const connection = span.attributes.get("db.connection");
160-
if (connection !== undefined) {
193+
if (typeof connection === "string") {
161194
acquired.push({
162195
connection,
163196
lockWaitMs: Number(span.attributes.get("db.lock_wait_ms") ?? 0),
@@ -204,7 +237,7 @@ const measureReadOnWriterConnection = Effect.fn("measureReadOnWriterConnection")
204237
span.end = (endTime, exit) => {
205238
end(endTime, exit);
206239
const connection = span.attributes.get("db.connection");
207-
if (connection !== undefined) {
240+
if (typeof connection === "string") {
208241
acquired.push({
209242
connection,
210243
lockWaitMs: Number(span.attributes.get("db.lock_wait_ms") ?? 0),
@@ -248,8 +281,7 @@ it.effect("the projection read completes while a writer holds the write lock", (
248281
const storeLayer = Layer.mergeAll(
249282
ProjectionStore.layer.pipe(Layer.provideMerge(database)),
250283
database,
251-
NodeServices.layer,
252-
);
284+
).pipe(Layer.provideMerge(NodeServices.layer));
253285

254286
const threadId = ThreadId.make("thread:lockwait:reader");
255287
const { acquired, heldForMs, uncommittedRows } = yield* Effect.gen(function* () {
@@ -276,7 +308,7 @@ it.effect("the projection read completes while a writer holds the write lock", (
276308
// uncommitted, and so invisible to the reader, when the read finished.
277309
assert.isAtLeast(heldForMs, 0);
278310
assert.equal(uncommittedRows, 0);
279-
console.log(
311+
yield* printMeasurement(
280312
`MEASURE read-on-reader: connection=${acquired[0]?.connection} lock_wait_ms=${acquired[0]?.lockWaitMs} lock_held_for_ms=${heldForMs} uncommitted_rows_visible=${uncommittedRows}`,
281313
);
282314
}).pipe(Effect.provide(NodeServices.layer)),
@@ -291,8 +323,7 @@ it.effect("the same read on the writer connection is blocked by the write lock",
291323
const storeLayer = Layer.mergeAll(
292324
ProjectionStore.layer.pipe(Layer.provideMerge(database)),
293325
database,
294-
NodeServices.layer,
295-
);
326+
).pipe(Layer.provideMerge(NodeServices.layer));
296327

297328
const threadId = ThreadId.make("thread:lockwait:writer");
298329
const { acquired, heldForMs, value } = yield* Effect.gen(function* () {
@@ -309,14 +340,14 @@ it.effect("the same read on the writer connection is blocked by the write lock",
309340
assert.equal(exit._tag, "Failure");
310341
// A read that failed for some other reason would not have waited for the
311342
// bound, so the failure has to BE the timeout.
312-
const found = Cause.findError(exit.cause);
313-
assert.isTrue(Result.isSuccess(found), "expected a typed failure, got " + String(found));
314-
const error = found.success;
343+
const error = Exit.isFailure(exit)
344+
? Cause.findErrorOption(exit.cause).pipe(Option.getOrElse(() => undefined))
345+
: undefined;
315346
assert.isTrue(Cause.isTimeoutError(error), "expected a timeout, got " + String(error));
316347
// It waited for the bound, with the lock held the whole time.
317348
assert.isAtLeast(heldForMs, 2_000);
318349
assert.deepEqual(acquired, []);
319-
console.log(
350+
yield* printMeasurement(
320351
`MEASURE read-on-writer: outcome=TimeoutError lock_held_for_ms=${heldForMs} connections_acquired=${acquired.length}`,
321352
);
322353
}).pipe(Effect.provide(NodeServices.layer)),

‎apps/server/src/orchestration-v2/ProjectionStoreReaderConnection.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const projectId = ProjectId.make("project:reader");
3434
* under a tracer for the attribute to exist at all.
3535
*/
3636
const makeConnectionRecorder = () => {
37-
const connections: Array<string | undefined> = [];
37+
const connections: Array<string> = [];
3838
const tracer = Tracer.make({
3939
span: (options) => {
4040
const span = new Tracer.NativeSpan(options);
@@ -44,7 +44,7 @@ const makeConnectionRecorder = () => {
4444
const connection = span.attributes.get("db.connection");
4545
// Only the spans that acquired a connection carry the attribute; the
4646
// statement spans inside a transaction do not.
47-
if (connection !== undefined) connections.push(connection);
47+
if (typeof connection === "string") connections.push(connection);
4848
};
4949
return span;
5050
},

0 commit comments

Comments
 (0)