8
8
createClient ,
9
9
RedisClientOptions ,
10
10
RedisClientType ,
11
+ RedisPoolOptions ,
12
+ RedisClientPoolType ,
13
+ createClientPool ,
11
14
createCluster ,
12
15
RedisClusterOptions ,
13
16
RedisClusterType
@@ -23,6 +26,7 @@ interface TestUtilsConfig {
23
26
}
24
27
25
28
interface CommonTestOptions {
29
+ serverArguments : Array < string > ;
26
30
minimumDockerVersion ?: Array < number > ;
27
31
}
28
32
@@ -33,11 +37,21 @@ interface ClientTestOptions<
33
37
RESP extends RespVersions ,
34
38
TYPE_MAPPING extends TypeMapping
35
39
> extends CommonTestOptions {
36
- serverArguments : Array < string > ;
37
40
clientOptions ?: Partial < RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > > ;
38
41
disableClientSetup ?: boolean ;
39
42
}
40
43
44
+ interface ClientPoolTestOptions <
45
+ M extends RedisModules ,
46
+ F extends RedisFunctions ,
47
+ S extends RedisScripts ,
48
+ RESP extends RespVersions ,
49
+ TYPE_MAPPING extends TypeMapping
50
+ > extends CommonTestOptions {
51
+ clientOptions ?: Partial < RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > > ;
52
+ poolOptions ?: RedisPoolOptions ;
53
+ }
54
+
41
55
interface ClusterTestOptions <
42
56
M extends RedisModules ,
43
57
F extends RedisFunctions ,
@@ -46,7 +60,6 @@ interface ClusterTestOptions<
46
60
TYPE_MAPPING extends TypeMapping
47
61
// POLICIES extends CommandPolicies
48
62
> extends CommonTestOptions {
49
- serverArguments : Array < string > ;
50
63
clusterConfiguration ?: Partial < RedisClusterOptions < M , F , S , RESP , TYPE_MAPPING /*, POLICIES*/ > > ;
51
64
numberOfMasters ?: number ;
52
65
numberOfReplicas ?: number ;
@@ -164,9 +177,9 @@ export default class TestUtils {
164
177
if ( ! dockerPromise ) return this . skip ( ) ;
165
178
166
179
const client = createClient ( {
167
- ...options ? .clientOptions ,
180
+ ...options . clientOptions ,
168
181
socket : {
169
- ...options ? .clientOptions ?. socket ,
182
+ ...options . clientOptions ?. socket ,
170
183
// TODO
171
184
// @ts -ignore
172
185
port : ( await dockerPromise ) . port
@@ -191,6 +204,53 @@ export default class TestUtils {
191
204
} ) ;
192
205
}
193
206
207
+ testWithClientPool <
208
+ M extends RedisModules = { } ,
209
+ F extends RedisFunctions = { } ,
210
+ S extends RedisScripts = { } ,
211
+ RESP extends RespVersions = 2 ,
212
+ TYPE_MAPPING extends TypeMapping = { }
213
+ > (
214
+ title : string ,
215
+ fn : ( client : RedisClientPoolType < M , F , S , RESP , TYPE_MAPPING > ) => unknown ,
216
+ options : ClientPoolTestOptions < M , F , S , RESP , TYPE_MAPPING >
217
+ ) : void {
218
+ let dockerPromise : ReturnType < typeof spawnRedisServer > ;
219
+ if ( this . isVersionGreaterThan ( options . minimumDockerVersion ) ) {
220
+ const dockerImage = this . #DOCKER_IMAGE;
221
+ before ( function ( ) {
222
+ this . timeout ( 30000 ) ;
223
+
224
+ dockerPromise = spawnRedisServer ( dockerImage , options . serverArguments ) ;
225
+ return dockerPromise ;
226
+ } ) ;
227
+ }
228
+
229
+ it ( title , async function ( ) {
230
+ if ( ! dockerPromise ) return this . skip ( ) ;
231
+
232
+ const pool = createClientPool ( {
233
+ ...options . clientOptions ,
234
+ socket : {
235
+ ...options . clientOptions ?. socket ,
236
+ // TODO
237
+ // @ts -ignore
238
+ port : ( await dockerPromise ) . port
239
+ }
240
+ } , options . poolOptions ) ;
241
+
242
+ await pool . connect ( ) ;
243
+
244
+ try {
245
+ await pool . flushAll ( ) ;
246
+ await fn ( pool ) ;
247
+ } finally {
248
+ await pool . flushAll ( ) ;
249
+ pool . destroy ( ) ;
250
+ }
251
+ } ) ;
252
+ }
253
+
194
254
static async #clusterFlushAll<
195
255
M extends RedisModules ,
196
256
F extends RedisFunctions ,
@@ -228,8 +288,8 @@ export default class TestUtils {
228
288
229
289
dockersPromise = spawnRedisCluster ( {
230
290
...dockerImage ,
231
- numberOfMasters : options ? .numberOfMasters ,
232
- numberOfReplicas : options ? .numberOfReplicas
291
+ numberOfMasters : options . numberOfMasters ,
292
+ numberOfReplicas : options . numberOfReplicas
233
293
} , options . serverArguments ) ;
234
294
return dockersPromise ;
235
295
} ) ;
0 commit comments