Redis connection refused only when using test containers

59 Views Asked by At

I am trying to get test containers and redis to work with a Node.js application that is using BullMQ to create some queues. I am getting connection refused when running tests but it works when running the actual application.

Code

const { expect } = require("chai");
const { createAllQueues } = require("../../../queue/create");

// const shutdown = require("../../../queue/shutdown");

const { RedisContainer } = require("@testcontainers/redis");
const { Wait } = require("testcontainers");

describe("Fully Cycle Queue Test", function() {
    this.timeout(10000);

    let redis;
    before(async () => {
        try {
            const password = "myRedisPassword";
            redis = await new RedisContainer()
            // .withExposedPorts(6379)
            .withPassword(password)
            .start();
            createAllQueues({
                maxRetriesPerRequest: null,
                host: redis.getHost(),
                port: redis.getMappedPort(6379),
                password: password
            });
        } catch (error) {
            console.log("Error starting redis: ", error);
        }
        
    });

    after(async () => {
        try {
            // shutdown();
            await redis.stop();
        } catch (error) {
            console.log("Error stopping redis: ", error);
        }
    });

    it("should start redis", async () => {
        expect(redis).to.exist;
        expect(redis).to.have.property("getMappedPort");
        expect(redis).to.have.property("getHost");
        expect(redis).to.have.property("getConnectionUrl");
        expect(redis).to.have.property("stop");
    });
...

Create.js file

const { Queue } = require("bullmq");
const ioredis = require("ioredis");

...

function createConnection(params) {
    logger.debug("Creating connection to Redis with params: ", params);
    try {
        const connection = new ioredis({
            maxRetriesPerRequest: params.maxRetriesPerRequest,
            host: params.host,
            port: params.port,
            password: params.password || undefined,
        });
        return connection;
    } catch (error) {
        logger.error("Error creating connection to Redis: ", error);
        return null;
    }
}


function createQueues(connection) {
    if (!connection) {
        throw new Error("connection is required");
    }
    let createdQueues = [];

    // Create queues based on the list
    queueList.forEach(queueName => {
        logger.info("Creating queue", queueName);
        const queue = new Queue(queueName, {
            connection,
            defaultJobOptions: {
                removeOnComplete: true,
                removeOnFail: true
            }
        });
        createdQueues.push(queue);
    });
    return createdQueues
}


function createAllQueues(params) {
    try {
        const connection = createConnection(params);
        return {
            createdQueues: createQueues(connection),
        
        };
    } catch (error) {
        logger.error("Error creating queues: ", error);
        return false;
    }
}

module.exports = {
    createAllQueues,
};

Error message:

Error: connect ECONNREFUSED 127.0.0.1:55163
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 55163
}

Seems to be hitting the right port but will not connect. The container spins up and down too fast for me to manually test it either.

0

There are 0 best solutions below