Skip to content

Commit

Permalink
fix: flaky test in TestProxySocketsAdminFacade (#1474)
Browse files Browse the repository at this point in the history
* fix: flaky test in TestProxySocketsAdminFacade

The test was incorrectly cancelling the context. When the proxySockets function encounters an error like a failure to connect to a Juju controller it returns with an error. Otherwise it returns a nil error when the context is cancelled. Here we were expecting the former for one of the tests cases but sometimes encountering the latter in a race condition.

* chore: remove expectProxyError bool from test
  • Loading branch information
kian99 authored Dec 2, 2024
1 parent 37afcf5 commit 63be70f
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions internal/rpc/apiproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestProxySocketsAdminFacade(t *testing.T) {
expectedClientResponse *message
expectedControllerMessage *message
oauthAuthenticatorError error
expectConnectTofail bool
expectedProxyError string
}{{
about: "login device call - client gets response with both user code and verification uri",
messageToSend: message{
Expand Down Expand Up @@ -232,18 +232,21 @@ func TestProxySocketsAdminFacade(t *testing.T) {
},
oauthAuthenticatorError: errors.E(errors.CodeUnauthorized),
}, {
about: "connection to controller fails",
expectConnectTofail: true,
about: "connection to controller fails",
expectedClientResponse: &message{
Error: "controller connection error",
},
expectedProxyError: "failed to connect to controller: controller connection error",
}}

for _, test := range tests {
t.Run(test.about, func(t *testing.T) {
proxyError := test.expectedProxyError != ""

ctx := context.Background()
ctx, cancelFunc := context.WithCancel(ctx)
defer cancelFunc()

clientWebsocket := newMockWebsocketConnection(10)
controllerWebsocket := newMockWebsocketConnection(10)
loginSvc := &mockLoginService{
Expand All @@ -257,7 +260,7 @@ func TestProxySocketsAdminFacade(t *testing.T) {
ConnClient: clientWebsocket,
TokenGen: &mockTokenGenerator{},
ConnectController: func(ctx context.Context) (rpc.WebsocketConnectionWithMetadata, error) {
if test.expectConnectTofail {
if proxyError {
return rpc.WebsocketConnectionWithMetadata{}, goerr.New("controller connection error")
}
return rpc.WebsocketConnectionWithMetadata{
Expand All @@ -275,8 +278,8 @@ func TestProxySocketsAdminFacade(t *testing.T) {
go func() {
defer wg.Done()
err = rpc.ProxySockets(ctx, helpers)
if test.expectConnectTofail {
c.Assert(err, qt.ErrorMatches, "failed to connect to controller: controller connection error")
if proxyError {
c.Assert(err, qt.ErrorMatches, test.expectedProxyError)
} else {
c.Assert(err, qt.ErrorMatches, "Context cancelled")
}
Expand All @@ -300,7 +303,9 @@ func TestProxySocketsAdminFacade(t *testing.T) {
c.Fatal("timed out waiting for response")
}
}
cancelFunc()
if !proxyError {
cancelFunc()
}
wg.Wait()
t.Logf("completed test %s", t.Name())
})
Expand Down

0 comments on commit 63be70f

Please sign in to comment.