最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

正确终止测试mongodb服务器的方法

运维笔记admin14浏览0评论

正确终止测试mongodb服务器的方法

正确终止测试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服务器进程。

发布评论

评论列表(0)

  1. 暂无评论