dongshenyu4638 2016-08-01 21:38
浏览 56
已采纳

无需睡眠即可在Golang中测试Elasticsearch

I am really new to Golang and I have a question regarding to testing.
I had a test where I wanted to check whether the persisting of a customer in elasticsearch works or not. I've reduced the code to the critical part and posted it on github: (https://github.com/fvosberg/elastic-go-testing)

The problem is, that I have to wait for elasticsearch to index the new document, before I can search for it. Is there another option than waiting a second for this to happen? This feels very ugly, but I don't know how I can test the integration (working with elasticsearch with lowercasing the email address ...) in another way.

Are there solutions for this problem?

package main

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

func TestRegistration(t *testing.T) {
    testCustomer := customer.Customer{Email: "testing@test.de"}
    testCustomer.Create()
    time.Sleep(time.Second * 1)
    _, err := customer.FindByEmail("testing@test.de")
    if err != nil {
        t.Logf("Error occured: %+v
", err)
        t.Fail()
    } else {
        t.Log("Found customer testing@test.de")
    }
}
  • 写回答

1条回答 默认 最新

  • dongpeng7744 2016-08-02 04:03
    关注

    Elasticsearch has a flush command that is useful for this situation. Since you're using the elastic project as an interface, you can use the following (where client is your ES client):

    ... testCustomer.Create() res, err := client.Flush().Do() if err != nil { t.Fatal(err) } _, err := customer.FindByEmail("testing@test.de") ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容