fix: align channel copy and gateway connection state
This commit is contained in:
@@ -13,20 +13,16 @@ import (
|
||||
)
|
||||
|
||||
func TestConnectChannelCallbacksConnectedState(t *testing.T) {
|
||||
var callback ConnectionStateCallback
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/admin/gateway/connections" {
|
||||
t.Fatalf("unexpected callback path: %s", r.URL.Path)
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&callback); err != nil {
|
||||
t.Fatalf("decode callback: %v", err)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer api.Close()
|
||||
|
||||
handler := handlerWithDial(api.URL+"/api", func(context.Context, ConnectChannelCommand) error {
|
||||
return nil
|
||||
handler := handlerWithConnect(func(context.Context, ConnectChannelCommand) (ConnectionStateCallback, error) {
|
||||
return ConnectionStateCallback{
|
||||
ChannelID: "channel-1",
|
||||
ConnectionID: "channel-1:primary",
|
||||
Status: "connected",
|
||||
DesiredConnections: 2,
|
||||
CurrentConnections: 2,
|
||||
LastConnectedAt: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
LastHeartbeatAt: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
}, nil
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
@@ -36,6 +32,10 @@ func TestConnectChannelCallbacksConnectedState(t *testing.T) {
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("unexpected response status: %d body=%s", resp.Code, resp.Body.String())
|
||||
}
|
||||
var callback ConnectionStateCallback
|
||||
if err := json.Unmarshal(resp.Body.Bytes(), &callback); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if callback.Status != "connected" || callback.CurrentConnections != 2 || callback.DesiredConnections != 2 {
|
||||
t.Fatalf("unexpected callback state: %+v", callback)
|
||||
}
|
||||
@@ -48,17 +48,16 @@ func TestConnectChannelCallbacksConnectedState(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConnectChannelCallbacksFailedState(t *testing.T) {
|
||||
var callback ConnectionStateCallback
|
||||
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := json.NewDecoder(r.Body).Decode(&callback); err != nil {
|
||||
t.Fatalf("decode callback: %v", err)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer api.Close()
|
||||
|
||||
handler := handlerWithDial(api.URL+"/api", func(context.Context, ConnectChannelCommand) error {
|
||||
return errTestDial
|
||||
handler := handlerWithConnect(func(context.Context, ConnectChannelCommand) (ConnectionStateCallback, error) {
|
||||
return ConnectionStateCallback{
|
||||
ChannelID: "channel-1",
|
||||
ConnectionID: "channel-1:primary",
|
||||
Status: "failed",
|
||||
DesiredConnections: 2,
|
||||
CurrentConnections: 0,
|
||||
LastDisconnectedAt: time.Now().UTC().Format(time.RFC3339Nano),
|
||||
LastError: errTestConnect.Error(),
|
||||
}, nil
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
@@ -68,14 +67,18 @@ func TestConnectChannelCallbacksFailedState(t *testing.T) {
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("unexpected response status: %d body=%s", resp.Code, resp.Body.String())
|
||||
}
|
||||
var callback ConnectionStateCallback
|
||||
if err := json.Unmarshal(resp.Body.Bytes(), &callback); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if callback.Status != "failed" || callback.CurrentConnections != 0 || callback.LastError == "" {
|
||||
t.Fatalf("unexpected callback state: %+v", callback)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectChannelRejectsInvalidCommand(t *testing.T) {
|
||||
handler := handlerWithDial("", func(context.Context, ConnectChannelCommand) error {
|
||||
return nil
|
||||
handler := handlerWithConnect(func(context.Context, ConnectChannelCommand) (ConnectionStateCallback, error) {
|
||||
return ConnectionStateCallback{}, nil
|
||||
})
|
||||
|
||||
resp := httptest.NewRecorder()
|
||||
@@ -180,16 +183,16 @@ func TestRecoveryOverviewEndpointReturnsCombinedView(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type testDialError struct{}
|
||||
type testConnectError struct{}
|
||||
|
||||
func (testDialError) Error() string {
|
||||
return "dial failed"
|
||||
func (testConnectError) Error() string {
|
||||
return "connect failed"
|
||||
}
|
||||
|
||||
var errTestDial testDialError
|
||||
var errTestConnect testConnectError
|
||||
|
||||
func handlerWithDial(apiBaseURL string, dial DialFunc) http.Handler {
|
||||
return handlerWithServer(Server{APIBaseURL: apiBaseURL, Dial: dial})
|
||||
func handlerWithConnect(connect ConnectFunc) http.Handler {
|
||||
return handlerWithServer(Server{Connect: connect})
|
||||
}
|
||||
|
||||
func handlerWithServer(server Server) http.Handler {
|
||||
|
||||
Reference in New Issue
Block a user