From a33f4e48e97aefc877ff0dd4bfd10afe823f80c4 Mon Sep 17 00:00:00 2001 From: Soam Vasani Date: Tue, 30 Aug 2016 16:40:20 -0700 Subject: [PATCH] Simplify test a bit --- src/router/mutablemux_test.go | 34 ++++++---------------------------- src/router/testUtil.go | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 src/router/testUtil.go diff --git a/src/router/mutablemux_test.go b/src/router/mutablemux_test.go index 1b1417ba..a20e5b8f 100644 --- a/src/router/mutablemux_test.go +++ b/src/router/mutablemux_test.go @@ -21,7 +21,6 @@ import ( "net/http" "github.com/gorilla/mux" "log" - "io/ioutil" "time" ) @@ -33,22 +32,8 @@ func NewHandler(responseWriter http.ResponseWriter, request *http.Request) { } func verifyRequest(expectedResponse string) { - resp, err := http.Get("http://localhost:3333") - if (err != nil) { - log.Panic("failed make get request") - } - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if (err != nil) { - log.Panic("failed to read response") - } - - bodyStr := string(body) - log.Printf("Server responded with %v", bodyStr) - if (bodyStr != expectedResponse) { - log.Panic("Unexpected response") - } + targetUrl := "http://localhost:3333" + testRequest(targetUrl, expectedResponse) } func startServer(mr *mutableRouter) { @@ -86,12 +71,8 @@ func TestMutableMux(t *testing.T) { // continuously make requests, panic if any fails time.Sleep(100 * time.Millisecond) - q1 := make(chan bool) - go spamServer(q1) - q2 := make(chan bool) - go spamServer(q2) - q3 := make(chan bool) - go spamServer(q3) + q := make(chan bool) + go spamServer(q) time.Sleep(5 * time.Millisecond) @@ -108,10 +89,7 @@ func TestMutableMux(t *testing.T) { // connect and verify the new handler log.Print("Verify new handler") verifyRequest("new handler") - - time.Sleep(5 * time.Millisecond) - q1 <- true - q2 <- true - q3 <- true + + q <- true time.Sleep(100 * time.Millisecond) } diff --git a/src/router/testUtil.go b/src/router/testUtil.go new file mode 100644 index 00000000..cd88dc0d --- /dev/null +++ b/src/router/testUtil.go @@ -0,0 +1,27 @@ +package router + +import ( + "net/http" + "log" + "io/ioutil" +) + +func testRequest(targetUrl string, expectedResponse string) { + resp, err := http.Get(targetUrl) + if (err != nil) { + log.Panic("failed make get request") + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if (err != nil) { + log.Panic("failed to read response") + } + + bodyStr := string(body) + log.Printf("Server responded with %v", bodyStr) + if (bodyStr != expectedResponse) { + log.Panic("Unexpected response") + } +} +