如何在测试期间处理陈旧的索引?
我在内存模式下使用RavenDB进行unit testing。 我的查询由静态索引支持。 我没有使用WaitForNonStaleResults()
API(我也不想)。
测试的典型工作流程是:
- 在In-Memory模式下初始化RavenDB
- 使用
IndexCreation.CreateIndexes(Assembly, IDocumentStore)
集成索引 - 插入测试数据(用于validation查询行为)
- 运行查询
- validation查询输出
我注意到步骤1-3发生得如此之快,静态索引没有时间在步骤4之前得到更新 – 因此索引是陈旧的。
我为此创建了一个快速的解决方案。 在第3步之后,我执行:
while (documentStore.DocumentDatabase.Statistics.StaleIndexes.Length != 0) Thread.Sleep(10);
这感觉很麻烦。 我想知道的是:
- 在In-Memory模式下运行RavenDB时,索引是否过时是正常的?
- 在测试期间是否有更好的方法来避免过时的索引?
将此交叉发布到RavenDB用户组并提供可用的解决方案。
在In-Memory模式下运行RavenDB时,索引是否过时是正常的?
是。 索引是索引。
在测试期间是否有更好的方法来避免过时的索引?
是。 初始化文档存储时配置全局约定:
var store = new EmbeddableDocumentStore(); store.RunInMemory = true; store.Conventions = new DocumentConvention { DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites }; store.Initialize();
注意: ConsistencyOptions.QueryYourWrites
不适用于Map / Reduce索引,即具有Reduce => ...
部分的索引。 对于这些,您必须在查询时使用Customize(x => x.WaitForNonStale...())
更新:还有另一种方法 ,可能更好(尚未亲自尝试过)。 您可以实现IDocumentQueryListener以强制所有查询返回非陈旧结果:
var store = new EmbeddableDocumentStore { RunInMemory = true }; store.Initialize(); store.RegisterListener(new ForceNonStaleQueryListener()); public class ForceNonStaleQueryListener : IDocumentQueryListener { public void BeforeQueryExecuted(IDocumentQueryCustomization customization) { queryCustomization.WaitForNonStaleResults(); } }