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

checks: better goroutine handling for test tcp server
This commit is contained in:
Seth Hoenig 2022-08-02 10:43:48 -05:00 committed by GitHub
commit 6c171c54c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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
}
}
}()
}