Simplify test a bit
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"log"
|
"log"
|
||||||
"io/ioutil"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,22 +32,8 @@ func NewHandler(responseWriter http.ResponseWriter, request *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func verifyRequest(expectedResponse string) {
|
func verifyRequest(expectedResponse string) {
|
||||||
resp, err := http.Get("http://localhost:3333")
|
targetUrl := "http://localhost:3333"
|
||||||
if (err != nil) {
|
testRequest(targetUrl, expectedResponse)
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startServer(mr *mutableRouter) {
|
func startServer(mr *mutableRouter) {
|
||||||
@@ -86,12 +71,8 @@ func TestMutableMux(t *testing.T) {
|
|||||||
|
|
||||||
// continuously make requests, panic if any fails
|
// continuously make requests, panic if any fails
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
q1 := make(chan bool)
|
q := make(chan bool)
|
||||||
go spamServer(q1)
|
go spamServer(q)
|
||||||
q2 := make(chan bool)
|
|
||||||
go spamServer(q2)
|
|
||||||
q3 := make(chan bool)
|
|
||||||
go spamServer(q3)
|
|
||||||
|
|
||||||
time.Sleep(5 * time.Millisecond)
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
|
||||||
@@ -109,9 +90,6 @@ func TestMutableMux(t *testing.T) {
|
|||||||
log.Print("Verify new handler")
|
log.Print("Verify new handler")
|
||||||
verifyRequest("new handler")
|
verifyRequest("new handler")
|
||||||
|
|
||||||
time.Sleep(5 * time.Millisecond)
|
q <- true
|
||||||
q1 <- true
|
|
||||||
q2 <- true
|
|
||||||
q3 <- true
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user