Skip to content

Commit

Permalink
Added init tests for inactivity tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyanaAndrukhiv committed Dec 16, 2024
1 parent eaa649f commit bbafe48
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions engine/access/rest/websockets/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,36 @@ func (s *WsControllerSuite) TestControllerShutdown() {

conn.AssertExpectations(t)
})

s.T().Run("Inactivity tracking", func(t *testing.T) {
t.Parallel()

conn := connmock.NewWebsocketConnection(t)
conn.On("Close").Return(nil).Once()
conn.On("SetReadDeadline", mock.Anything).Return(nil).Once()
conn.On("SetPongHandler", mock.AnythingOfType("func(string) error")).Return(nil).Once()

factory := dpmock.NewDataProviderFactory(t)
// Mock with short inactivity timeout for testing
wsConfig := s.wsConfig

wsConfig.InactivityTimeout = 50 * time.Millisecond
controller := NewWebSocketController(s.logger, wsConfig, conn, factory)

conn.
On("ReadJSON", mock.Anything).
Return(func(interface{}) error {
// wait on read
<-time.After(wsConfig.InactivityTimeout + 10)
return websocket.ErrCloseSent
}).
Once()

controller.HandleConnection(context.Background())
time.Sleep(wsConfig.InactivityTimeout)

conn.AssertExpectations(t)
})
}

func (s *WsControllerSuite) TestKeepaliveRoutine() {
Expand Down Expand Up @@ -902,6 +932,25 @@ func (s *WsControllerSuite) TestKeepaliveRoutine() {
})
}

// TestMonitorInactivity verifies that monitorInactivity returns an error
// when the WebSocket connection has no subscriptions for the configured inactivity timeout.
func (s *WsControllerSuite) TestMonitorInactivity() {
conn := connmock.NewWebsocketConnection(s.T())
factory := dpmock.NewDataProviderFactory(s.T())

// Mock with short inactivity timeout for testing
wsConfig := s.wsConfig

wsConfig.InactivityTimeout = 50 * time.Millisecond
controller := NewWebSocketController(s.logger, wsConfig, conn, factory)

err := controller.monitorInactivity(context.Background())
s.Require().Error(err)
s.Require().Equal(err, fmt.Errorf("no recent activity for %v", s.wsConfig.InactivityTimeout))

conn.AssertExpectations(s.T())
}

// newControllerMocks initializes mock WebSocket connection, data provider, and data provider factory.
// The mocked functions are expected to be called in a case when a test is expected to reach WriteJSON function.
func newControllerMocks(t *testing.T) (*connmock.WebsocketConnection, *dpmock.DataProviderFactory, *dpmock.DataProvider) {
Expand Down

0 comments on commit bbafe48

Please sign in to comment.