Skip to content

Commit

Permalink
Fix feedback - move to using httptest for server.
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrencegripper committed Nov 6, 2018
1 parent ee806af commit c38c66b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
12 changes: 8 additions & 4 deletions leaktest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
Expand All @@ -20,6 +21,9 @@ func (tr *testReporter) Errorf(format string, args ...interface{}) {
tr.msg = fmt.Sprintf(format, args...)
}

// Client for the TestServer
var testServer *httptest.Server

func TestCheck(t *testing.T) {
leakyFuncs := []struct {
f func()
Expand Down Expand Up @@ -81,7 +85,7 @@ func TestCheck(t *testing.T) {
DisableKeepAlives: true,
}
client := &http.Client{Transport: tr}
_, err := client.Get("http://localhost:8091")
_, err := client.Get(testServer.URL)
if err != nil {
t.Error(err)
}
Expand All @@ -95,7 +99,7 @@ func TestCheck(t *testing.T) {
DisableKeepAlives: false,
}
client := &http.Client{Transport: tr}
_, err := client.Get("http://localhost:8091")
_, err := client.Get(testServer.URL)
if err != nil {
t.Error(err)
}
Expand All @@ -106,7 +110,7 @@ func TestCheck(t *testing.T) {
// Start our keep alive server for keep alive tests
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go startKeepAliveEnabledServer(ctx)
testServer = startKeepAliveEnabledServer(ctx)

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

go time.Sleep(1500 * time.Millisecond)
time.Sleep(750 * time.Millisecond)
Expand Down
32 changes: 10 additions & 22 deletions leaktest_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package leaktest

import (
"context"
"log"
"net/http"
"net/http/httptest"
"time"
)

Expand All @@ -13,30 +13,18 @@ func index() http.Handler {
})
}

func startKeepAliveEnabledServer(ctx context.Context) {
router := http.NewServeMux()
router.Handle("/", index())

server := &http.Server{
Addr: ":8091",
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
func startKeepAliveEnabledServer(ctx context.Context) *httptest.Server {
server := httptest.NewUnstartedServer(index())
server.Config.ReadTimeout = 5 * time.Second
server.Config.WriteTimeout = 10 * time.Second
server.Config.IdleTimeout = 15 * time.Second
server.Config.SetKeepAlivesEnabled(true)

server.Start()
go func() {
<-ctx.Done()

server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
server.Close()
}()

log.Println("Server is ready to handle requests at", server.Addr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on %s: %v\n", server.Addr, err)
}

log.Println("Server stopped")
return server
}

0 comments on commit c38c66b

Please sign in to comment.