Fix test shutdown

Mutable mux test occasionally panics because the server is shut down
before the client goroutine.  Fix this by shutting down the client
goroutines before shutting down the server.
This commit is contained in:
Soam Vasani
2016-08-29 15:30:04 -07:00
parent ad961d4f4d
commit 828fa5f88c
+23 -13
View File
@@ -55,16 +55,21 @@ func startServer(mr *mutableRouter) {
http.ListenAndServe(":3333", mr) http.ListenAndServe(":3333", mr)
} }
func spamServer() {
func spamServer(quit chan bool) {
i := 0 i := 0
for { for {
i = i + 1 select {
resp, err := http.Get("http://localhost:3333") case <- quit:
if (err != nil) { break
log.Panicf("failed make get request %v", i) default:
i = i + 1
resp, err := http.Get("http://localhost:3333")
if (err != nil) {
log.Panicf("failed make get request %v: %v", i, err)
}
resp.Body.Close()
} }
resp.Body.Close()
log.Printf("request count = %v", i)
} }
} }
@@ -80,9 +85,13 @@ func TestMutableMux(t *testing.T) {
go startServer(mr) go startServer(mr)
// continuously make requests, panic if any fails // continuously make requests, panic if any fails
go spamServer() time.Sleep(100 * time.Millisecond)
go spamServer() q1 := make(chan bool)
go spamServer() go spamServer(q1)
q2 := make(chan bool)
go spamServer(q2)
q3 := make(chan bool)
go spamServer(q3)
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)
@@ -101,7 +110,8 @@ func TestMutableMux(t *testing.T) {
verifyRequest("new handler") verifyRequest("new handler")
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)
q1 <- true
// all done q2 <- true
log.Print("ok") q3 <- true
time.Sleep(100 * time.Millisecond)
} }