Unverified Commit c69d4e06 authored by Seth Hoenig's avatar Seth Hoenig Committed by GitHub
Browse files

Merge pull request #13968 from hashicorp/b-fix-flakey-tcp-check-test

checks: better goroutine handling for test tcp server
parents 581390be 44c2bf14
Showing with 17 additions and 8 deletions
+17 -8
......@@ -311,7 +311,7 @@ func TestChecker_Do_TCP(t *testing.T) {
switch tc.tcpMode {
case "ok":
// simulate tcp server by listening
go tcpServer(ctx, tc.tcpPort)
tcpServer(t, ctx, tc.tcpPort)
case "hang":
// simulate tcp hang by setting an already expired context
timeout, stop := context.WithDeadline(ctx, now.Add(-1*time.Second))
......@@ -327,16 +327,25 @@ func TestChecker_Do_TCP(t *testing.T) {
}
}
func tcpServer(ctx context.Context, port int) {
// tcpServer will start a tcp listener that accepts connections and closes them.
// The caller can close the listener by cancelling ctx.
func tcpServer(t *testing.T, ctx context.Context, port int) {
var lc net.ListenConfig
l, _ := lc.Listen(ctx, "tcp", net.JoinHostPort(
l, err := lc.Listen(ctx, "tcp", net.JoinHostPort(
"localhost", fmt.Sprintf("%d", port),
))
defer func() {
must.NoError(t, err, must.Sprint("port", port))
t.Cleanup(func() {
_ = l.Close()
}()
con, _ := l.Accept()
defer func() {
_ = con.Close()
})
go func() {
// caller can stop us by cancelling ctx
for {
_, acceptErr := l.Accept()
if acceptErr != nil {
return
}
}
}()
}
Supports Markdown
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