How should stale indexes be handled during testing?

前端 未结 1 1915
臣服心动
臣服心动 2020-12-28 21:02

I am using RavenDB in In-Memory mode for unit testing. My queries are backed by static indexes. I am not using WaitForNonStaleResults() API (nor do I want to).<

相关标签:
1条回答
  • 2020-12-28 21:26

    Cross-posted this to RavenDB usergroup and have a working solution.

    Is it normal for indexes to be stale when running RavenDB in In-Memory mode?

    Yes. An index is an index.

    Is there a better way to avoid stale indexes during testing?

    Yes. Configure global conventions when initialising document store:

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

    Note: ConsistencyOptions.QueryYourWrites doesn't work with Map/Reduce indexes, i.e. indexes with a Reduce => ... section. For these you have to use Customize(x => x.WaitForNonStale...()) when querying

    Update: There is another approach, which may be better (haven't personally tried it yet). You could implement IDocumentQueryListener to force all queries to return non-stale results:

    var store = new EmbeddableDocumentStore { RunInMemory = true };
    store.Initialize();
    
    store.RegisterListener(new ForceNonStaleQueryListener());
    
    public class ForceNonStaleQueryListener : IDocumentQueryListener
    {
        public void BeforeQueryExecuted(IDocumentQueryCustomization customization)
        {
            queryCustomization.WaitForNonStaleResults();
        }
    }
    
    0 讨论(0)
提交回复
热议问题