javascript - 由于挂起的 MongoDB 连接(@hapi/lab、mongodb-memory-server、node),测试未完成
问题描述
我遇到了这个问题,所有测试都成功通过,但最后,这个过程永远不会结束。我可以看到总是有两个mongod
流程实例在运行。
我正在使用mongodb-memory-server
库来模拟数据库连接、最新版本mongodb
和hapijs
环境。
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。写入 (_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 ();
消息:“方法 Promise.prototype.then 调用了不兼容的接收器#”
stack:"TypeError: Method Promise.prototype.then 在 Promise.then () 处调用了不兼容的接收器#\n"
我几乎尝试了所有方法:更改端口,使用 Promise 代替 async/await,避免使用 Sinon 和存根(以防万一)等等。我的想法已经不多了。
我使用这个完全相同的代码来模拟其他项目中的数据库,它工作得很好,不需要手动关闭数据库连接,它只是自动完成。
这发生在我的本地环境以及 Bitbucket 管道中,因此被丢弃为与我的计算机相关的东西。
解决方案
终于解决了。
不知道为什么会发生这种情况,但解决方案不是使用 mongo 实例相关的变量通过 @hapi/lab 关闭 MongoDB 连接,而是在块context
之外定义它们。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
,所以,小心它!
推荐阅读
- python - Python:计算矩阵内核时的计算精度
- javascript - woocommerce 消息“已添加到您的卡”已损坏
- jquery - 动态设置后选择器的宽度
- python - 通过 Python iLO REST 客户端连接到 HPE iLO 5 时出现 RetriesExhaustedError
- outlook-web-addins - Outlook 2016 的旁加载加载项
- python - 自 ElasticSearch 映射以来 Spark 映射中的 PyPark 错误
- python - 默认情况下,在 Python 中安装“可选”依赖项(setuptools)
- android - apksigner, zipalign and jarsigner not working as expected
- html - Make image scale next to a div inside a div
- python - Is it possible to generate a pytest HTML report on a timeout?