github.com/turingchain2020/turingchain@v1.1.21/p2p/base_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 package p2p 5 6 import ( 7 "testing" 8 9 "github.com/turingchain2020/turingchain/queue" 10 "github.com/turingchain2020/turingchain/types" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 var ( 15 testTy = "testP2P" 16 ) 17 18 type testP2P struct { 19 } 20 21 func newTestP2P(mgr *Manager, subCfg []byte) IP2P { 22 23 return &testP2P{} 24 } 25 26 func (p *testP2P) StartP2P() {} 27 28 func (p *testP2P) CloseP2P() {} 29 30 func initRegP2P() { 31 p2pRegTypes = make(map[string]CreateP2P) 32 } 33 34 func TestRegisterLoad(t *testing.T) { 35 36 initRegP2P() 37 RegisterP2PCreate(testTy, newTestP2P) 38 RegisterP2PCreate(testTy+"test", newTestP2P) 39 assert.Equal(t, 2, len(p2pRegTypes)) 40 create := LoadP2PCreate(testTy) 41 assert.NotNil(t, create) 42 } 43 44 func TestEvent(t *testing.T) { 45 46 cfg := types.NewTuringchainConfig(types.ReadFile("../cmd/turingchain/turingchain.test.toml")) 47 q := queue.New("channel") 48 q.SetConfig(cfg) 49 go q.Start() 50 initRegP2P() 51 RegisterP2PCreate(testTy, newTestP2P) 52 p2pCfg := cfg.GetModuleConfig().P2P 53 p2pCfg.Types = []string{testTy} 54 55 mgr := NewP2PMgr(cfg) 56 defer mgr.Close() 57 mgr.SetQueueClient(q.Client()) 58 59 subChan := mgr.PubSub.Sub(testTy) 60 61 events := []int64{types.EventTxBroadcast, types.EventBlockBroadcast, 62 types.EventFetchBlocks, types.EventGetMempool, types.EventFetchBlockHeaders, 63 types.EventPeerInfo, types.EventGetNetInfo, types.EventPubTopicMsg, types.EventFetchTopics, types.EventRemoveTopic, types.EventSubTopic} 64 65 for _, ty := range events { 66 67 mgr.Client.Send(mgr.Client.NewMessage("p2p", ty, nil), false) 68 msg, ok := (<-subChan).(*queue.Message) 69 assert.True(t, ok) 70 assert.Equal(t, ty, msg.Ty) 71 } 72 }