Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal-packages/testcontainers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"ioredis": "^5.3.2"
},
"devDependencies": {
"@testcontainers/postgresql": "^10.13.1",
"@testcontainers/redis": "^10.13.1",
"@testcontainers/postgresql": "^10.25.0",
"@testcontainers/redis": "^10.25.0",
"@trigger.dev/core": "workspace:*",
"testcontainers": "^10.13.1",
"testcontainers": "^10.25.0",
"tinyexec": "^0.3.0",
"vitest": "^1.4.0"
},
Expand Down
63 changes: 54 additions & 9 deletions internal-packages/testcontainers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,54 @@ type ContainerWithElectricContext = NetworkContext & PostgresContext & ElectricC

type Use<T> = (value: T) => Promise<void>;

let cleanupOrder = 0;
let activeCleanups = 0;

/**
* Logs the cleanup of a resource.
* @param resource - The resource that is being cleaned up.
* @param fn - The cleanup function.
*/
async function logCleanup(resource: string, fn: () => Promise<void>) {
const start = new Date();
const order = cleanupOrder++;
const activeAtStart = ++activeCleanups;

let error: unknown = null;
try {
await fn();
} catch (err) {
error = err instanceof Error ? err.message : String(err);
}

const end = new Date();
const activeAtEnd = --activeCleanups;
const parallel = activeAtStart > 1 || activeAtEnd > 0;

console.log(
JSON.stringify({
order,
start: start.toISOString(),
end: end.toISOString(),
parallel,
resource,
durationMs: end.getTime() - start.getTime(),
error,
activeAtStart,
activeAtEnd,
})
);
}

const network = async ({}, use: Use<StartedNetwork>) => {
const network = await new Network().start();
try {
await use(network);
} finally {
try {
await network.stop();
} catch (error) {
console.warn("Network stop error (ignored):", error);
}
// Make sure to stop the network after use
await logCleanup("network", async () => {
await network.stop();
});
}
};

Expand All @@ -55,7 +92,9 @@ const postgresContainer = async (
} finally {
// WARNING: Testcontainers by default will not wait until the container has stopped. It will simply issue the stop command and return immediately.
// If you need to wait for the container to be stopped, you can provide a timeout. The unit of timeout option here is second
await container.stop({ timeout: 10 });
await logCleanup("postgresContainer", async () => {
await container.stop({ timeout: 30 });
});
}
};

Expand All @@ -77,7 +116,9 @@ const prisma = async (
try {
await use(prisma);
} finally {
await prisma.$disconnect();
await logCleanup("prisma", async () => {
await prisma.$disconnect();
});
}
};

Expand All @@ -96,7 +137,9 @@ const redisContainer = async (
} finally {
// WARNING: Testcontainers by default will not wait until the container has stopped. It will simply issue the stop command and return immediately.
// If you need to wait for the container to be stopped, you can provide a timeout. The unit of timeout option here is second
await container.stop({ timeout: 10 });
await logCleanup("redisContainer", async () => {
await container.stop({ timeout: 30 });
});
}
};

Expand Down Expand Up @@ -148,7 +191,9 @@ const electricOrigin = async (
} finally {
// WARNING: Testcontainers by default will not wait until the container has stopped. It will simply issue the stop command and return immediately.
// If you need to wait for the container to be stopped, you can provide a timeout. The unit of timeout option here is second
await container.stop({ timeout: 10 });
await logCleanup("electricContainer", async () => {
await container.stop({ timeout: 30 });
});
}
};

Expand Down
Loading
Loading