@@ -31,17 +31,54 @@ type ContainerWithElectricContext = NetworkContext & PostgresContext & ElectricC
31
31
32
32
type Use < T > = ( value : T ) => Promise < void > ;
33
33
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
+
34
73
const network = async ( { } , use : Use < StartedNetwork > ) => {
35
74
const network = await new Network ( ) . start ( ) ;
36
75
try {
37
76
await use ( network ) ;
38
77
} finally {
39
- try {
40
- await network . stop ( ) ;
41
- } catch ( error ) {
42
- console . warn ( "Network stop error (ignored):" , error ) ;
43
- }
44
78
// Make sure to stop the network after use
79
+ await logCleanup ( "network" , async ( ) => {
80
+ await network . stop ( ) ;
81
+ } ) ;
45
82
}
46
83
} ;
47
84
@@ -55,7 +92,9 @@ const postgresContainer = async (
55
92
} finally {
56
93
// WARNING: Testcontainers by default will not wait until the container has stopped. It will simply issue the stop command and return immediately.
57
94
// 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
+ } ) ;
59
98
}
60
99
} ;
61
100
@@ -77,7 +116,9 @@ const prisma = async (
77
116
try {
78
117
await use ( prisma ) ;
79
118
} finally {
80
- await prisma . $disconnect ( ) ;
119
+ await logCleanup ( "prisma" , async ( ) => {
120
+ await prisma . $disconnect ( ) ;
121
+ } ) ;
81
122
}
82
123
} ;
83
124
@@ -96,7 +137,9 @@ const redisContainer = async (
96
137
} finally {
97
138
// WARNING: Testcontainers by default will not wait until the container has stopped. It will simply issue the stop command and return immediately.
98
139
// 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
+ } ) ;
100
143
}
101
144
} ;
102
145
@@ -148,7 +191,9 @@ const electricOrigin = async (
148
191
} finally {
149
192
// WARNING: Testcontainers by default will not wait until the container has stopped. It will simply issue the stop command and return immediately.
150
193
// 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
+ } ) ;
152
197
}
153
198
} ;
154
199
0 commit comments