簡體   English   中英

如何在測試期間處理陳舊的索引?

[英]How should stale indexes be handled during testing?

我在內存模式下使用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);

這感覺很麻煩。 我想知道的是:

  • 在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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM