49

我正在尝试调试我收到的一个非常不寻常的错误,因为我编写了一个简单的 REST 库。

我正在使用标准的 net/http 包来发出 Get、Post、Put、Delete 请求,但是当我连续发出多个请求时,我的测试偶尔会失败。我的测试如下所示:

func TestGetObject(t *testing.T) {
    firebaseRoot := New(firebase_url)
    body, err := firebaseRoot.Get("1")
    if err != nil {
        t.Errorf("Error: %s", err)
    }
    t.Logf("%q", body)
}  

func TestPushObject(t *testing.T) {
    firebaseRoot := New(firebase_url)
    msg := Message{"testing", "1..2..3"}
    body, err := firebaseRoot.Push("/", msg)
    if err != nil {
        t.Errorf("Error: %s", err)
    }
    t.Logf("%q", body)
}

我提出这样的要求:

// Send HTTP Request, return data
func (f *firebaseRoot) SendRequest(method string, path string, body io.Reader) ([]byte, error) {
url := f.BuildURL(path)

// create a request
req, err := http.NewRequest(method, url, body)
if err != nil {
    return nil, err
}

// send JSON to firebase
resp, err := http.DefaultClient.Do(req)
if err != nil {
    return nil, err
}

if resp.StatusCode != http.StatusOK {
    return nil, fmt.Errorf("Bad HTTP Response: %v", resp.Status)
}

defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
    return nil, err
}

return b, nil
} 

有时它可以工作,但大多数时候我会遇到 1 或 2 次失败:

--- FAIL: TestGetObject (0.00 seconds)
firebase_test.go:53: Error: Get https://go-firebase-test.firebaseio.com/1.json: EOF
firebase_test.go:55: ""

--- FAIL: TestPushObject (0.00 seconds)
firebase_test.go:63: Error: Post https://go-firebase-test.firebaseio.com/.json: EOF
firebase_test.go:65: ""
FAIL
exit status 1
FAIL    github.com/chourobin/go.firebase    3.422s

当我提出超过 1 个请求时会发生故障。如果我注释掉除 PUT 请求之外的所有内容,则测试始终通过。一旦我包含第二个测试,例如 GET,一个或另一个失败(有时都通过)。

4

4 回答 4

75

我可靠地经历了这一点。您需要将 Req.Close 设置为 true (示例中使用的延迟 resp.Body.Close() 语法是不够的)。像这样:

client := &http.Client{}
req, err := http.NewRequest(method, url, httpBody)

// NOTE this !!
req.Close = true

req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth("user", "pass")
resp, err := client.Do(req)
if err != nil {
    // whatever
}
defer resp.Body.Close()

response, err = ioutil.ReadAll(resp.Body)
if err != nil {
    // Whatever
}
于 2013-09-25T13:17:38.517 回答
35

我同意您不应该在单元测试中访问外部服务器的断言,为什么不直接使用内置的 http.Server 并提供您想要测试的内容。(实际上有 httptest 包可以帮助解决这个问题)

我最近在尝试抓取站点地图时遇到了同样的问题,这就是我迄今为止发现的:

默认情况下,Go 将发送带有标头的请求Connection: Keep-Alive并保持连接以供重复使用。我遇到的问题是服务器Connection: Keep-Alive在响应标头中响应,然后立即关闭连接。

作为在这种情况下 go 如何实现连接的一些背景知识(您可以查看 net/http/transport.go 中的完整代码)。有两个goroutine,一个负责写,一个负责读(readLoopwriteLoop)在大多数情况下readLoop will detect a close on the socket, and close down the connection. The problem here occurs when you initiate another request before the readLoop actually detects the close, and the EOF that it reads get interpreted as an error for that new request rather than a close that occurred prior to the request.

Given that this is the case the reason why sleeping in between requests works is that it gives readLoop time to detect the close on the connection before your new request and shut it down, so that your new request will initiate a new connection. (And the reason why it would intermittently fail is because there is some amount code running between your requests and depending of scheduling of goroutines, sometimes the EOF will be properly handled before your next request, sometimes not). And the req.Close = true, solution works because it prevents the connection from being re-used.

There is a ticket related to this situation: https://code.google.com/p/go/issues/detail?id=4677 (and a dupe ticket that I created that allowed me to reliably reproduce this: https://code.google.com/p/go/issues/detail?id=8122)

于 2014-05-30T20:52:48.190 回答
23

我猜你的代码没有问题。问题的最可能原因是服务器正在关闭连接。速率限制是造成这种情况的一个可能原因。

您的测试不应依赖于非常脆弱且不密封的外部服务。相反,您应该考虑在本地启动测试服务器。

于 2013-07-18T22:56:42.593 回答
0

我对这个错误的体验是当我为我的 JSON API 输入完全空的输入时!

我应该{}作为空 JSON 发送,但我发送了 `` 所以这个错误发生了

于 2022-03-01T12:56:08.893 回答