@@ -46,8 +46,26 @@ export async function pushDatabaseSchema(databaseUrl: string) {
4646 ) ;
4747}
4848
49+ /**
50+ * Caps each container's CPU/memory to approximate the 2-core CI runner locally (for timing + flake
51+ * reproduction). Set TESTCONTAINERS_CPU (cores per container, e.g. "2") and/or
52+ * TESTCONTAINERS_MEMORY_GB (GB per container). Pair with running the runner under `taskset -c 0,1`.
53+ * No-op when neither is set. (testcontainers v11 has no cpuset pinning, only this quota cap.)
54+ */
55+ export function withCiResourceLimits < T extends GenericContainer > ( container : T ) : T {
56+ const cpu = process . env . TESTCONTAINERS_CPU ;
57+ const memory = process . env . TESTCONTAINERS_MEMORY_GB ;
58+ if ( ! cpu && ! memory ) {
59+ return container ;
60+ }
61+ return container . withResourcesQuota ( {
62+ ...( cpu ? { cpu : Number ( cpu ) } : { } ) ,
63+ ...( memory ? { memory : Number ( memory ) } : { } ) ,
64+ } ) ;
65+ }
66+
4967export async function createPostgresContainer ( network : StartedNetwork ) {
50- const container = await new PostgreSqlContainer ( "docker.io/postgres:14" )
68+ const container = await withCiResourceLimits ( new PostgreSqlContainer ( "docker.io/postgres:14" ) )
5169 . withNetwork ( network )
5270 . withNetworkAliases ( "database" )
5371 . withCommand ( [ "-c" , "listen_addresses=*" , "-c" , "wal_level=logical" ] )
@@ -59,7 +77,9 @@ export async function createPostgresContainer(network: StartedNetwork) {
5977}
6078
6179export async function createClickHouseContainer ( network : StartedNetwork ) {
62- const container = await new ClickHouseContainer ( ) . withNetwork ( network ) . start ( ) ;
80+ const container = await withCiResourceLimits ( new ClickHouseContainer ( ) )
81+ . withNetwork ( network )
82+ . start ( ) ;
6383
6484 const client = createClient ( {
6585 url : container . getConnectionUrl ( ) ,
@@ -86,7 +106,7 @@ export async function createRedisContainer({
86106 port ?: number ;
87107 network ?: StartedNetwork ;
88108} ) {
89- let container = new RedisContainer ( "redis:7.2" )
109+ let container = withCiResourceLimits ( new RedisContainer ( "redis:7.2" ) )
90110 . withExposedPorts ( port ?? 6379 )
91111 . withStartupTimeout ( 120_000 ) ; // 2 minutes
92112
@@ -167,8 +187,10 @@ export async function createElectricContainer(
167187 network . getName ( )
168188 ) } :5432/${ postgresContainer . getDatabase ( ) } ?sslmode=disable`;
169189
170- const container = await new GenericContainer (
171- "electricsql/electric:1.2.4@sha256:20da3d0b0e74926c5623392db67fd56698b9e374c4aeb6cb5cadeb8fea171c36"
190+ const container = await withCiResourceLimits (
191+ new GenericContainer (
192+ "electricsql/electric:1.2.4@sha256:20da3d0b0e74926c5623392db67fd56698b9e374c4aeb6cb5cadeb8fea171c36"
193+ )
172194 )
173195 . withExposedPorts ( 3000 )
174196 . withNetwork ( network )
@@ -185,7 +207,7 @@ export async function createElectricContainer(
185207}
186208
187209export async function createMinIOContainer ( network : StartedNetwork ) {
188- const container = await new MinIOContainer ( )
210+ const container = await withCiResourceLimits ( new MinIOContainer ( ) )
189211 . withNetwork ( network )
190212 . withNetworkAliases ( "minio" )
191213 . start ( ) ;
0 commit comments