未验证 提交 86769e59 编写于 作者: W wei liu 提交者: GitHub

fix test unstable (#19214)

Signed-off-by: NWei Liu <wei.liu@zilliz.com>
Signed-off-by: NWei Liu <wei.liu@zilliz.com>
上级 539585e9
...@@ -38,6 +38,7 @@ import ( ...@@ -38,6 +38,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest"
"testing" "testing"
"time" "time"
...@@ -123,12 +124,8 @@ func TestLevelGetterAndSetter(t *testing.T) { ...@@ -123,12 +124,8 @@ func TestLevelGetterAndSetter(t *testing.T) {
} }
func TestUpdateLogLevelThroughHttp(t *testing.T) { func TestUpdateLogLevelThroughHttp(t *testing.T) {
httpServer := &http.Server{Addr: ":9081", ReadHeaderTimeout: time.Second * 3} httpServer := httptest.NewServer(nil)
go func() { defer httpServer.Close()
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
Fatal(err.Error())
}
}()
SetLevel(zap.DebugLevel) SetLevel(zap.DebugLevel)
assert.Equal(t, zap.DebugLevel, GetLevel()) assert.Equal(t, zap.DebugLevel, GetLevel())
...@@ -145,13 +142,14 @@ func TestUpdateLogLevelThroughHttp(t *testing.T) { ...@@ -145,13 +142,14 @@ func TestUpdateLogLevelThroughHttp(t *testing.T) {
Fatal(err.Error()) Fatal(err.Error())
} }
req, err := http.NewRequest(http.MethodPut, "http://localhost:9081/log/level", bytes.NewBuffer(payload)) url := httpServer.URL + "/log/level"
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(payload))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
if err != nil { if err != nil {
Fatal(err.Error()) Fatal(err.Error())
} }
client := &http.Client{} client := httpServer.Client()
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
Fatal(err.Error()) Fatal(err.Error())
...@@ -164,12 +162,6 @@ func TestUpdateLogLevelThroughHttp(t *testing.T) { ...@@ -164,12 +162,6 @@ func TestUpdateLogLevelThroughHttp(t *testing.T) {
} }
assert.Equal(t, "{\"level\":\"error\"}\n", string(body)) assert.Equal(t, "{\"level\":\"error\"}\n", string(body))
assert.Equal(t, zap.ErrorLevel, GetLevel()) assert.Equal(t, zap.ErrorLevel, GetLevel())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
Fatal(err.Error())
}
} }
func TestSampling(t *testing.T) { func TestSampling(t *testing.T) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册