Skip to content

Commit 8552a51

Browse files
add commentaries for tests
1 parent 2aa8933 commit 8552a51

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

engine/access/rest/websockets/controller.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,21 @@ func (c *Controller) handleListSubscriptions(ctx context.Context, msg models.Lis
224224

225225
func (c *Controller) shutdownConnection() {
226226
c.shutdownOnce.Do(func() {
227-
defer close(c.communicationChannel)
228-
defer func(conn WebsocketConnection) {
227+
defer func() {
228+
close(c.communicationChannel)
229+
229230
if err := c.conn.Close(); err != nil {
230231
c.logger.Warn().Err(err).Msg("error closing connection")
231232
}
232-
}(c.conn)
233+
}()
233234

234235
c.logger.Debug().Msg("shutting down connection")
235236

236-
err := c.dataProviders.ForEach(func(_ uuid.UUID, dp dp.DataProvider) error {
237-
dp.Close()
237+
_ = c.dataProviders.ForEach(func(id uuid.UUID, dp dp.DataProvider) error {
238+
err := dp.Close()
239+
c.logger.Error().Err(err).Msgf("error closing data provider: %s", id.String())
238240
return nil
239241
})
240-
if err != nil {
241-
c.logger.Error().Err(err).Msg("error closing data provider")
242-
}
243242

244243
c.dataProviders.Clear()
245244
})

engine/access/rest/websockets/controller_test.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type WsControllerSuite struct {
3131
}
3232

3333
func (s *WsControllerSuite) SetupTest() {
34-
//s.logger = unittest.LoggerWithWriterAndLevel(os.Stdout, zerolog.DebugLevel)
3534
s.logger = unittest.Logger()
3635
s.wsConfig = NewDefaultWebsocketConfig()
3736
s.streamApi = streammock.NewAPI(s.T())
@@ -42,6 +41,8 @@ func TestWsControllerSuite(t *testing.T) {
4241
suite.Run(t, new(WsControllerSuite))
4342
}
4443

44+
// TestSubscribeRequest tests the subscribe to topic flow.
45+
// We emulate a request message from a client, and a response message from a controller.
4546
func (s *WsControllerSuite) TestSubscribeRequest() {
4647
s.T().Run("Happy path", func(t *testing.T) {
4748
conn, dataProviderFactory, dataProvider := newControllerMocks(t)
@@ -58,6 +59,7 @@ func (s *WsControllerSuite) TestSubscribeRequest() {
5859
Arguments: nil,
5960
}
6061

62+
// Simulate receiving the subscription request from the client
6163
conn.
6264
On("ReadJSON", mock.Anything).
6365
Run(func(args mock.Arguments) {
@@ -70,17 +72,21 @@ func (s *WsControllerSuite) TestSubscribeRequest() {
7072
Return(nil).
7173
Once()
7274

75+
// Channel to signal the test flow completion
7376
done := make(chan struct{}, 1)
77+
78+
// Simulate writing a successful subscription response back to the client
7479
conn.
7580
On("WriteJSON", mock.Anything).
7681
Return(func(msg interface{}) error {
7782
response, ok := msg.(models.SubscribeMessageResponse)
7883
require.True(t, ok)
7984
require.True(t, response.Success)
80-
close(done)
85+
close(done) // Signal that response has been sent
8186
return websocket.ErrCloseSent
8287
})
8388

89+
// Simulate client closing connection after receiving the response
8490
conn.
8591
On("ReadJSON", mock.Anything).
8692
Return(func(interface{}) error {
@@ -95,12 +101,13 @@ func (s *WsControllerSuite) TestSubscribeRequest() {
95101
})
96102
}
97103

104+
// TestSubscribeBlocks tests the functionality for streaming blocks to a subscriber.
98105
func (s *WsControllerSuite) TestSubscribeBlocks() {
99106
s.T().Run("Stream one block", func(t *testing.T) {
100107
conn, dataProviderFactory, dataProvider := newControllerMocks(t)
101108
controller := NewWebSocketController(s.logger, s.wsConfig, dataProviderFactory, conn)
102109

103-
// we want data provider to write some block to controller
110+
// Simulate data provider write a block to the controller
104111
expectedBlock := unittest.BlockFixture()
105112
dataProvider.
106113
On("Run", mock.Anything).
@@ -110,15 +117,17 @@ func (s *WsControllerSuite) TestSubscribeBlocks() {
110117
Once()
111118

112119
done := make(chan struct{}, 1)
113-
var actualBlock flow.Block
114-
115120
s.expectSubscriptionRequest(conn, done)
116121
s.expectSubscriptionResponse(conn, true)
117122

123+
// Expect a valid block to be passed to WriteJSON.
124+
// If we got to this point, the controller executed all its logic properly
125+
var actualBlock flow.Block
118126
conn.
119127
On("WriteJSON", mock.Anything).
120128
Return(func(msg interface{}) error {
121-
block := msg.(flow.Block)
129+
block, ok := msg.(flow.Block)
130+
require.True(t, ok)
122131
actualBlock = block
123132

124133
close(done)
@@ -133,7 +142,7 @@ func (s *WsControllerSuite) TestSubscribeBlocks() {
133142
conn, dataProviderFactory, dataProvider := newControllerMocks(t)
134143
controller := NewWebSocketController(s.logger, s.wsConfig, dataProviderFactory, conn)
135144

136-
// we want data provider to write some block to controller
145+
// Simulate data provider writes some blocks to the controller
137146
expectedBlocks := unittest.BlockFixtures(100)
138147
dataProvider.
139148
On("Run", mock.Anything).
@@ -145,16 +154,20 @@ func (s *WsControllerSuite) TestSubscribeBlocks() {
145154
Once()
146155

147156
done := make(chan struct{}, 1)
148-
actualBlocks := make([]*flow.Block, len(expectedBlocks))
149-
i := 0
150-
151157
s.expectSubscriptionRequest(conn, done)
152158
s.expectSubscriptionResponse(conn, true)
153159

160+
i := 0
161+
actualBlocks := make([]*flow.Block, len(expectedBlocks))
162+
163+
// Expect valid blocks to be passed to WriteJSON.
164+
// If we got to this point, the controller executed all its logic properly
154165
conn.
155166
On("WriteJSON", mock.Anything).
156167
Return(func(msg interface{}) error {
157-
block := msg.(flow.Block)
168+
block, ok := msg.(flow.Block)
169+
require.True(t, ok)
170+
158171
actualBlocks[i] = &block
159172
i += 1
160173

@@ -172,6 +185,8 @@ func (s *WsControllerSuite) TestSubscribeBlocks() {
172185
})
173186
}
174187

188+
// newControllerMocks initializes mock WebSocket connection, data provider, and data provider factory.
189+
// The mocked functions are expected to be called in a case when a test is expected to reach WriteJSON function.
175190
func newControllerMocks(t *testing.T) (*connmock.WebsocketConnection, *dpmock.Factory, *dpmock.DataProvider) {
176191
conn := connmock.NewWebsocketConnection(t)
177192
conn.On("Close").Return(nil).Once()
@@ -192,6 +207,7 @@ func newControllerMocks(t *testing.T) (*connmock.WebsocketConnection, *dpmock.Fa
192207
return conn, factory, dataProvider
193208
}
194209

210+
// expectSubscriptionRequest mocks the client's subscription request.
195211
func (s *WsControllerSuite) expectSubscriptionRequest(conn *connmock.WebsocketConnection, done <-chan struct{}) {
196212
requestMessage := models.SubscribeMessageRequest{
197213
BaseMessageRequest: models.BaseMessageRequest{Action: "subscribe"},
@@ -223,6 +239,7 @@ func (s *WsControllerSuite) expectSubscriptionRequest(conn *connmock.WebsocketCo
223239
})
224240
}
225241

242+
// expectSubscriptionResponse mocks the subscription response sent to the client.
226243
func (s *WsControllerSuite) expectSubscriptionResponse(conn *connmock.WebsocketConnection, success bool) {
227244
conn.On("WriteJSON", mock.Anything).
228245
Run(func(args mock.Arguments) {

0 commit comments

Comments
 (0)