我正在尝试调试一个类似于以下内容的片状单元测试:
package main
import (
"net"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestFlaky(t *testing.T) {
// Mock an API
http.HandleFunc("/foo/bar", func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("foobar"))
require.NoError(t, err)
})
go func() {
require.NoError(t, http.ListenAndServe("localhost:7777", nil))
}()
// Wait (up to 1 second) for the mocked API to be available
conn, err := net.DialTimeout("tcp", "localhost:7777", time.Second)
require.NoError(t, err)
require.NoError(t, conn.Close())
}
但是,从错误之后的行中,我收到以下错误(仅在CI环境中):require.NoError()DialTimeout
--- FAIL: TestFlaky (0.00s)
main_test.go:24:
Error Trace: main_test.go:24
Error: Received unexpected error:
dial tcp [::1]:7777: connect: connection refused
Test: TestFlaky
由于测试立即失败,我猜这不是调整超时的问题。我应该如何使这个测试不片状?我正在考虑将最后三行替换为类似于以下内容的行:require.Eventually
var conn net.Conn
require.Eventually(t, func() bool {
var err error
conn, err = net.DialTimeout("tcp", "localhost:7777", time.Second)
if err != nil {
t.Logf("DialTimeout error: %v. Retrying...", err)
return false
}
return true
}, time.Second, 100*time.Millisecond)
require.NoError(t, conn.Close())
这是否足以消除测试剥落?
蛊毒传说
相关分类