Skip to content

Commit

Permalink
refactored hostfallback tests, fixed failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Jan 19, 2024
1 parent 220e2ef commit e61d1e4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 55 deletions.
4 changes: 3 additions & 1 deletion ably/http_paginated_response_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func TestHTTPPaginatedFallback(t *testing.T) {
app, err := ablytest.NewSandbox(nil)
assert.NoError(t, err)
defer app.Close()
opts := app.Options(ably.WithUseBinaryProtocol(false), ably.WithRESTHost("ably.invalid"), ably.WithFallbackHostsUseDefault(true))
opts := app.Options(ably.WithUseBinaryProtocol(false),
ably.WithRESTHost("ably.invalid"),
ably.WithFallbackHosts(nil))
client, err := ably.NewREST(opts...)
assert.NoError(t, err)
t.Run("request_time", func(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion ably/realtime_channel_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/ably/ably-go/ably"
"github.com/ably/ably-go/ably/internal/ablyutil"
"github.com/ably/ably-go/ablytest"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -320,7 +321,7 @@ func TestRealtimeChannel_ShouldReturnErrorIfReadLimitExceeded(t *testing.T) {
assert.NoError(t, err, "client2:.Subscribe(context.Background())=%v", err)
defer unsub2()

messageWith2MbSize := ablytest.GenerateRandomString(2048)
messageWith2MbSize := ablyutil.GenerateRandomString(2048)
err = channel1.Publish(context.Background(), "hello", messageWith2MbSize)
assert.NoError(t, err, "client1: Publish()=%v", err)

Expand Down
58 changes: 18 additions & 40 deletions ably/rest_client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TestRest_RSC7_AblyAgent(t *testing.T) {
})
}

func TestRest_hostfallback(t *testing.T) {
func TestRest_RSC15_HostFallback(t *testing.T) {

app, err := ablytest.NewSandbox(nil)
assert.NoError(t, err)
Expand All @@ -329,7 +329,7 @@ func TestRest_hostfallback(t *testing.T) {
var retryCount int
var hosts []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hosts = append(hosts, r.Host)
hosts = append(hosts, strings.Split(r.Host, ":")[0])
retryCount++
w.WriteHeader(http.StatusInternalServerError)
}))
Expand All @@ -340,39 +340,27 @@ func TestRest_hostfallback(t *testing.T) {
assert.Error(t, err, "expected an error")
return retryCount, hosts
}
t.Run("RSC15d RSC15a must use alternative host", func(t *testing.T) {

t.Run("RSC15a, RSC15b, RSC15d, RSC15g3: must use alternative host", func(t *testing.T) {
options := []ably.ClientOption{
ably.WithFallbackHosts(ably.DefaultFallbackHosts()),
ably.WithTLS(false),
ably.WithEnvironment(""), // remove default sandbox env
ably.WithHTTPMaxRetryCount(10),
ably.WithUseTokenAuth(true),
}
retryCount, hosts := runTestServer(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
}
assert.Equal(t, 6, retryCount) // 1 primary and 5 default fallback hosts
assert.Equal(t, "rest.ably.io", hosts[0]) // primary host
assertSubset(t, ably.DefaultFallbackHosts(), hosts[1:]) // remaining fallback hosts
assertUnique(t, hosts) // ensure all picked fallbacks are unique
})

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)
hosts = append(hosts, strings.Split(r.Host, ":")[0])
retryCount++
time.Sleep(2 * time.Second)
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -386,8 +374,8 @@ func TestRest_hostfallback(t *testing.T) {
}
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)")
err1 := client.Channels.Get("test").Publish(context.Background(), "ping", "pong")
assert.Contains(t, err1.Error(), "context deadline exceeded (Client.Timeout exceeded while awaiting headers)")
return retryCount, hosts
}

Expand All @@ -396,25 +384,15 @@ func TestRest_hostfallback(t *testing.T) {
options := []ably.ClientOption{
ably.WithFallbackHosts(ably.DefaultFallbackHosts()),
ably.WithTLS(false),
ably.WithEnvironment(""), // remove default sandbox env
ably.WithHTTPMaxRetryCount(10),
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
}
assert.Equal(t, 6, retryCount) // 1 primary and 5 default fallback hosts
assert.Equal(t, "rest.ably.io", hosts[0]) // primary host
assertSubset(t, ably.DefaultFallbackHosts(), hosts[1:]) // remaining fallback hosts
assertUnique(t, hosts) // ensure all picked fallbacks are unique
})

t.Run("rsc15b", func(t *testing.T) {
Expand Down
13 changes: 0 additions & 13 deletions ablytest/ablytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -210,15 +209,3 @@ func TimeFuncs(afterCalls chan<- AfterCall) (

return now, after
}

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))

func GenerateRandomString(length int) string {
b := make([]byte, length)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}

0 comments on commit e61d1e4

Please sign in to comment.