Skip to content

Commit

Permalink
Increase channel capacity for sync tests
Browse files Browse the repository at this point in the history
  • Loading branch information
themantre committed Apr 1, 2021
1 parent 3a6c516 commit ad7748d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestConfig() *Config {
return &Config{
Moniker: "test",
StartingTimeout: time.Second * 1,
HeartBeatTimeout: time.Second * 1,
HeartBeatTimeout: time.Second * 2,
SessionTimeout: time.Second * 1,
InitialBlockDownload: true,
BlockPerMessage: 10,
Expand Down
31 changes: 17 additions & 14 deletions sync/network_api/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ import (
)

type MockNetworkAPI struct {
ch chan *message.Message
id peer.ID
Firewall *firewall.Firewall
ParsFn ParsMessageFn
OtherAPI *MockNetworkAPI
PublishCh chan *message.Message
id peer.ID
Firewall *firewall.Firewall
ParsFn ParsMessageFn
OtherAPI *MockNetworkAPI
}

func MockingNetworkAPI(id peer.ID) *MockNetworkAPI {
return &MockNetworkAPI{
ch: make(chan *message.Message, 100),
id: id,
PublishCh: make(chan *message.Message, 1000),
id: id,
}
}
func (mock *MockNetworkAPI) Start() error {
Expand All @@ -37,17 +37,18 @@ func (mock *MockNetworkAPI) JoinDownloadTopic() error {
return nil
}
func (mock *MockNetworkAPI) LeaveDownloadTopic() {}
func (mock *MockNetworkAPI) PublishMessage(msg *message.Message) error {
mock.ch <- msg
return nil
}
func (mock *MockNetworkAPI) SelfID() peer.ID {
return mock.id
}
func (mock *MockNetworkAPI) PublishMessage(msg *message.Message) error {
mock.PublishCh <- msg
return nil
}
func (mock *MockNetworkAPI) CheckAndParsMessage(msg *message.Message, id peer.ID) bool {
d, _ := msg.Encode()
msg2 := mock.Firewall.ParsMessage(d, id)
if msg2 != nil {
logger.Info("Parsing the message", "msg", msg)
mock.ParsFn(msg2, mock.id)
return true
}
Expand All @@ -64,11 +65,12 @@ func (mock *MockNetworkAPI) ShouldPublishMessageWithThisType(t *testing.T, paylo
for {
select {
case <-timeout.C:
require.NoError(t, fmt.Errorf("Timeout"))
require.NoError(t, fmt.Errorf("ShouldPublishMessageWithThisType: Timeout"))
return nil
case msg := <-mock.ch:
case msg := <-mock.PublishCh:
logger.Info("shouldPublishMessageWithThisType", "id", mock.id, "msg", msg, "type", payloadType.String())
mock.sendMessageToOtherPeer(msg)
logger.Info("Nessage sent to other peer", "msg", msg)

if msg.PayloadType() == payloadType {
return msg
Expand All @@ -84,9 +86,10 @@ func (mock *MockNetworkAPI) ShouldNotPublishMessageWithThisType(t *testing.T, pa
select {
case <-timeout.C:
return
case msg := <-mock.ch:
case msg := <-mock.PublishCh:
logger.Info("shouldNotPublishMessageWithThisType", "id", mock.id, "msg", msg, "type", payloadType.String())
mock.sendMessageToOtherPeer(msg)
logger.Info("Nessage sent to other peer", "msg", msg)

assert.NotEqual(t, msg.PayloadType(), payloadType)
}
Expand Down
2 changes: 2 additions & 0 deletions sync/state_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

func TestAddBlockToCache(t *testing.T) {
setup(t)
disableHeartbeat(t)

b1, trxs1 := block.GenerateTestBlock(nil, nil)
b2, trxs2 := block.GenerateTestBlock(nil, nil)
Expand Down Expand Up @@ -163,6 +164,7 @@ func TestSessionTimeout(t *testing.T) {

func TestOneBlockBehind(t *testing.T) {
setup(t)
disableHeartbeat(t)

t.Run("Bob is not in the committee. Bob commits one block. Bob should broadcasts heartbeat. Alice should ask for the last block.", func(t *testing.T) {
addMoreBlocksForBob(t, 1)
Expand Down

0 comments on commit ad7748d

Please sign in to comment.