Skip to content

Commit 2e47955

Browse files
committed
stop redis container if setup fails
1 parent c66a302 commit 2e47955

File tree

1 file changed

+19
-2
lines changed
  • internal-packages/testcontainers/src

1 file changed

+19
-2
lines changed

internal-packages/testcontainers/src/utils.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { PostgreSqlContainer, StartedPostgreSqlContainer } from "@testcontainers/postgresql";
22
import { RedisContainer, StartedRedisContainer } from "@testcontainers/redis";
3+
import { tryCatch } from "@trigger.dev/core";
34
import Redis from "ioredis";
45
import path from "path";
6+
import { isDebug } from "std-env";
57
import { GenericContainer, StartedNetwork, Wait } from "testcontainers";
68
import { x } from "tinyexec";
79
import { expect } from "vitest";
@@ -67,7 +69,12 @@ export async function createRedisContainer({
6769
.start();
6870

6971
// Add a verification step
70-
await verifyRedisConnection(startedContainer);
72+
const [error] = await tryCatch(verifyRedisConnection(startedContainer));
73+
74+
if (error) {
75+
await startedContainer.stop({ timeout: 30 });
76+
throw new Error("verifyRedisConnection error", { cause: error });
77+
}
7178

7279
return {
7380
container: startedContainer,
@@ -94,11 +101,21 @@ async function verifyRedisConnection(container: StartedRedisContainer) {
94101
};
95102

96103
redis.on("error", (error) => {
97-
console.log("verifyRedisConnection error", error, containerMetadata);
104+
if (isDebug) {
105+
console.log("verifyRedisConnection: client error", error, containerMetadata);
106+
}
107+
108+
// Don't throw here, we'll do that below if the ping fails
98109
});
99110

100111
try {
101112
await redis.ping();
113+
} catch (error) {
114+
if (isDebug) {
115+
console.log("verifyRedisConnection: ping error", error, containerMetadata);
116+
}
117+
118+
throw new Error("verifyRedisConnection: ping error", { cause: error });
102119
} finally {
103120
await redis.quit();
104121
}

0 commit comments

Comments
 (0)