aboutsummaryrefslogtreecommitdiffstats
path: root/core/utils/utils_test.go
diff options
context:
space:
mode:
authorMission Liao <mission.liao@dexon.org>2019-01-11 12:58:30 +0800
committerGitHub <noreply@github.com>2019-01-11 12:58:30 +0800
commit809e8def862fdfa792061a448f952747f1af4d3c (patch)
treebd038971e65a09bc9bb399f03a37b194ce67ae3c /core/utils/utils_test.go
parentfa25817354d5b7d40f5911004232392acfe7fe53 (diff)
downloaddexon-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar.gz
dexon-consensus-809e8def862fdfa792061a448f952747f1af4d3c.tar.zst
dexon-consensus-809e8def862fdfa792061a448f952747f1af4d3c.zip
syncer: fix issues when switching to core.Consensus (#418)
- when confirmed blocks passed to core.Consensus aren't continuous in position in some chain, the pulling would skip those missing blocks. - fix: when some block is missing, avoid adding it and all blocks after it to core.Consensus. - we need to avoid the receive channel of network module full. - fix: during switching to core.Consensus, we need to launch a dummy receiver to receive from receive channel of network module. - fix: between the period during core.Consensus created and before running, a dummy receiver is also required to receive from receive channel of network module.
Diffstat (limited to 'core/utils/utils_test.go')
-rw-r--r--core/utils/utils_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/core/utils/utils_test.go b/core/utils/utils_test.go
index 34336b3..88576b8 100644
--- a/core/utils/utils_test.go
+++ b/core/utils/utils_test.go
@@ -18,7 +18,9 @@
package utils
import (
+ "context"
"testing"
+ "time"
"github.com/stretchr/testify/suite"
@@ -111,6 +113,64 @@ func (s *UtilsTestSuite) TestVerifyDKGComplaint() {
s.False(ok)
}
+func (s *UtilsTestSuite) TestDummyReceiver() {
+ var (
+ msgCount = 1000
+ fakeMsgs = make([]int, 0, msgCount)
+ )
+ for i := 0; i < msgCount; i++ {
+ fakeMsgs = append(fakeMsgs, i)
+ }
+ launchDummySender := func(msgs []int, inputChan chan<- interface{}) {
+ finished := make(chan struct{}, 1)
+ go func() {
+ defer func() {
+ finished <- struct{}{}
+ }()
+ for _, v := range msgs {
+ inputChan <- v
+ }
+ }()
+ select {
+ case <-finished:
+ case <-time.After(1 * time.Second):
+ s.Require().FailNow("unable to deliver all messages in time")
+ }
+ }
+ checkBuffer := func(sent []int, buff []interface{}) {
+ s.Require().Len(buff, len(sent))
+ for i := range sent {
+ s.Require().Equal(sent[i], buff[i].(int))
+ }
+ }
+ // Basic scenario: a dummy receiver with caching enabled.
+ recv := make(chan interface{})
+ buff := []interface{}{}
+ cancel, finished := LaunchDummyReceiver(
+ context.Background(), recv, func(msg interface{}) {
+ buff = append(buff, msg)
+ })
+ launchDummySender(fakeMsgs, recv)
+ cancel()
+ select {
+ case <-finished:
+ case <-time.After(1 * time.Second):
+ s.Require().FailNow("should finished after cancel is called")
+ }
+ checkBuffer(fakeMsgs, buff)
+ // Dummy receiver can be shutdown along with parent context, and caching
+ // is not enabled.
+ ctx, cancel := context.WithCancel(context.Background())
+ _, finished = LaunchDummyReceiver(ctx, recv, nil)
+ launchDummySender(fakeMsgs, recv)
+ cancel()
+ select {
+ case <-finished:
+ case <-time.After(1 * time.Second):
+ s.Require().FailNow("should finished after cancel is called")
+ }
+}
+
func TestUtils(t *testing.T) {
suite.Run(t, new(UtilsTestSuite))
}