Skip to content

Instantly share code, notes, and snippets.

@josephwoodward
Last active December 5, 2021 04:52
Show Gist options
  • Save josephwoodward/0e9856ec3f69c2638c1d58df4e3ecb8e to your computer and use it in GitHub Desktop.
Save josephwoodward/0e9856ec3f69c2638c1d58df4e3ecb8e to your computer and use it in GitHub Desktop.
func TestSlowConnection(t *testing.T) {
// Arrange
// Use the client to get our proxy configured on test setup
proxy, err := client.Proxy("upstream_api")
if err != nil {
t.Fatalf("fetching proxy 'upstream_api': %v", err)
}
// Add a 'toxic' to introduce 3 seconds latency into the upstream request
proxy.AddToxic("latency", "latency", "upstream", 1.0, toxiproxy.Attributes{
"latency": 3000,
})
// Delete the proxy after use for test isolation
defer proxy.RemoveToxic("latency")
// Act
start := time.Now()
// Make our request to our dependant service
resp, err := http.Get("http://" + proxy.Listen)
// Assert
if time.Since(start) < 3000*time.Millisecond {
t.Fatalf("request completed sooner than expected")
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("wanted 200, got %v", resp.StatusCode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment