Question

My goal is to have integration test that tests a Node Module which saves values in the redis. Here is the test:

require('coffee-script');
var should = require('should')
    , redis = require('redis')
    , async = require('async')
    , Domain = require('../index');

async.series([
    function(callback){
        db = redis.createClient();
        callback(null,'zero');
    },
    function(callback){
        db.flushdb( function (err, didSucceed) {
            if(err){
                console.log('error: ' + err.message);
                callback(err, 'one');
            }
        });
        callback(null, 'one');
    },
    function(callback){
        should.exist(Domain);

        domain = new Domain({'attOne':1, 'attTwo':2, 'id':1});

        domain.should.have.property('save');
        domain.should.have.property('attOne');
        domain.should.have.property('attTwo');

        domain.save.should.be.an.instanceof(Function);
        callback(null, 'two');
    },
    function(){
        db.end();
    }
],
    function(err, results){
        if(err){
           console.log('error encountered: ' + err.message);
           db.end();
        }
        console.log('All tests passed');
    }
);

With this test, the problem is that the redis connection is closed before the redis flushes the the database(If i remove the db.end() at all, the test hangs but the redis db is flushed). Apparently, the async.series is not working as i expect it where every function is run before the next one in a serial order or i am misunderstanding? how to ensure this test executes in serial flow so redis connection is not closed before the flush? ...any recommendations of any libraries/frameworks that could help me on what i am trying to accomplish here? Thank You

Était-ce utile?

La solution

well, the issue is that the callback in the redis hash commands(i.e. flushdb, hgetall ) doesn't stop the particular async process making it hang. I am not sure whether the 'async' module isn't it stoping properly or the 'redis' module has something different that prohibits 'async' to close that process....if you know the answer, please, share.

The solution was 'mocha' test framework. After installing(i.e npm install mocha) and then running 'mocha test/mytest.js', it would exit without any changes to the above code...not even formatting according to Mocha framework, it runs the test and exits nicely. At the same time, it would hang if run with 'node test/mytest.js'....the mystery of why so still remains:)

I hope this helps...It's time for Mocha now:)

Autres conseils

To end the connection and stop the hanging, use client.quit() or client.end() at proper place.

For example, put this at the end of the codes:

//...
setTimeout(function(){ client.quit(); }, 3000);
// 3000 means wait 3 seconds and quit the redis connection

client.unref() is another experimental function can do the job.

Refers to : doc

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top