Skip to content

Commit

Permalink
Updated realtime test for fallback hosts, removed code for possible f…
Browse files Browse the repository at this point in the history
…lakiness
  • Loading branch information
sacOO7 committed Aug 24, 2024
1 parent 22d35c9 commit 63cb110
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
5 changes: 5 additions & 0 deletions ably/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ type DurationFromMsecs = durationFromMsecs
type ProtoErrorInfo = errorInfo
type ProtoFlag = protoFlag
type ProtocolMessage = protocolMessage
type WebsocketErr = websocketErr

func (w *WebsocketErr) HttpResp() *http.Response {
return w.resp
}

const (
DefaultCipherKeyLength = defaultCipherKeyLength
Expand Down
52 changes: 46 additions & 6 deletions ably/realtime_client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,44 +272,84 @@ func TestRealtime_RTN17_Integration_HostFallback_Internal_Server_Error(t *testin
serverURL, err := url.Parse(server.URL)
assert.NoError(t, err)

fallbackHost := "sandbox-a-fallback.ably-realtime.com"
connAttempts := 0

app, realtime := ablytest.NewRealtime(
ably.WithAutoConnect(false),
ably.WithTLS(false),
ably.WithUseTokenAuth(true),
ably.WithFallbackHosts([]string{"sandbox-a-fallback.ably-realtime.com"}),
ably.WithFallbackHosts([]string{fallbackHost}),
ably.WithDial(func(protocol string, u *url.URL, timeout time.Duration) (ably.Conn, error) {
connAttempts += 1
conn, err := ably.DialWebsocket(protocol, u, timeout)
if connAttempts == 1 {
assert.Equal(t, serverURL.Host, u.Host)
var websocketErr *ably.WebsocketErr
assert.ErrorAs(t, err, &websocketErr)
assert.Equal(t, http.StatusInternalServerError, websocketErr.HttpResp().StatusCode)
} else {
assert.NoError(t, err)
assert.Equal(t, fallbackHost, u.Hostname())
}
return conn, err
}),
ably.WithRealtimeHost(serverURL.Host))

defer safeclose(t, ablytest.FullRealtimeCloser(realtime), app)

err = ablytest.Wait(ablytest.ConnWaiter(realtime, realtime.Connect, ably.ConnectionEventConnected), nil)
assert.NoError(t, err)

assert.Equal(t, "sandbox-a-fallback.ably-realtime.com", realtime.Rest().ActiveRealtimeHost())
assert.Equal(t, 2, connAttempts)
assert.Equal(t, fallbackHost, realtime.Rest().ActiveRealtimeHost())
}

func TestRealtime_RTN17_Integration_HostFallback_Timeout(t *testing.T) {
timedOut := make(chan bool)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
<-timedOut
w.WriteHeader(http.StatusSwitchingProtocols)
}))
defer server.Close()
serverURL, err := url.Parse(server.URL)
assert.NoError(t, err)

fallbackHost := "sandbox-a-fallback.ably-realtime.com"
requestTimeout := 2 * time.Second
connAttempts := 0

app, realtime := ablytest.NewRealtime(
ably.WithAutoConnect(false),
ably.WithTLS(false),
ably.WithUseTokenAuth(true),
ably.WithFallbackHosts([]string{"sandbox-a-fallback.ably-realtime.com"}),
ably.WithRealtimeRequestTimeout(2*time.Second),
ably.WithFallbackHosts([]string{fallbackHost}),
ably.WithRealtimeRequestTimeout(requestTimeout),
ably.WithDial(func(protocol string, u *url.URL, timeout time.Duration) (ably.Conn, error) {
connAttempts += 1
assert.Equal(t, requestTimeout, timeout)
conn, err := ably.DialWebsocket(protocol, u, timeout)
if connAttempts == 1 {
assert.Equal(t, serverURL.Host, u.Host)
var timeoutError net.Error
assert.ErrorAs(t, err, &timeoutError)
assert.True(t, timeoutError.Timeout())
timedOut <- true
} else {
assert.NoError(t, err)
assert.Equal(t, fallbackHost, u.Hostname())
}
return conn, err
}),
ably.WithRealtimeHost(serverURL.Host))

defer safeclose(t, ablytest.FullRealtimeCloser(realtime), app)

err = ablytest.Wait(ablytest.ConnWaiter(realtime, realtime.Connect, ably.ConnectionEventConnected), nil)
assert.NoError(t, err)

assert.Equal(t, "sandbox-a-fallback.ably-realtime.com", realtime.Rest().ActiveRealtimeHost())
assert.Equal(t, 2, connAttempts)
assert.Equal(t, fallbackHost, realtime.Rest().ActiveRealtimeHost())
}

func checkUnique(ch chan string, typ string, n int) error {
Expand Down

0 comments on commit 63cb110

Please sign in to comment.