Skip to content

Commit

Permalink
fix panic when connection to controller fails (#1415)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneDutto authored Oct 24, 2024
1 parent 5ea27ad commit f89b63e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/rpc/apiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ func (p *clientProxy) makeControllerConnection(ctx context.Context) error {
connWithMetadata, err := p.createControllerConn(ctx)
if err != nil {
createConnErr = errors.E(op, err)
return
}

p.msgs.controllerUUID = connWithMetadata.ControllerUUID
Expand Down
18 changes: 16 additions & 2 deletions internal/rpc/apiproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package rpc_test
import (
"context"
"encoding/json"
goerr "errors"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -69,6 +70,7 @@ func TestProxySocketsAdminFacade(t *testing.T) {
expectedClientResponse *message
expectedControllerMessage *message
oauthAuthenticatorError error
expectConnectTofail bool
}{{
about: "login device call - client gets response with both user code and verification uri",
messageToSend: message{
Expand Down Expand Up @@ -229,6 +231,12 @@ func TestProxySocketsAdminFacade(t *testing.T) {
ErrorCode: "unauthorized access",
},
oauthAuthenticatorError: errors.E(errors.CodeUnauthorized),
}, {
about: "connection to controller fails",
expectConnectTofail: true,
expectedClientResponse: &message{
Error: "controller connection error",
},
}}

for _, test := range tests {
Expand All @@ -249,6 +257,9 @@ func TestProxySocketsAdminFacade(t *testing.T) {
ConnClient: clientWebsocket,
TokenGen: &mockTokenGenerator{},
ConnectController: func(ctx context.Context) (rpc.WebsocketConnectionWithMetadata, error) {
if test.expectConnectTofail {
return rpc.WebsocketConnectionWithMetadata{}, goerr.New("controller connection error")
}
return rpc.WebsocketConnectionWithMetadata{
Conn: controllerWebsocket,
ModelName: "test model",
Expand All @@ -264,7 +275,11 @@ func TestProxySocketsAdminFacade(t *testing.T) {
go func() {
defer wg.Done()
err = rpc.ProxySockets(ctx, helpers)
c.Assert(err, qt.ErrorMatches, "Context cancelled")
if test.expectConnectTofail {
c.Assert(err, qt.ErrorMatches, "failed to connect to controller: controller connection error")
} else {
c.Assert(err, qt.ErrorMatches, "Context cancelled")
}
}()
data, err := json.Marshal(test.messageToSend)
c.Assert(err, qt.IsNil)
Expand All @@ -289,7 +304,6 @@ func TestProxySocketsAdminFacade(t *testing.T) {
wg.Wait()
t.Logf("completed test %s", t.Name())
})

}
}

Expand Down

0 comments on commit f89b63e

Please sign in to comment.