ChainSafe / ChainSafe/gossamer

(dot/network) `testStreamHandler` use a notify channel to remove `time.Sleep` in tests

Open
#2,204 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
454
Forks
144
PR merge metrics
No merged PRs in 30d

Description

## Issue summary

- I noticed that in all tests whose uses `testStreamHandler` has a `time.Sleep(time.Second)` to wait for the messages sent to the stream handler to arrive and then call the asserts to check.
- However, is it possible to `testStreamHandler` struct has a field `messageArrived chan<- struct{}` that always a message is arrived sent a message to the test to unblock and exec the tests without `time.Sleep(...)`

## Other information and links
```go
type testStreamHandler struct {
messageArrived <-chan struct{}
...
}

func (s *testStreamHandler) handleMessage(stream libp2pnetwork.Stream, msg Message) error {
s.messageArrived <- struct{}
...
}
```

In the test side
```go
func Test_...(t *testing.T) {
...
+ handlerMessageArrived := make(chan struct{})
+ handler := newTestStreamHandler(testBlockRequestMessageDecoder, handlerMessageArrived)
...
- time.Sleep(time.Second)
+ <-handlerMessageArrived

msg, ok := handler.messages[nodeA.host.id()]
require.True(t, ok)
require.Equal(t, 1, len(msg))
require.Equal(t, testBlockReqMessage, msg[0])
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.