【问题标题】:Tests don't finish because of pending MongoDB connections (@hapi/lab, mongodb-memory-server, node)由于挂起的 MongoDB 连接(@hapi/lab、mongodb-memory-server、node),测试未完成
【发布时间】:2020-04-09 08:20:46
【问题描述】:

我遇到了这个问题,所有测试都成功通过,但最后,这个过程永远不会结束。而我能看到的是,总是有两个mongod 进程实例在运行。

我正在使用 mongodb-memory-server 库来模拟数据库连接、mongodbhapijs 环境的最新版本。

const { expect } = require ('@hapi/code');
const lab = exports.lab = require ('@hapi/lab').script ();
const { it, describe, after, before } = lab;
const Sinon = require ('sinon');
const DAO = require ('./index');
const CaseBaseDao = require ('case-base-dao'); //from private npm repo

let baseDaoFindStub;
let updateOneStub;

describe ('Profiles DAO', () =>
{
    before (async ({ context }) =>
    {
        const { MongoClient } = require ('mongodb');
        const { MongoMemoryServer } = require ('mongodb-memory-server');
        const mongoServerInstance = new MongoMemoryServer ({ instance: { port: 27018, dbName: 'administration' } });

        const url = await mongoServerInstance.getConnectionString ();
        const dbClient = await MongoClient.connect (url);
        const collection = await dbClient.db ().createCollection ('profiles');
        await collection.insertMany (require ('./examples/profiles.json'));
        const dao = DAO () (dbClient.db ());

        context.dbClient = dbClient;
        context.mongoServerInstance = mongoServerInstance;
        context.sandbox = Sinon.createSandbox ();
        context.dao = dao;

        baseDaoFindStub = context.sandbox.stub (CaseBaseDao.BaseDao.prototype, 'find').resolves ();
        updateOneStub = context.sandbox.stub (dao.collection, 'updateOne').resolves ();
    });

    after (async ({ context: { sandbox, dbClient, mongoServerInstance } }) =>
    {
        await dbClient.close (); // only added for debug purposes
        await mongoServerInstance.stop (); // only added for debug purposes
        sandbox.restore ();
    });

    it ('Should expose DAO methods', ({ context: { dao } }) =>
    {
        expect (dao).to.be.an.object ();
        expect (dao.findByEmail).to.exist ();
        expect (dao.find).to.exist ();
        expect (dao.findByQuery).to.exist ();
        expect (dao.updateOneById).to.exist ();
        expect (dao.addSmartPerformanceDashboards).to.exist ();
        expect (dao.deleteSmartPerformanceDashboards).to.exist ();
        expect (dao.updateSmartPerformanceDashboards).to.exist ();
    });
});

还有很多测试,但是如果我只执行这一个,它就会卡在最后。

如果我尝试在 after 块中手动关闭连接,则会出现以下错误:

对于await dbClient.close()

消息:“非法调用”

stack:"TypeError: Illegal invocation\n at handleWriteReq (internal/stream_base_commons.js:46:26)\n at writeGeneric (internal/stream_base_commons.js:139:15)\n at Socket._writeGeneric (net.js :771:11)\n 在 Socket._write (net.js:783:8)\n 在 doWrite (_stream_writable.js:431:12)\n 在 writeOrBuffer (_stream_writable.js:415:5)\n 在 Socket .Writable.write (_stream_writable.js:305:11)\n 在 Connection.write ({path}/node_modules/mongodb/lib/core/connection/connection.js:265:21)\n 在 {path}/node_modules /mongodb/lib/core/connection/pool.js:1208:40\n 在 checkStatus ({path}/node_modules/mongodb/lib/core/connection/pool.js:732:21)\n 在 Pool.destroy ( {path}/node_modules/mongodb/lib/core/connection/pool.js:739:3)\n 在 Server.destroy ({path}/node_modules/mongodb/lib/core/topologies/server.js:897:15 )\n ...

对于await mongoServerInstance.stop ();

message:"Method Promise.prototype.then 调用了不兼容的接收器#"

stack:"TypeError: Method Promise.prototype.then 在 Promise.then () 处调用了不兼容的接收器#\n"

我几乎尝试了所有方法:更改端口、使用 Promise 代替 async/await、避免使用 Sinon 和存根(以防万一)等等。但我的想法已经不多了。

我正在使用这个完全相同的代码来模拟其他项目中的数据库,它工作得很好,不需要手动关闭数据库连接,它只是自动完成。

这发生在我的本地环境以及 Bitbucket 管道中,因此被丢弃为与我的计算机相关的东西。

【问题讨论】:

    标签: javascript node.js mongodb unit-testing hapijs


    【解决方案1】:

    终于解决了。

    不知道为什么会发生这种情况,但解决方案不是通过@hapi/lab context 使用与mongo 实例相关的变量来关闭MongoDB 连接,而是在before 块之外定义它们。所以:

    let baseDaoFindStub;
    let updateOneStub;
    let mongoServerInstance;  // Now defined here
    let dbClient;             // Now defined here
    
    describe ('Profiles DAO', () =>
    {
        before (async ({ context }) =>
        {
            const { MongoClient } = require ('mongodb');
            const { MongoMemoryServer } = require ('mongodb-memory-server');
            mongoServerInstance = new MongoMemoryServer ({ instance: { port: 27018, dbName: 'administration' } });
    
            const url = await mongoServerInstance.getConnectionString ();
            dbClient = await MongoClient.connect (url);
            const collection = await dbClient.db ().createCollection ('profiles');
            await collection.insertMany (req
    ...
    ...
    ...
    
    after (async ({ context: { sandbox } }) =>  // Don't get vars from context
        {
            sandbox.restore ();
            await dbClient.close ();
            await mongoServerInstance.stop ();
        });
    

    我不是第一次在使用@hapi/lab context 时遇到这种问题,所以请小心!

    【讨论】:

      猜你喜欢
      • 2021-02-01
      • 2020-05-29
      • 2021-03-09
      • 2019-07-23
      • 2014-03-17
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多