torotask
Version:
Task queue processing in NodeJS based on BullMQ and Redis
47 lines • 1.37 kB
JavaScript
import { RedisMemoryServer } from 'redis-memory-server';
import { Redis } from 'ioredis';
export class TestRedisServer {
server = null;
redis = null;
async start() {
this.server = new RedisMemoryServer({
instance: {
port: 0, // Random available port
},
});
await this.server.start();
}
async stop() {
if (this.redis) {
this.redis.disconnect();
this.redis = null;
}
if (this.server) {
await this.server.stop();
this.server = null;
}
}
async getRedisUrl() {
if (!this.server) {
throw new Error('Redis server not started');
}
const host = await this.server.getHost();
const port = await this.server.getPort();
if (!host || !port) {
throw new Error(`Redis server not properly initialized - host: ${host}, port: ${port}`);
}
return `redis://${host}:${port}`;
}
async getRedisClient() {
if (!this.redis) {
const redisUrl = await this.getRedisUrl();
this.redis = new Redis(redisUrl);
}
return this.redis;
}
async flushAll() {
const client = await this.getRedisClient();
await client.flushall();
}
}
//# sourceMappingURL=test-redis.js.map