From e61d1e46bf4f3a0547955f5f4014d7785e9f5779 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 19 Jan 2024 19:58:11 +0530 Subject: [PATCH] refactored hostfallback tests, fixed failing tests --- ...ttp_paginated_response_integration_test.go | 4 +- ably/realtime_channel_integration_test.go | 3 +- ably/rest_client_integration_test.go | 58 ++++++------------- ablytest/ablytest.go | 13 ----- 4 files changed, 23 insertions(+), 55 deletions(-) diff --git a/ably/http_paginated_response_integration_test.go b/ably/http_paginated_response_integration_test.go index ed196accf..d47285b29 100644 --- a/ably/http_paginated_response_integration_test.go +++ b/ably/http_paginated_response_integration_test.go @@ -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) { diff --git a/ably/realtime_channel_integration_test.go b/ably/realtime_channel_integration_test.go index 88dadd5d5..c5a0ec0a5 100644 --- a/ably/realtime_channel_integration_test.go +++ b/ably/realtime_channel_integration_test.go @@ -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" @@ -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) diff --git a/ably/rest_client_integration_test.go b/ably/rest_client_integration_test.go index 25e101666..f4a3afbe0 100644 --- a/ably/rest_client_integration_test.go +++ b/ably/rest_client_integration_test.go @@ -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) @@ -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) })) @@ -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) @@ -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 } @@ -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) { diff --git a/ablytest/ablytest.go b/ablytest/ablytest.go index dba905ad0..fc7b83630 100644 --- a/ablytest/ablytest.go +++ b/ablytest/ablytest.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "math/rand" "net/http" "os" "reflect" @@ -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) -}