Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Apr 4, 2024
1 parent 9c423b4 commit bfbb016
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 86 deletions.
29 changes: 13 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import path from "node:path"
import type {
ConnectionDetailsFromWorker,
InitialWorkerData,
SharedWorkerFunctions,
TestWorkerFunctions,
} from "./internal-types"
import type {
ConnectionDetails,
Expand All @@ -16,7 +18,6 @@ import type { Jsonifiable } from "type-fest"
import type { ExecutionContext } from "ava"
import { once } from "node:events"
import { createBirpc } from "birpc"
import { SharedWorkerFunctions, TestWorkerFunctions } from "./lib/rpc"
import { ExecResult } from "testcontainers"
import isPlainObject from "lodash/isPlainObject"

Expand Down Expand Up @@ -187,23 +188,19 @@ export const getTestPostgresDatabaseFactory = <
}
)

// todo: properly tear down?
const messageHandlerAbortController = new AbortController()
const messageHandlerPromise = Promise.race([
once(messageHandlerAbortController.signal, "abort"),
(async () => {
const worker = await workerPromise
await worker.available
// Automatically cleaned up by AVA since each test file runs in a separate worker
const _messageHandlerPromise = (async () => {
const worker = await workerPromise
await worker.available

for await (const msg of worker.subscribe()) {
rpcCallback!(msg.data)

if (messageHandlerAbortController.signal.aborted) {
break
}
for await (const msg of worker.subscribe()) {
if (msg.data.type === "teardown") {
console.log("breaking")
break
}
})(),
])
rpcCallback!(msg.data)
}
})()

const getTestPostgresDatabase: GetTestPostgresDatabase<Params> = async (
t: ExecutionContext,
Expand Down
63 changes: 16 additions & 47 deletions src/internal-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ExecResult, StartedNetwork } from "testcontainers"
import type { ExecResult } from "testcontainers"
import type { Jsonifiable } from "type-fest"

import type {
ConnectionDetails,
GetTestPostgresDatabaseFactoryOptions,
Expand All @@ -13,52 +14,20 @@ export interface InitialWorkerData {

export type ConnectionDetailsFromWorker = Omit<ConnectionDetails, "pool">

export interface RequestDatabaseFromWorkerMessage {
type: "GET_TEST_DATABASE"
key?: string
params?: Jsonifiable
}

export interface RequestBeforeTemplateIsBakedHookToBeRunMessage {
type: "RUN_HOOK_BEFORE_TEMPLATE_IS_BAKED"
connectionDetails: ConnectionDetailsFromWorker
}

export interface FinishedRunningBeforeTemplateIsBakedHookMessage {
type: "FINISHED_RUNNING_HOOK_BEFORE_TEMPLATE_IS_BAKED"
result:
| {
status: "success"
result: any
}
| {
status: "error"
error: Error | string
}
export interface TestWorkerFunctions {
runBeforeTemplateIsBakedHook: (
connectionDetails: ConnectionDetailsFromWorker,
params?: Jsonifiable
) => Promise<unknown>
}

export interface ExecCommandInContainerMessage {
type: "EXEC_COMMAND_IN_CONTAINER"
command: string[]
export interface SharedWorkerFunctions {
getTestDatabase: (options: {
key?: string
params?: Jsonifiable
}) => Promise<{
connectionDetails: ConnectionDetailsFromWorker
beforeTemplateIsBakedResult: unknown
}>
execCommandInContainer: (command: string[]) => Promise<ExecResult>
}

export interface ExecCommandInContainerResultMessage {
type: "EXEC_COMMAND_IN_CONTAINER_RESULT"
result: ExecResult
}

export interface GotDatabaseMessage {
type: "GOT_DATABASE"
connectionDetails: ConnectionDetailsFromWorker
beforeTemplateIsBakedResult: FinishedRunningBeforeTemplateIsBakedHookMessage["result"]
}

export type MessageToWorker =
| RequestDatabaseFromWorkerMessage
| FinishedRunningBeforeTemplateIsBakedHookMessage
| ExecCommandInContainerMessage
export type MessageFromWorker =
| RequestBeforeTemplateIsBakedHookToBeRunMessage
| GotDatabaseMessage
| ExecCommandInContainerResultMessage
export type WorkerMessage = MessageToWorker | MessageFromWorker
21 changes: 0 additions & 21 deletions src/lib/rpc.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import pg from "pg"
import { GenericContainer, Network } from "testcontainers"
import { Mutex } from "async-mutex"
import hash from "object-hash"
import type { InitialWorkerData } from "./internal-types"
import type {
InitialWorkerData,
SharedWorkerFunctions,
TestWorkerFunctions,
} from "./internal-types"
import getRandomDatabaseName from "./lib/get-random-database-name"
import type { SharedWorker } from "ava/plugin"
import { type BirpcReturn, type ChannelOptions, createBirpc } from "birpc"
import { once } from "node:events"
import type { SharedWorkerFunctions, TestWorkerFunctions } from "./lib/rpc"
import type { Jsonifiable } from "type-fest"

type WorkerRpc = BirpcReturn<TestWorkerFunctions, SharedWorkerFunctions>
Expand Down Expand Up @@ -54,6 +57,7 @@ export class Worker {
testWorker.teardown(async () => {
messageHandlerAbortController.abort()
await messageHandlerPromise
testWorker.publish({ type: "teardown" })
})

const rpc: WorkerRpc = createBirpc<
Expand Down

0 comments on commit bfbb016

Please sign in to comment.