Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Last active April 5, 2016 16:37
Show Gist options
  • Save jochasinga/283a3f69e3336fbeeaaeaa1e8c6b6a88 to your computer and use it in GitHub Desktop.
Save jochasinga/283a3f69e3336fbeeaaeaa1e8c6b6a88 to your computer and use it in GitHub Desktop.
Example of using Relay Proxy to simulate latent connection in testing
// Error handling omitted for brevity
func TestGet(t *testing.T) {
Convey("GIVEN the test server", t, func() {
ts := httptest.NewServer(http.HandlerFunc(handler))
Convey("GIVEN a slow connection which takes 4s to-from the server", func() {
// this delay is per trip, meaning it doubles for a round trip
delay := time.Duration(2) * time.Second
conn := relay.NewProxy(delay, ts)
Convey("WITH a client that times out after 3s", func() {
timeout := time.Duration(3) * time.Second
client := &Client{Timeout: timeout}
Convey("EXPECT the request to timeout in ~3s", func() {
start := time.Now()
resp, _ := client.Get(conn.URL)
elapsed := time.Since(start)
So(elapsed, ShouldAlmostEqual, timeout)
})
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment