Commit d55d7b93 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 1570a27e
...@@ -1361,9 +1361,7 @@ func TestServeTLS(t *testing.T) { ...@@ -1361,9 +1361,7 @@ func TestServeTLS(t *testing.T) {
// Not parallel: uses global test hooks. // Not parallel: uses global test hooks.
defer afterTest(t) defer afterTest(t)
defer SetTestHookServerServe(nil) defer SetTestHookServerServe(nil)
var ok bool
const maxTries = 5
var ln net.Listener
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey) cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
...@@ -1372,38 +1370,30 @@ func TestServeTLS(t *testing.T) { ...@@ -1372,38 +1370,30 @@ func TestServeTLS(t *testing.T) {
Certificates: []tls.Certificate{cert}, Certificates: []tls.Certificate{cert},
} }
Try: ln := newLocalListener(t)
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)
}
defer ln.Close() 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{ c, err := tls.Dial("tcp", ln.Addr().String(), &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
NextProtos: []string{"h2", "http/1.1"}, NextProtos: []string{"h2", "http/1.1"},
......
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