RavenDB how to flush?

后端 未结 2 940
萌比男神i
萌比男神i 2020-12-30 12:11

I am using RavenDb embedded. As a part of my intergration tests I want to check objects are persisted. When I SaveChanges on an object, then retrieve it, it cannot be found

2条回答
  •  独厮守ぢ
    2020-12-30 12:51

    Basically, the EmbeddableDocumentStore takes longer to save and Index that new data, than saving and querying.

    So when your tests say:-

    1. Store and SaveChanges.
    2. Load.
    3. Did this load?

    The load completes much quicker than the indexing has had time to finish.

    So, Like Daniel Lang said, you need to wait for stale results.

    But, you'll have to do that for every query you wish to check, in your code. So, let's cheat (legally) :)

    Here is how you can tell your document store to ALWAYS wait for stale results, if something queries the store:

    // Initialise the Store.
    var documentStore = new EmbeddableDocumentStore
                        {
                            RunInMemory = true
                        };
    documentStore.Initialize();
    
    // Force query's to wait for index's to catch up. Unit Testing only :P
    documentStore.RegisterListener(new NoStaleQueriesListener());
    
    ....
    
    
    #region Nested type: NoStaleQueriesListener
    
    public class NoStaleQueriesListener : IDocumentQueryListener
    {
        #region Implementation of IDocumentQueryListener
    
        public void BeforeQueryExecuted(IDocumentQueryCustomization queryCustomization)
        {
            queryCustomization.WaitForNonStaleResults();
        }
    
        #endregion
    }
    
    #endregion
    

    Now to see this in action, check out RavenOverflow @ Github. And the Tests project in that solution has all the love you might want.

提交回复
热议问题