Skip to content

Fix testcontainers cleanup #2023

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

Merged
merged 20 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
77 changes: 77 additions & 0 deletions .github/workflows/testcontainers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: "🐳 Testcontainers"

on:
workflow_call:
workflow_dispatch:
push:

jobs:
unitTests:
name: "🧪 Unit Tests (run ${{ matrix.run }})"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
run: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
steps:
- name: 🔧 Configure docker address pool
run: |
CONFIG='{
"default-address-pools" : [
{
"base" : "172.17.0.0/12",
"size" : 20
},
{
"base" : "192.168.0.0/16",
"size" : 24
}
]
}'
mkdir -p /etc/docker
echo "$CONFIG" | sudo tee /etc/docker/daemon.json
sudo systemctl restart docker

- name: ⬇️ Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: ⎔ Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 8.15.5

- name: ⎔ Setup node
uses: buildjet/setup-node@v4
with:
node-version: 20.11.1
cache: "pnpm"

# ..to avoid rate limits when pulling images
- name: 🐳 Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: 📥 Download deps
run: pnpm install --frozen-lockfile

- name: 📀 Generate Prisma Client
run: pnpm run generate

- name: 🧪 Run Webapp Unit Tests
run: pnpm run test:webapp
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres
DIRECT_URL: postgresql://postgres:postgres@localhost:5432/postgres
SESSION_SECRET: "secret"
MAGIC_LINK_SECRET: "secret"
ENCRYPTION_KEY: "secret"

- name: 🧪 Run Package Unit Tests
run: pnpm run test:packages

- name: 🧪 Run Internal Unit Tests
run: pnpm run test:internal
18 changes: 18 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ jobs:
name: "🧪 Unit Tests"
runs-on: ubuntu-latest
steps:
- name: 🔧 Configure docker address pool
run: |
CONFIG='{
"default-address-pools" : [
{
"base" : "172.17.0.0/12",
"size" : 20
},
{
"base" : "192.168.0.0/16",
"size" : 24
}
]
}'
mkdir -p /etc/docker
echo "$CONFIG" | sudo tee /etc/docker/daemon.json
sudo systemctl restart docker

- name: ⬇️ Checkout repo
uses: actions/checkout@v4
with:
Expand Down
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
53 changes: 44 additions & 9 deletions internal-packages/testcontainers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,52 @@ 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 promise - The cleanup promise to await..
*/
async function logCleanup(resource: string, promise: Promise<unknown>) {
const start = new Date();
const order = cleanupOrder++;
const activeAtStart = ++activeCleanups;

let error: unknown = null;
try {
await promise;
} 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,
resource,
durationMs: end.getTime() - start.getTime(),
start: start.toISOString(),
end: end.toISOString(),
parallel,
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", network.stop());
}
};

Expand All @@ -55,7 +90,7 @@ 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", container.stop({ timeout: 30 }));
}
};

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

Expand All @@ -96,7 +131,7 @@ 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", container.stop({ timeout: 30 }));
}
};

Expand Down Expand Up @@ -148,7 +183,7 @@ 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", container.stop({ timeout: 30 }));
}
};

Expand Down
Loading
Loading