Skip to content

Commit 68eb755

Browse files
committed
increase cleanup timeout and add better logs
1 parent 80551ed commit 68eb755

File tree

1 file changed

+54
-9
lines changed
  • internal-packages/testcontainers/src

1 file changed

+54
-9
lines changed

internal-packages/testcontainers/src/index.ts

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,54 @@ type ContainerWithElectricContext = NetworkContext & PostgresContext & ElectricC
3131

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

34+
let cleanupOrder = 0;
35+
let activeCleanups = 0;
36+
37+
/**
38+
* Logs the cleanup of a resource.
39+
* @param resource - The resource that is being cleaned up.
40+
* @param fn - The cleanup function.
41+
*/
42+
async function logCleanup(resource: string, fn: () => Promise<void>) {
43+
const start = new Date();
44+
const order = cleanupOrder++;
45+
const activeAtStart = ++activeCleanups;
46+
47+
let error: unknown = null;
48+
try {
49+
await fn();
50+
} catch (err) {
51+
error = err instanceof Error ? err.message : String(err);
52+
}
53+
54+
const end = new Date();
55+
const activeAtEnd = --activeCleanups;
56+
const parallel = activeAtStart > 1 || activeAtEnd > 0;
57+
58+
console.log(
59+
JSON.stringify({
60+
order,
61+
start: start.toISOString(),
62+
end: end.toISOString(),
63+
parallel,
64+
resource,
65+
durationMs: end.getTime() - start.getTime(),
66+
error,
67+
activeAtStart,
68+
activeAtEnd,
69+
})
70+
);
71+
}
72+
3473
const network = async ({}, use: Use<StartedNetwork>) => {
3574
const network = await new Network().start();
3675
try {
3776
await use(network);
3877
} finally {
39-
try {
40-
await network.stop();
41-
} catch (error) {
42-
console.warn("Network stop error (ignored):", error);
43-
}
4478
// Make sure to stop the network after use
79+
await logCleanup("network", async () => {
80+
await network.stop();
81+
});
4582
}
4683
};
4784

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

@@ -77,7 +116,9 @@ const prisma = async (
77116
try {
78117
await use(prisma);
79118
} finally {
80-
await prisma.$disconnect();
119+
await logCleanup("prisma", async () => {
120+
await prisma.$disconnect();
121+
});
81122
}
82123
};
83124

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

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

0 commit comments

Comments
 (0)