2012-02-07 88 views
7

我正在使用RavenDb嵌入式。作爲我的集成測試的一部分,我想檢查對象是否持久。當我在一個對象上保存更改時,然後檢索它,除非我處理我的連接,否則無法找到它。RavenDB如何刷新?

這並沒有爲我工作,因爲沒有文件返回

using (var session = _dataDocumentStore.Instance.OpenSession()) 
{ 
     session.Store(file); 
     session.SaveChanges(); 
} 

....

using (var session = _dataDocumentStore.Instance.OpenSession()) 
{ 
      return session.Query<File>().ToList(); 
} 

我創建了部署並重新創建一個EmbeddableDocumentStore其工作的Flush方法,但作爲這是感覺根本的東西,我可能會以錯誤的方式去做事:

public static IDocumentStore Initialize() 
     { 
      instance = new EmbeddableDocumentStore 
      { 
       DataDirectory = "App_Data/Database", 
       UseEmbeddedHttpServer = true, 


      }; 

      instance.Initialize(); 
      return instance; 
     } 

     public void Flush() 
     { 
      instance.Dispose(); 
      Initialize(); 

     } 

H你堅持RavenDB,然後檢查它已被持續?在這個任何意見將是巨大的

+0

是否有您所呼叫instance.Initialize()的理由的愛;當您進入IDocumentStore Initialize()方法時,您正在創建新實例 – MethodMan 2012-02-07 17:56:06

+0

數據庫本身顯示什麼?檢查Raven Studio以確保您的保存工作正常。你的文件在那裏嗎? – 2012-02-07 18:47:30

回答

15

基本上,EmbeddableDocumentStore需要更長的時間來保存和指數新數據,不是保存和查詢。

所以,當你的測試說: -

  1. 存儲和調用SaveChanges。
  2. 負載。
  3. 這篇負荷?

加載完成waaay比索引有時間快完成了。

因此,像丹尼爾·蘭說,U需要等待陳舊結果。

但..你必須爲你做的要檢查,在你的代碼-every-查詢。所以..讓作弊(合法):)

告訴你的文檔存儲到一直等陳舊的結果,如果某些查詢的商店。

怎麼樣?

// 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 

現在看到這個在行動中,檢查出RavenOverflow @ github。而在該解決方案的Tests project有所有你可能需要:)

enter image description here

+0

++。對於版本3:'documentStore.Listeners.RegisterListener' – 2015-09-21 20:30:24