RavenDb : Force indexes to wait until not stale whilst unit testing

前端 未结 3 521
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 07:36

When unit testing with RavenDb, it is often the case that newly added data is retrieved or otherwise processed. This can lead to \'stale index\' exceptions e.g.

3条回答
  •  轮回少年
    2020-12-14 08:31

    If you have a Map/Reduce index, DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites won't work. You need to use an alternative method.

    In your units tests, call code like this, straight after you've inserted any data, this will force the all indexes to update before you do anything else:

    while (documentStore.DatabaseCommands.GetStatistics().StaleIndexes.Length != 0)
    {
        Thread.Sleep(10);
    }
    

    Update You can of course put it in an extension method if you want to:

    public static class IDocumentSessionExt
    {
        public static void ClearStaleIndexes(this IDocumentSession db)
        {
            while (db.Advanced.DatabaseCommands.GetStatistics().StaleIndexes.Length != 0)
            {
                Thread.Sleep(10);
            }
        }
    }
    

    Then you can say:

    db.ClearStaleIndexes();
    

提交回复
热议问题