Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Last active April 5, 2016 16:40
Show Gist options
  • Save jochasinga/a7344f5eb1c14eda51d2fc3e7053f639 to your computer and use it in GitHub Desktop.
Save jochasinga/a7344f5eb1c14eda51d2fc3e7053f639 to your computer and use it in GitHub Desktop.
Using Switcher in Go Relay package
// Error handling omitted for brevity
var (
helloMarsHandler = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello Mars client!")
}
goodDayHandler = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Good day client!")
}
palomaHandler = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Paloma client!")
}
)
func TestSwitcher(t *testing.T) {
Convey("GIVEN a few backend servers", t, func() {
backends := []HTTPTestServer{
httptest.NewServer(http.HandlerFunc(helloMarsHandler)),
httptest.NewServer(http.HandlerFunc(goodDayHandler)),
httptest.NewServer(http.HandlerFunc(palomaHandler)),
}
Convey("GIVEN a frontend switcher", func() {
latency := time.Duration(0)
sw := relay.NewSwitcher(latency, backends)
Convey("WITH first request", func() {
resp, _ := http.Get(sw.URL)
Convey("EXPECT `Hello Mars client!` from the first server", func() {
b, _ := ioutil.ReadAll(resp.Body)
So(string(b), ShouldEqual, "Hello Mars client!")
})
})
Convey("WITH second request", func() {
resp, _ := http.Get(sw.URL)
Convey("EXPECT `Good day client!` from the second server", func() {
b, _ := ioutil.ReadAll(resp.Body)
So(string(b), ShouldEqual, "Good day client!")
})
})
Convey("WITH third request", func() {
resp, _ := http.Get(sw.URL)
Convey("EXPECT `Paloma client!` from the third server", func() {
b, _ := ioutil.ReadAll(resp.Body)
So(string(b), ShouldEqual, "Paloma client!")
})
})
Convey("WITH forth request", func() {
resp, _ := http.Get(sw.URL)
Convey("EXPECT `Hello Mars client!` from the first server", func() {
b, _ := ioutil.ReadAll(resp.Body)
So(string(b), ShouldEqual, "Hello Mars client!")
})
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment