@@ -42,75 +42,66 @@ func TestMain(m *testing.M) {
4242}
4343
4444func TestHandler_SendMulti (t * testing.T ) {
45- var wg sync.WaitGroup
46- wg .Add (6 )
47-
48- resultCh = make (chan []byte )
49-
50- go func () {
51- genesisHash := common .MustHexToHash ("0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3" )
52-
53- GetInstance ().SendMessage (NewSystemConnectedTM (false , "chain" , & genesisHash ,
54- "systemName" , "nodeName" , "netID" , "startTime" , "0.1" ))
55-
56- wg .Done ()
57- }()
58-
59- go func () {
60- bh := common .MustHexToHash ("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6" )
61- GetInstance ().SendMessage (NewBlockImportTM (& bh , big .NewInt (2 ), "NetworkInitialSync" ))
62-
63- wg .Done ()
64- }()
65-
66- go func () {
67- GetInstance ().SendMessage (NewBandwidthTM (2 , 3 , 1 ))
68-
69- wg .Done ()
70- }()
71-
72- go func () {
73- bestHash := common .MustHexToHash ("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6" )
74- finalisedHash := common .MustHexToHash ("0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2" )
75- GetInstance ().SendMessage (NewBlockIntervalTM (& bestHash , big .NewInt (32375 ), & finalisedHash ,
76- big .NewInt (32256 ), big .NewInt (0 ), big .NewInt (1234 )))
77-
78- wg .Done ()
79- }()
80-
81- go func () {
82- bestHash := common .MustHexToHash ("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6" )
83- GetInstance ().SendMessage (NewNotifyFinalizedTM (bestHash , "32375" ))
45+ expected := [][]byte {
46+ []byte (`{"authority":false,"chain":"chain","genesis_hash":"0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3","implementation":"systemName","msg":"system.connected","name":"nodeName","network_id":"netID","startup_time":"startTime","ts":` ),
47+ []byte (`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","height":2,"msg":"block.import","origin":"NetworkInitialSync","ts":` ),
48+ []byte (`{"bandwidth_download":2,"bandwidth_upload":3,"msg":"system.interval","peers":1,"ts":` ),
49+ []byte (`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","finalized_hash":"0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2","finalized_height":32256,"height":32375,"msg":"system.interval","ts":` ), //nolint
50+ []byte (`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","height":"32375","msg":"notify.finalized","ts":` ),
51+ []byte (`{"hash":"0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c","msg":"prepared_block_for_proposing","number":"1","ts":` ),
52+ []byte (`{"future":2,"msg":"txpool.import","ready":1,"ts":` ),
53+ }
8454
85- wg .Done ()
86- }()
55+ messages := []Message {
56+ NewBandwidthTM (2 , 3 , 1 ),
57+ NewTxpoolImportTM (1 , 2 ),
58+
59+ func (genesisHash common.Hash ) Message {
60+ return NewSystemConnectedTM (false , "chain" , & genesisHash ,
61+ "systemName" , "nodeName" , "netID" , "startTime" , "0.1" )
62+ }(common .MustHexToHash ("0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3" )),
63+
64+ func (bh common.Hash ) Message {
65+ return NewBlockImportTM (& bh , big .NewInt (2 ), "NetworkInitialSync" )
66+ }(common .MustHexToHash ("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6" )),
67+
68+ func (bestHash , finalisedHash common.Hash ) Message {
69+ return NewBlockIntervalTM (& bestHash , big .NewInt (32375 ), & finalisedHash ,
70+ big .NewInt (32256 ), big .NewInt (0 ), big .NewInt (1234 ))
71+ }(
72+ common .MustHexToHash ("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6" ),
73+ common .MustHexToHash ("0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2" ),
74+ ),
75+
76+ NewNotifyFinalizedTM (common .MustHexToHash ("0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6" ), "32375" ),
77+ NewPreparedBlockForProposingTM (common .MustHexToHash ("0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c" ), "1" ),
78+ }
8779
88- go func () {
89- bestHash := common .MustHexToHash ("0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c" )
90- GetInstance ().SendMessage (NewPreparedBlockForProposingTM (bestHash , "1" ))
80+ resultCh = make (chan []byte )
9181
92- wg .Done ()
93- }()
82+ var wg sync.WaitGroup
83+ for _ , message := range messages {
84+ wg .Add (1 )
85+ go func (msg Message ) {
86+ GetInstance ().SendMessage (msg )
87+ wg .Done ()
88+ }(message )
89+ }
9490
9591 wg .Wait ()
9692
97- expected1 := []byte (`{"authority":false,"chain":"chain","genesis_hash":"0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3","implementation":"systemName","msg":"system.connected","name":"nodeName","network_id":"netID","startup_time":"startTime","ts":` )
98- expected2 := []byte (`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","height":2,"msg":"block.import","origin":"NetworkInitialSync","ts":` )
99- expected3 := []byte (`{"bandwidth_download":2,"bandwidth_upload":3,"msg":"system.interval","peers":1,"ts":` )
100- expected4 := []byte (`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","finalized_hash":"0x687197c11b4cf95374159843e7f46fbcd63558db981aaef01a8bac2a44a1d6b2","finalized_height":32256,"height":32375,"msg":"system.interval","ts":` ) // nolint
101- expected5 := []byte (`{"best":"0x07b749b6e20fd5f1159153a2e790235018621dd06072a62bcd25e8576f6ff5e6","height":"32375","msg":"notify.finalized","ts":` )
102- expected6 := []byte (`{"hash":"0x5814aec3e28527f81f65841e034872f3a30337cf6c33b2d258bba6071e37e27c","msg":"prepared_block_for_proposing","number":"1","ts":` )
103-
104- expected := [][]byte {expected1 , expected3 , expected4 , expected5 , expected2 , expected6 }
105-
10693 var actual [][]byte
10794 for data := range resultCh {
10895 actual = append (actual , data )
109- if len (actual ) == 6 {
96+ if len (actual ) == len ( expected ) {
11097 break
11198 }
11299 }
113100
101+ sort .Slice (expected , func (i , j int ) bool {
102+ return bytes .Compare (expected [i ], expected [j ]) < 0
103+ })
104+
114105 sort .Slice (actual , func (i , j int ) bool {
115106 return bytes .Compare (actual [i ], actual [j ]) < 0
116107 })
0 commit comments