Commit 1f0a1f4c authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: de-flake ConnState test

LGTM=josharian
R=golang-codereviews, josharian
CC=golang-codereviews
https://golang.org/cl/70270043
parent 281088b1
...@@ -2276,12 +2276,8 @@ func TestServerConnState(t *testing.T) { ...@@ -2276,12 +2276,8 @@ func TestServerConnState(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
type connIDAndState struct {
connID int
state ConnState
}
var mu sync.Mutex // guard stateLog and connID var mu sync.Mutex // guard stateLog and connID
var stateLog []connIDAndState var stateLog = map[int][]ConnState{}
var connID = map[net.Conn]int{} var connID = map[net.Conn]int{}
ts.Config.ConnState = func(c net.Conn, state ConnState) { ts.Config.ConnState = func(c net.Conn, state ConnState) {
...@@ -2296,7 +2292,7 @@ func TestServerConnState(t *testing.T) { ...@@ -2296,7 +2292,7 @@ func TestServerConnState(t *testing.T) {
id = len(connID) + 1 id = len(connID) + 1
connID[c] = id connID[c] = id
} }
stateLog = append(stateLog, connIDAndState{id, state}) stateLog[id] = append(stateLog[id], state)
} }
ts.Start() ts.Start()
...@@ -2308,27 +2304,18 @@ func TestServerConnState(t *testing.T) { ...@@ -2308,27 +2304,18 @@ func TestServerConnState(t *testing.T) {
mustGet(t, ts.URL+"/hijack") mustGet(t, ts.URL+"/hijack")
want := []connIDAndState{ want := map[int][]ConnState{
{1, StateNew}, 1: []ConnState{StateNew, StateActive, StateIdle, StateActive, StateClosed},
{1, StateActive}, 2: []ConnState{StateNew, StateActive, StateIdle, StateActive, StateClosed},
{1, StateIdle}, 3: []ConnState{StateNew, StateActive, StateHijacked},
{1, StateActive},
{1, StateClosed},
{2, StateNew},
{2, StateActive},
{2, StateIdle},
{2, StateActive},
{2, StateClosed},
{3, StateNew},
{3, StateActive},
{3, StateHijacked},
} }
logString := func(l []connIDAndState) string { logString := func(m map[int][]ConnState) string {
var b bytes.Buffer var b bytes.Buffer
for _, cs := range l { for id, l := range m {
fmt.Fprintf(&b, "[%d %s] ", cs.connID, cs.state) fmt.Fprintf(&b, "Conn %d: ", id)
for _, s := range l {
fmt.Fprintf(&b, "%s ", s)
}
} }
return b.String() return b.String()
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment