mirror of
https://github.com/golang/go
synced 2024-11-18 11:24:41 -07:00
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: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
1570a27e78
commit
d55d7b9397
@ -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,17 +1370,15 @@ func TestServeTLS(t *testing.T) {
|
||||
Certificates: []tls.Certificate{cert},
|
||||
}
|
||||
|
||||
Try:
|
||||
for try := 0; try < maxTries; try++ {
|
||||
ln = newLocalListener(t)
|
||||
ln := newLocalListener(t)
|
||||
defer ln.Close()
|
||||
addr := ln.Addr().String()
|
||||
t.Logf("Got %v", addr)
|
||||
lnc := make(chan net.Listener, 1)
|
||||
|
||||
serving := make(chan bool, 1)
|
||||
SetTestHookServerServe(func(s *Server, ln net.Listener) {
|
||||
lnc <- ln
|
||||
})
|
||||
handler := HandlerFunc(func(w ResponseWriter, r *Request) {
|
||||
serving <- true
|
||||
})
|
||||
handler := HandlerFunc(func(w ResponseWriter, r *Request) {})
|
||||
s := &Server{
|
||||
Addr: addr,
|
||||
TLSConfig: tlsConf,
|
||||
@ -1392,18 +1388,12 @@ Try:
|
||||
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
|
||||
t.Fatalf("ServeTLS: %v", err)
|
||||
case <-serving:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("timeout")
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
t.Fatalf("Failed to start up after %d tries", maxTries)
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
c, err := tls.Dial("tcp", ln.Addr().String(), &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
NextProtos: []string{"h2", "http/1.1"},
|
||||
|
Loading…
Reference in New Issue
Block a user