Skip to content

Commit

Permalink
Added a test to check for alternative host on timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Jan 19, 2024
1 parent 7ed5ace commit bb48043
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions ably/rest_client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,7 @@ func TestRest_hostfallback(t *testing.T) {
client, err := ably.NewREST(app.Options(append(options, ably.WithHTTPClient(newHTTPClientMock(server)))...)...)
assert.NoError(t, err)
err = client.Channels.Get("test").Publish(context.Background(), "ping", "pong")
assert.Error(t, err,
"expected an error")
assert.Error(t, err, "expected an error")
return retryCount, hosts
}
t.Run("RSC15d RSC15a must use alternative host", func(t *testing.T) {
Expand Down Expand Up @@ -368,6 +367,56 @@ func TestRest_hostfallback(t *testing.T) {
uniq[h] = true
}
})

runTestServerWithRequestTimeout := func(t *testing.T, options []ably.ClientOption) (int, []string) {
var retryCount int
var hosts []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hosts = append(hosts, r.Host)
retryCount++
time.Sleep(2 * time.Second)
w.WriteHeader(http.StatusInternalServerError)
}))
defer server.Close()
httpClientMock := &http.Client{
Timeout: 1 * time.Second,
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) { return url.Parse(server.URL) },
},
}
client, err := ably.NewREST(app.Options(append(options, ably.WithHTTPClient(httpClientMock))...)...)
assert.NoError(t, err)
err = client.Channels.Get("test").Publish(context.Background(), "ping", "pong")
assert.Contains(t, err.Error(), "context deadline exceeded (Client.Timeout exceeded while awaiting headers)")
return retryCount, hosts
}

t.Run("RSC15l2 must use alternative host on timeout", func(t *testing.T) {

options := []ably.ClientOption{
ably.WithFallbackHosts(ably.DefaultFallbackHosts()),
ably.WithTLS(false),
ably.WithUseTokenAuth(true),
}
retryCount, hosts := runTestServerWithRequestTimeout(t, options)
assert.Equal(t, 4, retryCount, "expected 4 http calls got %d", retryCount)
// make sure the host header is set. Since we are using defaults from the spec
// the hosts should be in [a..e].ably-realtime.com
expect := strings.Join(ably.DefaultFallbackHosts(), ", ")
for _, host := range hosts[1:] {
assert.Contains(t, expect, host, "expected %s got be in %s", host, expect)
}

// ensure all picked fallbacks are unique
uniq := make(map[string]bool)
for _, h := range hosts {
_, ok := uniq[h]
assert.False(t, ok,
"duplicate fallback %s", h)
uniq[h] = true
}
})

t.Run("rsc15b", func(t *testing.T) {
t.Run("must not occur when default rest.ably.io is overridden", func(t *testing.T) {

Expand Down

0 comments on commit bb48043

Please sign in to comment.