-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use birpc #28
Merged
Merged
Use birpc #28
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d748fd
Use birpc
codetheweb 943755f
Bump node version
codetheweb abdfbd3
Fix tests
codetheweb 9c423b4
Fix serialization error
codetheweb bfbb016
Cleanup
codetheweb 2b97eaf
Rename
codetheweb 3e31180
Cleanup
codetheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,57 @@ | ||
import { registerSharedWorker, SharedWorker } from "ava/plugin" | ||
import { registerSharedWorker } from "ava/plugin" | ||
import hash from "object-hash" | ||
import path from "node:path" | ||
import { | ||
import type { | ||
ConnectionDetailsFromWorker, | ||
FinishedRunningBeforeTemplateIsBakedHookMessage, | ||
InitialWorkerData, | ||
MessageFromWorker, | ||
MessageToWorker, | ||
SharedWorkerFunctions, | ||
TestWorkerFunctions, | ||
} from "./internal-types" | ||
import { | ||
import type { | ||
ConnectionDetails, | ||
GetTestPostgresDatabase, | ||
GetTestPostgresDatabaseFactoryOptions, | ||
GetTestPostgresDatabaseOptions, | ||
GetTestPostgresDatabaseResult, | ||
} from "./public-types" | ||
import { Pool } from "pg" | ||
import { Jsonifiable } from "type-fest" | ||
import { StartedNetwork } from "testcontainers" | ||
import { ExecutionContext } from "ava" | ||
import type { Jsonifiable } from "type-fest" | ||
import type { ExecutionContext } from "ava" | ||
import { once } from "node:events" | ||
import { createBirpc } from "birpc" | ||
import { ExecResult } from "testcontainers" | ||
import isPlainObject from "lodash/isPlainObject" | ||
|
||
// https://stackoverflow.com/a/30580513 | ||
const isSerializable = (obj: Record<any, any>): boolean => { | ||
var isNestedSerializable | ||
function isPlain(val: any) { | ||
return ( | ||
typeof val === "undefined" || | ||
typeof val === "string" || | ||
typeof val === "boolean" || | ||
typeof val === "number" || | ||
Array.isArray(val) || | ||
isPlainObject(val) | ||
) | ||
} | ||
if (!isPlain(obj)) { | ||
return false | ||
} | ||
for (var property in obj) { | ||
if (obj.hasOwnProperty(property)) { | ||
if (!isPlain(obj[property])) { | ||
return false | ||
} | ||
if (typeof obj[property] == "object") { | ||
isNestedSerializable = isSerializable(obj[property]) | ||
if (!isNestedSerializable) { | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
} | ||
|
||
const getWorker = async ( | ||
initialData: InitialWorkerData, | ||
|
@@ -50,6 +83,24 @@ const getWorker = async ( | |
}) | ||
} | ||
|
||
const teardownConnection = async ({ | ||
pool, | ||
pgbouncerPool, | ||
}: ConnectionDetails) => { | ||
try { | ||
await pool.end() | ||
await pgbouncerPool?.end() | ||
} catch (error) { | ||
if ( | ||
(error as Error).message.includes("Called end on pool more than once") | ||
) { | ||
return | ||
} | ||
|
||
throw error | ||
} | ||
} | ||
|
||
export const getTestPostgresDatabaseFactory = < | ||
Params extends Jsonifiable = never | ||
>( | ||
|
@@ -63,71 +114,34 @@ export const getTestPostgresDatabaseFactory = < | |
|
||
const workerPromise = getWorker(initialData, options as any) | ||
|
||
const getTestPostgresDatabase: GetTestPostgresDatabase<Params> = async ( | ||
t: ExecutionContext, | ||
params: any, | ||
getTestDatabaseOptions?: GetTestPostgresDatabaseOptions | ||
) => { | ||
const mapWorkerConnectionDetailsToConnectionDetails = ( | ||
connectionDetailsFromWorker: ConnectionDetailsFromWorker | ||
): ConnectionDetails => { | ||
const pool = new Pool({ | ||
connectionString: connectionDetailsFromWorker.connectionString, | ||
}) | ||
|
||
let pgbouncerPool: Pool | undefined | ||
if (connectionDetailsFromWorker.pgbouncerConnectionString) { | ||
pgbouncerPool = new Pool({ | ||
connectionString: | ||
connectionDetailsFromWorker.pgbouncerConnectionString, | ||
}) | ||
} | ||
|
||
t.teardown(async () => { | ||
try { | ||
await pool.end() | ||
await pgbouncerPool?.end() | ||
} catch (error) { | ||
if ( | ||
(error as Error).message.includes( | ||
"Called end on pool more than once" | ||
) | ||
) { | ||
return | ||
} | ||
const mapWorkerConnectionDetailsToConnectionDetails = ( | ||
connectionDetailsFromWorker: ConnectionDetailsFromWorker | ||
): ConnectionDetails => { | ||
const pool = new Pool({ | ||
connectionString: connectionDetailsFromWorker.connectionString, | ||
}) | ||
|
||
throw error | ||
} | ||
let pgbouncerPool: Pool | undefined | ||
if (connectionDetailsFromWorker.pgbouncerConnectionString) { | ||
pgbouncerPool = new Pool({ | ||
connectionString: connectionDetailsFromWorker.pgbouncerConnectionString, | ||
}) | ||
|
||
return { | ||
...connectionDetailsFromWorker, | ||
pool, | ||
pgbouncerPool, | ||
} | ||
} | ||
|
||
const worker = await workerPromise | ||
await worker.available | ||
|
||
const waitForAndHandleReply = async ( | ||
message: SharedWorker.Plugin.PublishedMessage | ||
): Promise<GetTestPostgresDatabaseResult> => { | ||
let reply = await message.replies().next() | ||
const replyData: MessageFromWorker = reply.value.data | ||
|
||
if (replyData.type === "RUN_HOOK_BEFORE_TEMPLATE_IS_BAKED") { | ||
let result: FinishedRunningBeforeTemplateIsBakedHookMessage["result"] = | ||
{ | ||
status: "success", | ||
result: undefined, | ||
} | ||
return { | ||
...connectionDetailsFromWorker, | ||
pool, | ||
pgbouncerPool, | ||
} | ||
} | ||
|
||
let rpcCallback: (data: any) => void | ||
const rpc = createBirpc<SharedWorkerFunctions, TestWorkerFunctions>( | ||
{ | ||
runBeforeTemplateIsBakedHook: async (connection, params) => { | ||
if (options?.beforeTemplateIsBaked) { | ||
const connectionDetails = | ||
mapWorkerConnectionDetailsToConnectionDetails( | ||
replyData.connectionDetails | ||
) | ||
mapWorkerConnectionDetailsToConnectionDetails(connection) | ||
|
||
// Ignore if the pool is terminated by the shared worker | ||
// (This happens in CI for some reason even though we drain the pool first.) | ||
|
@@ -143,94 +157,70 @@ export const getTestPostgresDatabaseFactory = < | |
throw error | ||
}) | ||
|
||
try { | ||
const hookResult = await options.beforeTemplateIsBaked({ | ||
params, | ||
connection: connectionDetails, | ||
containerExec: async (command) => { | ||
const request = reply.value.reply({ | ||
type: "EXEC_COMMAND_IN_CONTAINER", | ||
command, | ||
}) | ||
|
||
reply = await request.replies().next() | ||
|
||
if ( | ||
reply.value.data.type !== "EXEC_COMMAND_IN_CONTAINER_RESULT" | ||
) { | ||
throw new Error( | ||
"Expected EXEC_COMMAND_IN_CONTAINER_RESULT message" | ||
) | ||
} | ||
|
||
return reply.value.data.result | ||
}, | ||
}) | ||
|
||
result = { | ||
status: "success", | ||
result: hookResult, | ||
} | ||
} catch (error) { | ||
result = { | ||
status: "error", | ||
error: | ||
error instanceof Error | ||
? error.stack ?? error.message | ||
: new Error( | ||
"Unknown error type thrown in beforeTemplateIsBaked hook" | ||
), | ||
} | ||
} finally { | ||
// Otherwise connection will be killed by worker when converting to template | ||
await connectionDetails.pool.end() | ||
} | ||
} | ||
const hookResult = await options.beforeTemplateIsBaked({ | ||
params: params as any, | ||
connection: connectionDetails, | ||
containerExec: async (command): Promise<ExecResult> => | ||
rpc.execCommandInContainer(command), | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you weren't kidding this is definitely way simpler |
||
|
||
try { | ||
return waitForAndHandleReply( | ||
reply.value.reply({ | ||
type: "FINISHED_RUNNING_HOOK_BEFORE_TEMPLATE_IS_BAKED", | ||
result, | ||
} as MessageToWorker) | ||
) | ||
} catch (error) { | ||
if (error instanceof Error && error.name === "DataCloneError") { | ||
await teardownConnection(connectionDetails) | ||
|
||
if (hookResult && !isSerializable(hookResult)) { | ||
throw new TypeError( | ||
"Return value of beforeTemplateIsBaked() hook could not be serialized. Make sure it returns only JSON-serializable values." | ||
) | ||
} | ||
|
||
throw error | ||
} | ||
} else if (replyData.type === "GOT_DATABASE") { | ||
if (replyData.beforeTemplateIsBakedResult.status === "error") { | ||
if (typeof replyData.beforeTemplateIsBakedResult.error === "string") { | ||
throw new Error(replyData.beforeTemplateIsBakedResult.error) | ||
} | ||
|
||
throw replyData.beforeTemplateIsBakedResult.error | ||
return hookResult | ||
} | ||
}, | ||
}, | ||
{ | ||
post: async (data) => { | ||
const worker = await workerPromise | ||
await worker.available | ||
worker.publish(data) | ||
}, | ||
on: (data) => { | ||
rpcCallback = data | ||
}, | ||
} | ||
) | ||
|
||
return { | ||
...mapWorkerConnectionDetailsToConnectionDetails( | ||
replyData.connectionDetails | ||
), | ||
beforeTemplateIsBakedResult: | ||
replyData.beforeTemplateIsBakedResult.result, | ||
} | ||
} | ||
// Automatically cleaned up by AVA since each test file runs in a separate worker | ||
const _messageHandlerPromise = (async () => { | ||
const worker = await workerPromise | ||
await worker.available | ||
|
||
throw new Error(`Unexpected message type: ${replyData.type}`) | ||
for await (const msg of worker.subscribe()) { | ||
rpcCallback!(msg.data) | ||
} | ||
})() | ||
|
||
return waitForAndHandleReply( | ||
worker.publish({ | ||
type: "GET_TEST_DATABASE", | ||
params, | ||
key: getTestDatabaseOptions?.databaseDedupeKey, | ||
} as MessageToWorker) | ||
const getTestPostgresDatabase: GetTestPostgresDatabase<Params> = async ( | ||
t: ExecutionContext, | ||
params: any, | ||
getTestDatabaseOptions?: GetTestPostgresDatabaseOptions | ||
) => { | ||
const testDatabaseConnection = await rpc.getTestDatabase({ | ||
databaseDedupeKey: getTestDatabaseOptions?.databaseDedupeKey, | ||
params, | ||
}) | ||
|
||
const connectionDetails = mapWorkerConnectionDetailsToConnectionDetails( | ||
testDatabaseConnection.connectionDetails | ||
) | ||
|
||
t.teardown(async () => { | ||
await teardownConnection(connectionDetails) | ||
}) | ||
|
||
return { | ||
...connectionDetails, | ||
beforeTemplateIsBakedResult: | ||
testDatabaseConnection.beforeTemplateIsBakedResult, | ||
} | ||
} | ||
|
||
return getTestPostgresDatabase | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the idea meant to establish a bidirectional communication with the postgres docker container?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not quite
AVA's architecture has one worker thread per test file (called a test worker) and one worker thread per "shared worker" (called... a shared worker)
previous to this PR, we implemented 2-way RPC calls between the test worker and shared workers ourselves, but using birpc really makes the intent and logic cleaner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i see, this is to communicate with ava's worker thread