简体   繁体   English

使用Mocha进行集成测试时如何处理Elasticsearch的索引计时

[英]How do I handle elasticsearch's index timing when doing integration testing with mocha

I'm working on a node project that uses elasticsearch. 我正在使用弹性搜索的节点项目上。 My test chain is gulp,mocha,chai,sinon. 我的测试链是gulp,mocha,chai,sinon。 I'm having a hard time with getting my integration tests to work consistently. 我很难使我的集成​​测试能够始终如一地工作。 I want to load sample data in the database then run some tests over it. 我想将样本数据加载到数据库中,然后对其进行一些测试。 I believe I'm having problems because the documents I'm loading aren't indexed by the time my tests that use them run. 我相信我遇到了问题,因为我正在加载的文档在使用它们的测试运行时尚未被索引。

I've worked around this by doing the following: 我通过以下方法解决了这个问题:

    before(function (done) {
        testData.simpleLoadData(100, 2000);
        setTimeout(function () {
            done();
        }, 5000);
    });

This works fine locally, it works occasionally on travis. 这在本地工作正常,偶尔在travis上工作。 When I up the timer to 10000 it generally works in both places. 当我将计时器设置为10000时,它通常可以在两个地方工作。

Is there a way to this without resorting to setTimeouts in the test code? 有没有办法在测试代码中不诉诸setTimeouts? Manually dealing with timing makes me a little squeamish. 手动处理时间会让我有些胆怯。

Is manually dealing with timeouts the best option I have or are there better ways? 手动处理超时是我最好的选择,还是有更好的方法?

Note: these are integration tests and I explicitly want to use the external dependencies. 注意:这些是集成测试,我明确希望使用外部依赖项。 I have unit tests that don't rely on the database already. 我的单元测试已经不依赖数据库了。

There are two things here that can trip you up: shard allocation and refresh cycles. 这里有两件事可能会让您绊倒:分片分配和刷新周期。

The first one can happen when you create a new index. 当您创建新索引时,可能会发生第一个事件。 The Create Index api will return a 200 OK as soon as the master acknowledges the request and begins the creation process. 主节点确认请求并开始创建过程后,Create Index api将返回200 OK。 But the actual shard allocation happens asynchronously in the background. 但是实际的分片分配在后台异步发生。 And while it is quick, integration tests can sometimes execute before the index is fully up and running, causing an error when you try to index documents. 而且虽然速度很快,但是集成测试有时可以在索引完全启动和运行之前执行,从而在尝试索引文档时导致错误。

The simplest way to make this robust is by creating the index and then calling the Health API with wait_for_status=green (or yellow, depending if you use replicas). 使此功能更强大的最简单方法是创建索引,然后使用wait_for_status=green (或黄色,取决于您是否使用副本)来调用Health API This call will block until the index is fully allocated. 该调用将一直阻塞,直到完全分配了索引。

The next issue relates to the near-realtime aspect of search. 下一个问题涉及搜索的近实时性。 By default, Elasticsearch refreshes the search index every second. 默认情况下,Elasticsearch每秒刷新一次搜索索引。 This can be too slow for integration tests, and your documents may be indexed but not searchable when your tests run. 对于集成测试而言,这可能太慢,并且您的文档可能已建立索引,但在运行测试时不可搜索。

To fix this, index all your documents, then call a Refresh API on the target index. 要解决此问题,请为所有文档建立索引,然后在目标索引上调用Refresh API Once this call returns, your documents will be "live" and searchable. 该调用返回后,您的文档将“处于活动状态”并且可以搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用mocha + sinon测试ExpressJS路由时,如何“存根”该路由本地的函数? - When testing an ExpressJS route using mocha+sinon, how do you “stub” a function that's local to the route? Supertest,Mocha和Sinon的单元测试超时 - Unit testing with Supertest, Mocha & Sinon timing out 如何在mocha的mocha.opts文件中添加chaijs的断言? - How do I add chaijs's assert in mocha's mocha.opts file? 如何处理大量的ElasticSearch Index操作? - How can I handle a large amount of ElasticSearch Index operations? 如何从脚本访问 mocha 的 json 报告? - How do I access mocha's json report from a script? Node.js-为什么在用摩卡和僵尸进行测试时会泄漏? - Node.js - why do I get leaks when testing with mocha and zombie? 使用Mocha测试Promises时,发生错误时如何打印完整的堆栈跟踪 - When testing Promises with Mocha, how can I print the full stack trace when an error occurs 使用Mocha / chai / sinon进行单元测试Express-如何测试res.send对象的形状? - Unit Testing Express with mocha/chai/sinon - how do I test my res.send object shape? Mocha/Node.js/PostgreSQL 集成测试 - Mocha/Node.js/PostgreSQL integration testing 避免在带有 Mocha 测试的 Elasticsearch 请求中使用 setTimeout() - Avoid using setTimeout() in Elasticsearch requests with Mocha testing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM