The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/consensus/consensus_msg_sender_test.go

69 lines
2.3 KiB

package consensus
import (
"sync/atomic"
"testing"
msg_pb "github.com/woop-chain/woop/api/proto/message"
nodeconfig "github.com/woop-chain/woop/internal/configs/node"
"github.com/woop-chain/woop/test/helpers"
"github.com/stretchr/testify/assert"
)
func TestMessageSenderInitialization(t *testing.T) {
hostData := helpers.Hosts[0]
host, _, err := helpers.GenerateHost(hostData.IP, hostData.Port)
assert.NoError(t, err)
messageSender := NewMessageSender(host)
expectedMessageSender := &MessageSender{blockNum: 0, host: host, retryTimes: int(phaseDuration.Seconds()) / RetryIntervalInSec}
assert.Equal(t, expectedMessageSender.host, messageSender.host)
assert.Equal(t, expectedMessageSender.retryTimes, messageSender.retryTimes)
assert.Equal(t, uint64(0), messageSender.blockNum)
assert.Equal(t, 0, numberOfMessagesToRetry(messageSender))
}
func TestMessageSenderReset(t *testing.T) {
hostData := helpers.Hosts[0]
host, _, err := helpers.GenerateHost(hostData.IP, hostData.Port)
assert.NoError(t, err)
messageSender := NewMessageSender(host)
assert.Equal(t, uint64(0), messageSender.blockNum)
assert.Equal(t, 0, numberOfMessagesToRetry(messageSender))
groups := []nodeconfig.GroupID{
"wiki/testnet/0.0.1/client/beacon",
"wiki/testnet/0.0.1/node/beacon",
}
p2pMsg := []byte{0}
msgType := msg_pb.MessageType_ANNOUNCE
msgRetry := MessageRetry{blockNum: 1, groups: groups, p2pMsg: p2pMsg, msgType: msgType, retryCount: 0}
atomic.StoreUint32(&msgRetry.isActive, 1)
messageSender.messagesToRetry.Store(msgType, &msgRetry)
assert.Equal(t, 1, numberOfMessagesToRetry(messageSender))
messageSender.Reset(1)
assert.Equal(t, uint64(1), messageSender.blockNum)
assert.Equal(t, 0, numberOfMessagesToRetry(messageSender))
msgType = msg_pb.MessageType_COMMITTED
msgRetry = MessageRetry{blockNum: 2, groups: groups, p2pMsg: p2pMsg, msgType: msgType, retryCount: 0}
atomic.StoreUint32(&msgRetry.isActive, 1)
messageSender.messagesToRetry.Store(msgType, &msgRetry)
messageSender.Reset(2)
assert.Equal(t, uint64(2), messageSender.blockNum)
assert.Equal(t, 1, numberOfMessagesToRetry(messageSender))
}
func numberOfMessagesToRetry(messageSender *MessageSender) int {
messagesToRetryCount := 0
messageSender.messagesToRetry.Range(func(_, _ interface{}) bool {
messagesToRetryCount++
return true
})
return messagesToRetryCount
}