|
1 | 1 | import { describe, expect, it } from "@effect/vitest"; |
2 | 2 | import type { |
3 | 3 | ApplicationStoredEvent, |
| 4 | + OrchestrationProjectShell, |
4 | 5 | OrchestrationV2ShellSnapshot, |
5 | 6 | OrchestrationV2ShellStreamItem, |
6 | 7 | OrchestrationV2StoredEvent, |
7 | 8 | OrchestrationV2ThreadShell, |
8 | 9 | } from "@t3tools/contracts"; |
9 | 10 | import { ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; |
| 11 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
10 | 12 | import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
11 | 13 | import * as DateTime from "effect/DateTime"; |
12 | 14 | import * as Duration from "effect/Duration"; |
13 | 15 | import * as Effect from "effect/Effect"; |
| 16 | +import * as Fiber from "effect/Fiber"; |
| 17 | +import * as FileSystem from "effect/FileSystem"; |
| 18 | +import * as Deferred from "effect/Deferred"; |
14 | 19 | import * as Option from "effect/Option"; |
| 20 | +import * as Path from "effect/Path"; |
15 | 21 | import * as SqlClient from "effect/sql/SqlClient"; |
16 | 22 | import * as Stream from "effect/Stream"; |
17 | 23 | import * as TestClock from "effect/testing/TestClock"; |
@@ -144,6 +150,73 @@ describe("loadShellSnapshotParts", () => { |
144 | 150 | expect(snapshot.threads.threads).toEqual([thread]); |
145 | 151 | }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), |
146 | 152 | ); |
| 153 | + |
| 154 | + it.effect( |
| 155 | + "reads threads, projects and sequence from one reader snapshot while a write commits", |
| 156 | + () => |
| 157 | + Effect.gen(function* () { |
| 158 | + const fs = yield* FileSystem.FileSystem; |
| 159 | + const path = yield* Path.Path; |
| 160 | + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-shell-snapshot-" }); |
| 161 | + const filename = path.join(directory, "state.sqlite"); |
| 162 | + yield* Effect.gen(function* () { |
| 163 | + const sql = yield* SqlClient.SqlClient; |
| 164 | + yield* sql`PRAGMA journal_mode = WAL`; |
| 165 | + yield* sql`CREATE TABLE threads(id TEXT NOT NULL)`; |
| 166 | + yield* sql`CREATE TABLE projects(id TEXT NOT NULL)`; |
| 167 | + yield* sql`CREATE TABLE events(sequence INTEGER NOT NULL)`; |
| 168 | + yield* sql`INSERT INTO threads VALUES ('thread-a')`; |
| 169 | + yield* sql`INSERT INTO projects VALUES ('project-a')`; |
| 170 | + yield* sql`INSERT INTO events VALUES (1)`; |
| 171 | + |
| 172 | + const threadsRead = yield* Deferred.make<void>(); |
| 173 | + const write = yield* Effect.forkChild( |
| 174 | + Deferred.await(threadsRead).pipe( |
| 175 | + Effect.andThen( |
| 176 | + sql.withTransaction( |
| 177 | + Effect.all([ |
| 178 | + sql`INSERT INTO threads VALUES ('thread-b')`, |
| 179 | + sql`INSERT INTO projects VALUES ('project-b')`, |
| 180 | + sql`INSERT INTO events VALUES (2)`, |
| 181 | + ]), |
| 182 | + ), |
| 183 | + ), |
| 184 | + ), |
| 185 | + ); |
| 186 | + |
| 187 | + const snapshot = yield* loadShellSnapshotParts({ |
| 188 | + sql, |
| 189 | + readThreads: Effect.gen(function* () { |
| 190 | + const rows = yield* sql<{ id: string }>`SELECT id FROM threads ORDER BY id`; |
| 191 | + yield* Deferred.succeed(threadsRead, undefined); |
| 192 | + // The write commits before the rest of the snapshot is read. Holding |
| 193 | + // the writer here would deadlock this join. |
| 194 | + yield* Fiber.join(write); |
| 195 | + return Effect.succeed({ |
| 196 | + schemaVersion: 1 as const, |
| 197 | + snapshotSequence: 0, |
| 198 | + threads: rows.map((row) => shellFixture({ id: ThreadId.make(row.id) })), |
| 199 | + archivedThreads: [], |
| 200 | + }); |
| 201 | + }), |
| 202 | + listProjects: Effect.map( |
| 203 | + sql<{ id: string }>`SELECT id FROM projects ORDER BY id`, |
| 204 | + (rows) => rows.map((row) => ({ id: row.id }) as OrchestrationProjectShell), |
| 205 | + ), |
| 206 | + latestSequence: Effect.map( |
| 207 | + sql<{ sequence: number }>`SELECT MAX(sequence) AS sequence FROM events`, |
| 208 | + (rows) => rows[0]?.sequence ?? 0, |
| 209 | + ), |
| 210 | + }); |
| 211 | + |
| 212 | + expect({ |
| 213 | + threads: snapshot.threads.threads.map((thread) => thread.id), |
| 214 | + projects: snapshot.projects.map((project) => project.id), |
| 215 | + snapshotSequence: snapshot.snapshotSequence, |
| 216 | + }).toEqual({ threads: ["thread-a"], projects: ["project-a"], snapshotSequence: 1 }); |
| 217 | + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename, readerWorker: true }))); |
| 218 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 219 | + ); |
147 | 220 | }); |
148 | 221 |
|
149 | 222 | function shellFixture(overrides: Partial<OrchestrationV2ThreadShell>): OrchestrationV2ThreadShell { |
|
0 commit comments