Last active
April 5, 2016 16:40
-
-
Save jochasinga/a7344f5eb1c14eda51d2fc3e7053f639 to your computer and use it in GitHub Desktop.
Using Switcher in Go Relay package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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