Skip to content

Commit c38c66b

Browse files
Fix feedback - move to using httptest for server.
1 parent ee806af commit c38c66b

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

leaktest_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"net/http/httptest"
89
"sync"
910
"testing"
1011
"time"
@@ -20,6 +21,9 @@ func (tr *testReporter) Errorf(format string, args ...interface{}) {
2021
tr.msg = fmt.Sprintf(format, args...)
2122
}
2223

24+
// Client for the TestServer
25+
var testServer *httptest.Server
26+
2327
func TestCheck(t *testing.T) {
2428
leakyFuncs := []struct {
2529
f func()
@@ -81,7 +85,7 @@ func TestCheck(t *testing.T) {
8185
DisableKeepAlives: true,
8286
}
8387
client := &http.Client{Transport: tr}
84-
_, err := client.Get("http://localhost:8091")
88+
_, err := client.Get(testServer.URL)
8589
if err != nil {
8690
t.Error(err)
8791
}
@@ -95,7 +99,7 @@ func TestCheck(t *testing.T) {
9599
DisableKeepAlives: false,
96100
}
97101
client := &http.Client{Transport: tr}
98-
_, err := client.Get("http://localhost:8091")
102+
_, err := client.Get(testServer.URL)
99103
if err != nil {
100104
t.Error(err)
101105
}
@@ -106,7 +110,7 @@ func TestCheck(t *testing.T) {
106110
// Start our keep alive server for keep alive tests
107111
ctx, cancel := context.WithCancel(context.Background())
108112
defer cancel()
109-
go startKeepAliveEnabledServer(ctx)
113+
testServer = startKeepAliveEnabledServer(ctx)
110114

111115
// this works because the running goroutine is left running at the
112116
// start of the next test case - so the previous leaks don't affect the
@@ -134,7 +138,7 @@ func TestCheck(t *testing.T) {
134138
// be based on time after the test finishes rather than time after the test's
135139
// start.
136140
func TestSlowTest(t *testing.T) {
137-
defer CheckTimeout(t, 1000*time.Millisecond)()
141+
defer CheckTimeout(t, 1000 * time.Millisecond)()
138142

139143
go time.Sleep(1500 * time.Millisecond)
140144
time.Sleep(750 * time.Millisecond)

leaktest_utils_test.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package leaktest
22

33
import (
44
"context"
5-
"log"
65
"net/http"
6+
"net/http/httptest"
77
"time"
88
)
99

@@ -13,30 +13,18 @@ func index() http.Handler {
1313
})
1414
}
1515

16-
func startKeepAliveEnabledServer(ctx context.Context) {
17-
router := http.NewServeMux()
18-
router.Handle("/", index())
19-
20-
server := &http.Server{
21-
Addr: ":8091",
22-
ReadTimeout: 5 * time.Second,
23-
WriteTimeout: 10 * time.Second,
24-
IdleTimeout: 15 * time.Second,
25-
}
16+
func startKeepAliveEnabledServer(ctx context.Context) *httptest.Server {
17+
server := httptest.NewUnstartedServer(index())
18+
server.Config.ReadTimeout = 5 * time.Second
19+
server.Config.WriteTimeout = 10 * time.Second
20+
server.Config.IdleTimeout = 15 * time.Second
21+
server.Config.SetKeepAlivesEnabled(true)
2622

23+
server.Start()
2724
go func() {
2825
<-ctx.Done()
29-
30-
server.SetKeepAlivesEnabled(false)
31-
if err := server.Shutdown(ctx); err != nil {
32-
log.Fatalf("Could not gracefully shutdown the server: %v\n", err)
33-
}
26+
server.Close()
3427
}()
3528

36-
log.Println("Server is ready to handle requests at", server.Addr)
37-
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
38-
log.Fatalf("Could not listen on %s: %v\n", server.Addr, err)
39-
}
40-
41-
log.Println("Server stopped")
29+
return server
4230
}

0 commit comments

Comments
 (0)