@@ -31,7 +31,6 @@ type WsControllerSuite struct {
3131}
3232
3333func (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.
4546func (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.
98105func (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.
175190func 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.
195211func (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.
226243func (s * WsControllerSuite ) expectSubscriptionResponse (conn * connmock.WebsocketConnection , success bool ) {
227244 conn .On ("WriteJSON" , mock .Anything ).
228245 Run (func (args mock.Arguments ) {
0 commit comments