正确终止测试mongodb服务器的方法
我们正在使用mongodb-prebuilt
软件包为开发的软件执行单元/集成测试(CI / CD),该软件与mongodb数据库进行交互。
常见用例如下:使用prebuild包启动测试mongodb服务器,执行测试套件执行,然后终止服务器。第一和第二项很容易完成,但第三项会导致一些问题。
如果mongodb-prebuilt
-started服务器尚未终止,则测试运行器将永久挂起。相反,如果尝试通过testConnectionmand({ shutdown: 1 })
命令终止服务器,那么unhandledRejection
会触发,它会在关闭的连接上引用 - 这当然是通过停止服务器强制关闭的。
哪个是在mongodb-prebuilt
测试部分处理afterAll
的正确方法?测试引擎是jest
,但它并不重要。
这里的示例代码:
import { MongodHelper } from 'mongodb-prebuilt';
import { MongoClient } from 'mongodb';
describe('Test suite', () => {
let testConnection;
beforeAll(async () => {
const mongodHelper = new MongodHelper(['--port', "27018"]);
await mongodHelper.run();
testConnection = await MongoClient.connect(`mongodb://localhost:27018/admin`);
});
afterAll(async () => {
await testConnection.dropDatabase();
await testConnectionmand({ shutdown: 1 });
await testConnection.close();
testConnection = null;
});
/* Here follows some test suite that uses testConnection */
}
有一些尝试解决问题:
1)不要等待testConnectionmand({ shutdown: 1 })
生成的承诺,并立即启动客户端连接关闭 - 它适用于某些机器,但很可能取决于执行速度,因此工作不稳定。
2)由于在测试结束时客户端连接终止并不重要 - 可以在process.on('unhandledRejection', ...)
部分设置afterAll
处理程序并且只是将异常静音 - 效果很好,但似乎在意识形态上不正确
那么,也许是原始任务的定型解决方案?
重要说明:建议使用一些模拟软件包而不是现场mongodb
根本不合适,因为开发的软件是mongodb
的特定适配器,应该尊重实时数据库的所有方面,包括时间,管理命令等等
原始问题最方便的解决方案是使用mongo-unit
npm软件包,尽管名称如此,它提供了用于实际mongodb预构建服务器的自动集成测试的API,包括启动和停止它:https://github/mikhail-angelov/mongo-unit/
使用mongo-unit
的仪表测试套件如下所示:
import 'regenerator-runtime/runtime';
import mongoUnit from 'mongo-unit';
import { MongoClient } from 'mongodb';
describe('MongoDB-based test suite, () => {
let testConnection;
beforeAll(async () => {
const connectionUrl = await mongoUnit.start({ dbName: 'admin' });
testConnection = await MongoClient.connect(connectionUrl);
});
afterAll(async () => {
await testConnection.dropDatabase();
await testConnection.close(true);
await mongoUnit.stop();
testConnection = null;
});
/* Test suite goes here */
});
包还允许在启动时选择初始数据库,并在测试套件结束时执行清除mongodb服务器进程。