| 50 | * `begin`, so a test can make either take time. |
| 51 | */ |
| 52 | const makeHarness = (options: { |
| 53 | readonly spanAttributes?: ReadonlyArray<readonly [string, unknown]> | undefined |
| 54 | readonly acquire?: Effect.Effect<void> | undefined |
| 55 | readonly duringBegin?: Effect.Effect<void> | undefined |
| 56 | readonly duringRollbackSavepoint?: Effect.Effect<void> | undefined |
| 57 | readonly begin?: SqlError.SqlError | undefined |
| 58 | readonly commit?: (() => Effect.Effect<void, SqlError.SqlError>) | undefined |
| 59 | readonly onCommitFailure?: (() => Effect.Effect<void, SqlError.SqlError>) | undefined |
| 60 | readonly savepoint?: SqlError.SqlError | undefined |
| 61 | readonly rollbackSavepoint?: SqlError.SqlError | undefined |
| 62 | readonly releaseSavepoint?: true | SqlError.SqlError | undefined |
| 63 | } = {}) => { |
| 64 | const release = options.releaseSavepoint |
| 65 | const onCommitFailure = options.onCommitFailure |
| 66 | const calls: Array<string> = [] |
| 67 | const conn: StubConnection = { id: "stub" } |
| 68 | const transactionService = Context.Service< |
| 69 | StubTransactionConnection, |
| 70 | readonly [conn: StubConnection, counter: number] |
| 71 | >(`test/SqlClient/TransactionConnection/${harnessIdCounter++}`) |
| 72 | |
| 73 | let transactionActive = false |
| 74 | const savepoints: Array<number> = [] |
| 75 | |
| 76 | const record = (name: string) => Effect.sync(() => calls.push(name)) |
| 77 | |
| 78 | const withTransaction = SqlClient.makeWithTransaction({ |
| 79 | transactionService, |
| 80 | spanAttributes: options.spanAttributes ?? [], |
| 81 | acquireConnection: () => |
| 82 | Effect.as( |
| 83 | Effect.flatMap( |
| 84 | Effect.andThen(record("acquireConnection"), options.acquire ?? Effect.void), |
| 85 | () => Effect.addFinalizer(() => record("closeConnection")) |
| 86 | ), |
| 87 | conn |
| 88 | ), |
| 89 | begin: (_conn, statement) => |
| 90 | Effect.flatMap(record(statement === undefined ? "begin" : `begin(${statement})`), () => { |
| 91 | if (options.begin !== undefined) { |
| 92 | return Effect.fail(options.begin) |
| 93 | } |
| 94 | transactionActive = true |
| 95 | return options.duringBegin ?? Effect.void |
| 96 | }), |
| 97 | savepoint: (_conn, id) => |
| 98 | Effect.flatMap(record(`savepoint(${id})`), () => { |
| 99 | if (options.savepoint !== undefined) { |
| 100 | return Effect.fail(options.savepoint) |
| 101 | } |
| 102 | savepoints.push(id) |
| 103 | return Effect.void |
| 104 | }), |
| 105 | commit: () => |
| 106 | Effect.flatMap(record("commit"), () => |
| 107 | options.commit !== undefined |
| 108 | ? options.commit() |
| 109 | : transactionActive |