2016-08-01 46 views
1

我對Golang非常陌生,我有一個關於測試的問題。
我有一個測試,我想檢查在elasticsearch的客戶是否堅持工作。我已將代碼縮減到關鍵部分,並將其發佈到github上:(https://github.com/fvosberg/elastic-go-testing在Golang測試Elasticsearch睡不着覺

問題是,我必須等待elasticsearch索引新文檔,然後才能搜索它。除了等待這一切發生,還有其他的選擇嗎?這感覺非常難看,但我不知道如何以另一種方式來測試整合(使用elasticsearch來降低電子郵件地址......)。

是否有解決這個問題的方法?

package main 

import (
    "github.com/fvosberg/elastic-go-testing/customer" 
    "testing" 
    "time" 
) 

func TestRegistration(t *testing.T) { 
    testCustomer := customer.Customer{Email: "[email protected]"} 
    testCustomer.Create() 
    time.Sleep(time.Second * 1) 
    _, err := customer.FindByEmail("[email protected]") 
    if err != nil { 
     t.Logf("Error occured: %+v\n", err) 
     t.Fail() 
    } else { 
     t.Log("Found customer [email protected]") 
    } 
} 

回答

1

Elasticsearch有一個flush命令,對這種情況很有用。由於您使用的elastic項目作爲一個接口,可以使用下列命令(其中客戶端是你的ES客戶端):

... testCustomer.Create() res, err := client.Flush().Do() if err != nil { t.Fatal(err) } _, err := customer.FindByEmail("[email protected]") ...

+1

太感謝你了,我不相信有一個解決方案除了等待一秒之外,這個問題。它像一個魅力 – noeden