如何在测试期间处理陈旧的索引?

Arn*_*kas 11 c# testing indexing ravendb

我在内存模式下使用RavenDB进行单元测试.我的查询由静态索引支持.我没有使用WaitForNonStaleResults()API(我也不想).

测试的典型工作流程是:

  1. 在In-Memory模式下初始化RavenDB
  2. 使用集成索引 IndexCreation.CreateIndexes(Assembly, IDocumentStore)
  3. 插入测试数据(用于验证查询行为)
  4. 运行查询
  5. 验证查询输出

我注意到步骤1-3发生得如此之快,静态索引没有时间在步骤4之前得到更新 - 因此索引是陈旧的.

我为此创建了一个快速的解决方案.在第3步之后,我执行:

while (documentStore.DocumentDatabase.Statistics.StaleIndexes.Length != 0)
    Thread.Sleep(10);
Run Code Online (Sandbox Code Playgroud)

这感觉很麻烦.我想知道的是:

  • 在In-Memory模式下运行RavenDB时,索引是否过时是正常的?
  • 在测试期间是否有更好的方法来避免过时的索引?

Arn*_*kas 15

将此交叉发布到RavenDB用户组并提供有效的解决方案.

在In-Memory模式下运行RavenDB时,索引是否过时是正常的?

是.索引是索引.

在测试期间是否有更好的方法来避免过时的索引?

是.初始化文档存储时配置全局约定:

var store = new EmbeddableDocumentStore();
store.RunInMemory = true;
store.Conventions = new DocumentConvention
{
    DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites
};

store.Initialize();
Run Code Online (Sandbox Code Playgroud)

注意: 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();
    }
}
Run Code Online (Sandbox Code Playgroud)