1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

add testWithClientPool and first pool test

This commit is contained in:
Leibale
2023-11-20 12:47:13 -05:00
parent f8c24c0edd
commit 52e8c50dc8
2 changed files with 77 additions and 6 deletions

View File

@@ -0,0 +1,11 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
describe.only('RedisClientPool', () => {
testUtils.testWithClientPool('sendCommand', async pool => {
assert.equal(
await pool.sendCommand(['PING']),
'PONG'
);
}, GLOBAL.SERVERS.OPEN);
});

View File

@@ -8,6 +8,9 @@ import {
createClient, createClient,
RedisClientOptions, RedisClientOptions,
RedisClientType, RedisClientType,
RedisPoolOptions,
RedisClientPoolType,
createClientPool,
createCluster, createCluster,
RedisClusterOptions, RedisClusterOptions,
RedisClusterType RedisClusterType
@@ -23,6 +26,7 @@ interface TestUtilsConfig {
} }
interface CommonTestOptions { interface CommonTestOptions {
serverArguments: Array<string>;
minimumDockerVersion?: Array<number>; minimumDockerVersion?: Array<number>;
} }
@@ -33,11 +37,21 @@ interface ClientTestOptions<
RESP extends RespVersions, RESP extends RespVersions,
TYPE_MAPPING extends TypeMapping TYPE_MAPPING extends TypeMapping
> extends CommonTestOptions { > extends CommonTestOptions {
serverArguments: Array<string>;
clientOptions?: Partial<RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>>; clientOptions?: Partial<RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>>;
disableClientSetup?: boolean; disableClientSetup?: boolean;
} }
interface ClientPoolTestOptions<
M extends RedisModules,
F extends RedisFunctions,
S extends RedisScripts,
RESP extends RespVersions,
TYPE_MAPPING extends TypeMapping
> extends CommonTestOptions {
clientOptions?: Partial<RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>>;
poolOptions?: RedisPoolOptions;
}
interface ClusterTestOptions< interface ClusterTestOptions<
M extends RedisModules, M extends RedisModules,
F extends RedisFunctions, F extends RedisFunctions,
@@ -46,7 +60,6 @@ interface ClusterTestOptions<
TYPE_MAPPING extends TypeMapping TYPE_MAPPING extends TypeMapping
// POLICIES extends CommandPolicies // POLICIES extends CommandPolicies
> extends CommonTestOptions { > extends CommonTestOptions {
serverArguments: Array<string>;
clusterConfiguration?: Partial<RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>>; clusterConfiguration?: Partial<RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>>;
numberOfMasters?: number; numberOfMasters?: number;
numberOfReplicas?: number; numberOfReplicas?: number;
@@ -164,9 +177,9 @@ export default class TestUtils {
if (!dockerPromise) return this.skip(); if (!dockerPromise) return this.skip();
const client = createClient({ const client = createClient({
...options?.clientOptions, ...options.clientOptions,
socket: { socket: {
...options?.clientOptions?.socket, ...options.clientOptions?.socket,
// TODO // TODO
// @ts-ignore // @ts-ignore
port: (await dockerPromise).port port: (await dockerPromise).port
@@ -191,6 +204,53 @@ export default class TestUtils {
}); });
} }
testWithClientPool<
M extends RedisModules = {},
F extends RedisFunctions = {},
S extends RedisScripts = {},
RESP extends RespVersions = 2,
TYPE_MAPPING extends TypeMapping = {}
>(
title: string,
fn: (client: RedisClientPoolType<M, F, S, RESP, TYPE_MAPPING>) => unknown,
options: ClientPoolTestOptions<M, F, S, RESP, TYPE_MAPPING>
): void {
let dockerPromise: ReturnType<typeof spawnRedisServer>;
if (this.isVersionGreaterThan(options.minimumDockerVersion)) {
const dockerImage = this.#DOCKER_IMAGE;
before(function () {
this.timeout(30000);
dockerPromise = spawnRedisServer(dockerImage, options.serverArguments);
return dockerPromise;
});
}
it(title, async function () {
if (!dockerPromise) return this.skip();
const pool = createClientPool({
...options.clientOptions,
socket: {
...options.clientOptions?.socket,
// TODO
// @ts-ignore
port: (await dockerPromise).port
}
}, options.poolOptions);
await pool.connect();
try {
await pool.flushAll();
await fn(pool);
} finally {
await pool.flushAll();
pool.destroy();
}
});
}
static async #clusterFlushAll< static async #clusterFlushAll<
M extends RedisModules, M extends RedisModules,
F extends RedisFunctions, F extends RedisFunctions,
@@ -228,8 +288,8 @@ export default class TestUtils {
dockersPromise = spawnRedisCluster({ dockersPromise = spawnRedisCluster({
...dockerImage, ...dockerImage,
numberOfMasters: options?.numberOfMasters, numberOfMasters: options.numberOfMasters,
numberOfReplicas: options?.numberOfReplicas numberOfReplicas: options.numberOfReplicas
}, options.serverArguments); }, options.serverArguments);
return dockersPromise; return dockersPromise;
}); });