From d55d7b93978e1a10b6588962f999d3ebd6d65a3d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 8 Jun 2017 20:35:02 +0000 Subject: [PATCH] net/http: simplify recently added TestServeTLS TestServeTLS was added in CL 44074, merged today. This cleans up the test a little. Updates #13228 Change-Id: I6efd798fe5fa015a34addbf60ae26919a1ed283e Reviewed-on: https://go-review.googlesource.com/45152 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/net/http/serve_test.go | 58 ++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 5ed7895093..7137599c42 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -1361,9 +1361,7 @@ func TestServeTLS(t *testing.T) { // Not parallel: uses global test hooks. defer afterTest(t) defer SetTestHookServerServe(nil) - var ok bool - const maxTries = 5 - var ln net.Listener + cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey) if err != nil { t.Fatal(err) @@ -1372,38 +1370,30 @@ func TestServeTLS(t *testing.T) { Certificates: []tls.Certificate{cert}, } -Try: - for try := 0; try < maxTries; try++ { - ln = newLocalListener(t) - addr := ln.Addr().String() - t.Logf("Got %v", addr) - lnc := make(chan net.Listener, 1) - SetTestHookServerServe(func(s *Server, ln net.Listener) { - lnc <- ln - }) - handler := HandlerFunc(func(w ResponseWriter, r *Request) { - }) - s := &Server{ - Addr: addr, - TLSConfig: tlsConf, - Handler: handler, - } - errc := make(chan error, 1) - go func() { errc <- s.ServeTLS(ln, "", "") }() - select { - case err := <-errc: - t.Logf("On try #%v: %v", try+1, err) - continue - case ln = <-lnc: - ok = true - t.Logf("Listening on %v", ln.Addr().String()) - break Try - } - } - if !ok { - t.Fatalf("Failed to start up after %d tries", maxTries) - } + ln := newLocalListener(t) defer ln.Close() + addr := ln.Addr().String() + + serving := make(chan bool, 1) + SetTestHookServerServe(func(s *Server, ln net.Listener) { + serving <- true + }) + handler := HandlerFunc(func(w ResponseWriter, r *Request) {}) + s := &Server{ + Addr: addr, + TLSConfig: tlsConf, + Handler: handler, + } + errc := make(chan error, 1) + go func() { errc <- s.ServeTLS(ln, "", "") }() + select { + case err := <-errc: + t.Fatalf("ServeTLS: %v", err) + case <-serving: + case <-time.After(5 * time.Second): + t.Fatal("timeout") + } + c, err := tls.Dial("tcp", ln.Addr().String(), &tls.Config{ InsecureSkipVerify: true, NextProtos: []string{"h2", "http/1.1"},