node.js - 当异步回调存在时,redis hash 命令挂起 Node.js

标签 node.js mocha.js node-redis

我的目标是进行集成测试,测试在 redis 中保存值的 Node 模块。这是测试:

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');
    }
);

通过此测试,问题在于 Redis 连接在 Redis 刷新数据库之前关闭(如果我完全删除 db.end(),测试会挂起,但 Redis 数据库会被刷新)。显然, async.series 没有按照我的预期工作,每个函数都按顺序在下一个函数之前运行,或者我误解了?如何确保此测试以串行流执行,以便 Redis 连接在刷新之前不会关闭? ...任何库/框架的任何建议可以帮助我完成我在这里想要完成的任务?谢谢您

最佳答案

要结束连接并停止挂起,请在适当的位置使用 client.quit()client.end()

例如,将其放在代码末尾:

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

client.unref() 是另一个可以完成这项工作的实验函数。

引用:doc

关于node.js - 当异步回调存在时,redis hash 命令挂起 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701296/

相关文章:

node.js - cucumberjs:查找步骤结果是否失败

node.js - 如何在redis中增加一个值?

javascript - Nodejs和redis结构化数据

node.js - Puppeteer - Handlin page.waitForSelector() 失败

node.js - Firebase 异步函数语法错误

javascript - MochaJS - 在其他文件中重用 block

javascript - Mocha 因 "timeout exceeded"失败

redis - 为什么在使用 node 和 redis 时我的 promise 没有解决?我的数组返回第一次迭代而不是等待整个代码运行

node.js - 更改mongo数据库

javascript - Gulp 任务完成且没有错误,但没有文件保存到目标