OSDN Git Service

Fix p2p module test file error (#1670)
authoryahtoo <yahtoo.ma@gmail.com>
Thu, 4 Apr 2019 08:17:55 +0000 (16:17 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 4 Apr 2019 08:17:55 +0000 (16:17 +0800)
p2p/discover/net.go
p2p/peer_test.go
p2p/switch_test.go
p2p/test_util.go

index 825cded..dcc071b 100644 (file)
@@ -576,14 +576,14 @@ loop:
                                if printTestImgLogs {
                                        rad := r.radius / (maxRadius/1000000 + 1)
                                        minrad := r.minRadius / (maxRadius/1000000 + 1)
-                                       log.WithFields(log.Fields{"module": logModule}).Debug("*R %d %v %016x %v\n", tm/1000000, topic, net.tab.self.sha[:8], rad)
-                                       log.WithFields(log.Fields{"module": logModule}).Debug("*MR %d %v %016x %v\n", tm/1000000, topic, net.tab.self.sha[:8], minrad)
+                                       log.WithFields(log.Fields{"module": logModule}).Debugf("*R %d %v %016x %v\n", tm/1000000, topic, net.tab.self.sha[:8], rad)
+                                       log.WithFields(log.Fields{"module": logModule}).Debugf("*MR %d %v %016x %v\n", tm/1000000, topic, net.tab.self.sha[:8], minrad)
                                }
                        }
                        for topic, t := range net.topictab.topics {
                                wp := t.wcl.nextWaitPeriod(tm)
                                if printTestImgLogs {
-                                       log.WithFields(log.Fields{"module": logModule}).Debug("*W %d %v %016x %d\n", tm/1000000, topic, net.tab.self.sha[:8], wp/1000000)
+                                       log.WithFields(log.Fields{"module": logModule}).Debugf("*W %d %v %016x %d\n", tm/1000000, topic, net.tab.self.sha[:8], wp/1000000)
                                }
                        }
 
index f927619..4e34adb 100644 (file)
@@ -160,11 +160,11 @@ type inboundPeer struct {
        config  *cfg.Config
 }
 
-func (ip *inboundPeer) dial(addr *NetAddress) error {
+func (ip *inboundPeer) dial(addr *NetAddress) {
        pc, err := newOutboundPeerConn(addr, ip.PrivKey, DefaultPeerConfig(ip.config.P2P))
        if err != nil {
                fmt.Println("newOutboundPeerConn:", err)
-               return err
+               return
        }
 
        _, err = pc.HandshakeTimeout(&NodeInfo{
@@ -176,8 +176,7 @@ func (ip *inboundPeer) dial(addr *NetAddress) error {
        }, 5*time.Second)
        if err != nil {
                fmt.Println("Failed to perform handshake:", err)
-               return err
+               return
        }
-
-       return nil
+       time.AfterFunc(10*time.Second, pc.CloseConn)
 }
index da0db19..8f39926 100644 (file)
@@ -1,16 +1,19 @@
 package p2p
 
 import (
-       "github.com/tendermint/go-crypto"
        "io/ioutil"
        "os"
        "sync"
        "testing"
+       "time"
+
+       "github.com/davecgh/go-spew/spew"
+       "github.com/tendermint/go-crypto"
 
        cfg "github.com/bytom/config"
+       dbm "github.com/bytom/database/leveldb"
        "github.com/bytom/errors"
        conn "github.com/bytom/p2p/connection"
-       dbm "github.com/bytom/database/leveldb"
 )
 
 var (
@@ -130,12 +133,17 @@ func TestFiltersOutItself(t *testing.T) {
        defer os.RemoveAll(dirPath)
 
        testDB := dbm.NewDB("testdb", "leveldb", dirPath)
-
-       s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+       cfg := *testCfg
+       cfg.P2P.ListenAddress = "127.0.1.1:0"
+       swPrivKey := crypto.GenPrivKeyEd25519()
+       cfg.P2P.PrivateKey = swPrivKey.String()
+       s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
        s1.Start()
        defer s1.Stop()
+
        // simulate s1 having a public key and creating a remote peer with the same key
-       rp := &remotePeer{PrivKey: s1.nodePrivKey, Config: testCfg}
+       rpCfg := *testCfg
+       rp := &remotePeer{PrivKey: s1.nodePrivKey, Config: &rpCfg}
        rp.Start()
        defer rp.Stop()
        if err = s1.DialPeerWithAddress(rp.addr); errors.Root(err) != ErrConnectSelf {
@@ -143,10 +151,7 @@ func TestFiltersOutItself(t *testing.T) {
        }
 
        //S1 dialing itself ip address
-       addr, err := NewNetAddressString("0.0.0.0:46656")
-       if err != nil {
-               t.Fatal(err)
-       }
+       addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
 
        if err := s1.DialPeerWithAddress(addr); errors.Root(err) != ErrConnectSelf {
                t.Fatal(err)
@@ -161,10 +166,16 @@ func TestDialBannedPeer(t *testing.T) {
        defer os.RemoveAll(dirPath)
 
        testDB := dbm.NewDB("testdb", "leveldb", dirPath)
-       s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+       cfg := *testCfg
+       cfg.P2P.ListenAddress = "127.0.1.1:0"
+       swPrivKey := crypto.GenPrivKeyEd25519()
+       cfg.P2P.PrivateKey = swPrivKey.String()
+       s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
        s1.Start()
        defer s1.Stop()
-       rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+
+       rpCfg := *testCfg
+       rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: &rpCfg}
        rp.Start()
        defer rp.Stop()
        s1.AddBannedPeer(rp.addr.IP.String())
@@ -186,12 +197,19 @@ func TestDuplicateOutBoundPeer(t *testing.T) {
        defer os.RemoveAll(dirPath)
 
        testDB := dbm.NewDB("testdb", "leveldb", dirPath)
-       s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+       cfg := *testCfg
+       cfg.P2P.ListenAddress = "127.0.1.1:0"
+       swPrivKey := crypto.GenPrivKeyEd25519()
+       cfg.P2P.PrivateKey = swPrivKey.String()
+       s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
        s1.Start()
        defer s1.Stop()
-       rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+
+       rpCfg := *testCfg
+       rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: &rpCfg}
        rp.Start()
        defer rp.Stop()
+
        if err = s1.DialPeerWithAddress(rp.addr); err != nil {
                t.Fatal(err)
        }
@@ -209,28 +227,29 @@ func TestDuplicateInBoundPeer(t *testing.T) {
        defer os.RemoveAll(dirPath)
 
        testDB := dbm.NewDB("testdb", "leveldb", dirPath)
-       s1 := MakeSwitch(testCfg, testDB, initSwitchFunc)
+       cfg := *testCfg
+       cfg.P2P.ListenAddress = "127.0.1.1:0"
+       swPrivKey := crypto.GenPrivKeyEd25519()
+       cfg.P2P.PrivateKey = swPrivKey.String()
+       s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
        s1.Start()
        defer s1.Stop()
 
-       inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
-       addr, err := NewNetAddressString(s1.nodeInfo.ListenAddr)
+       inpCfg := *testCfg
+       inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: &inpCfg}
+       addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
        if err != nil {
                t.Fatal(err)
        }
+       go inp.dial(addr)
 
-       if err = inp.dial(addr); err != nil {
-               t.Fatal(err)
-       }
-
-       inp1 := &inboundPeer{PrivKey: inp.PrivKey, config: testCfg}
-
-       if err = inp1.dial(addr); err != nil {
-               t.Fatal(err)
-       }
+       inp1Cfg := *testCfg
+       inp1 := &inboundPeer{PrivKey: inp.PrivKey, config: &inp1Cfg}
+       go inp1.dial(addr)
 
+       time.Sleep(1 * time.Second)
        if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 1 {
-               t.Fatal("TestDuplicateInBoundPeer peer size error", outbound, inbound, dialing)
+               t.Fatal("TestDuplicateInBoundPeer peer size error want 1, got:", outbound, inbound, dialing, spew.Sdump(s1.peers.lookup))
        }
 }
 
@@ -244,34 +263,44 @@ func TestAddInboundPeer(t *testing.T) {
        testDB := dbm.NewDB("testdb", "leveldb", dirPath)
        cfg := *testCfg
        cfg.P2P.MaxNumPeers = 2
-       s1 := MakeSwitch(&cfg, testDB, initSwitchFunc)
+       cfg.P2P.ListenAddress = "127.0.1.1:0"
+       swPrivKey := crypto.GenPrivKeyEd25519()
+       cfg.P2P.PrivateKey = swPrivKey.String()
+       s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
        s1.Start()
        defer s1.Stop()
 
-       inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
-       addr, err := NewNetAddressString(s1.nodeInfo.ListenAddr)
+       inpCfg := *testCfg
+       inpPrivKey := crypto.GenPrivKeyEd25519()
+       inpCfg.P2P.PrivateKey = inpPrivKey.String()
+       inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
+       addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
        if err != nil {
                t.Fatal(err)
        }
+       go inp.dial(addr)
 
-       if err := inp.dial(addr); err != nil {
-               t.Fatal(err)
-       }
-
-       rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+       rpCfg := *testCfg
+       rpPrivKey := crypto.GenPrivKeyEd25519()
+       rpCfg.P2P.PrivateKey = rpPrivKey.String()
+       rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
        rp.Start()
        defer rp.Stop()
+
        if err := s1.DialPeerWithAddress(rp.addr); err != nil {
                t.Fatal(err)
        }
 
-       if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
-               t.Fatal("TestAddInboundPeer peer size error")
-       }
-       inp2 := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
+       inp2Cfg := *testCfg
+       inp2PrivKey := crypto.GenPrivKeyEd25519()
+       inp2Cfg.P2P.PrivateKey = inp2PrivKey.String()
+       inp2 := &inboundPeer{PrivKey: inp2PrivKey, config: &inp2Cfg}
+
+       go inp2.dial(addr)
 
-       if err := inp2.dial(addr); err == nil {
-               t.Fatal("TestAddInboundPeer MaxNumPeers limit error")
+       time.Sleep(1 * time.Second)
+       if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
+               t.Fatal("TestAddInboundPeer peer size error want 2 got:", spew.Sdump(s1.peers.lookup))
        }
 }
 
@@ -285,38 +314,45 @@ func TestStopPeer(t *testing.T) {
        testDB := dbm.NewDB("testdb", "leveldb", dirPath)
        cfg := *testCfg
        cfg.P2P.MaxNumPeers = 2
-       s1 := MakeSwitch(&cfg, testDB, initSwitchFunc)
+       cfg.P2P.ListenAddress = "127.0.1.1:0"
+       swPrivKey := crypto.GenPrivKeyEd25519()
+       cfg.P2P.PrivateKey = swPrivKey.String()
+       s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
        s1.Start()
        defer s1.Stop()
 
-       inp := &inboundPeer{PrivKey: crypto.GenPrivKeyEd25519(), config: testCfg}
-       addr, err := NewNetAddressString("127.0.0.1:46656")
+       inpCfg := *testCfg
+       inpPrivKey := crypto.GenPrivKeyEd25519()
+       inpCfg.P2P.PrivateKey = inpPrivKey.String()
+       inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
+       addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
        if err != nil {
                t.Fatal(err)
        }
+       go inp.dial(addr)
 
-       if err := inp.dial(addr); err != nil {
-               t.Fatal(err)
-       }
-
-       rp := &remotePeer{PrivKey: crypto.GenPrivKeyEd25519(), Config: testCfg}
+       rpCfg := *testCfg
+       rpPrivKey := crypto.GenPrivKeyEd25519()
+       rpCfg.P2P.PrivateKey = rpPrivKey.String()
+       rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
        rp.Start()
        defer rp.Stop()
+
        if err := s1.DialPeerWithAddress(rp.addr); err != nil {
                t.Fatal(err)
        }
-
+       time.Sleep(1 * time.Second)
        if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 2 {
-               t.Fatal("TestStopPeer peer size error")
+               t.Fatal("TestStopPeer peer size error want 2,got:", spew.Sdump(s1.peers.lookup))
        }
 
        s1.StopPeerGracefully(s1.peers.list[0].Key)
        if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 1 {
-               t.Fatal("TestStopPeer peer size error")
+               t.Fatal("TestStopPeer peer size error,want 1,got:", spew.Sdump(s1.peers.lookup))
        }
 
        s1.StopPeerForError(s1.peers.list[0], "stop for test")
        if outbound, inbound, dialing := s1.NumPeers(); outbound+inbound+dialing != 0 {
-               t.Fatal("TestStopPeer peer size error")
+               t.Fatal("TestStopPeer peer size error,want 0, got:", spew.Sdump(s1.peers.lookup))
        }
 }
index b8d4cac..00b172b 100644 (file)
@@ -8,7 +8,6 @@ import (
        cmn "github.com/tendermint/tmlibs/common"
 
        cfg "github.com/bytom/config"
-       "github.com/bytom/errors"
        "github.com/bytom/p2p/connection"
        "github.com/bytom/p2p/discover"
        dbm "github.com/bytom/database/leveldb"
@@ -46,33 +45,6 @@ func CreateRoutableAddr() (addr string, netAddr *NetAddress) {
        return
 }
 
-// MakeConnectedSwitches switches connected via arbitrary net.Conn; useful for testing
-// Returns n switches, connected according to the connect func.
-// If connect==Connect2Switches, the switches will be fully connected.
-// initSwitch defines how the ith switch should be initialized (ie. with what reactors).
-// NOTE: panics if any switch fails to start.
-func MakeConnectedSwitches(cfg []*cfg.Config, n int, testDB dbm.DB, initSwitch func(*Switch) *Switch, connect func([]*Switch, int, int)) []*Switch {
-       if len(cfg) != n {
-               panic(errors.New("cfg number error"))
-       }
-       switches := make([]*Switch, n)
-       for i := 0; i < n; i++ {
-               switches[i] = MakeSwitch(cfg[i], testDB, initSwitch)
-       }
-
-       if err := startSwitches(switches); err != nil {
-               panic(err)
-       }
-
-       for i := 0; i < n; i++ {
-               for j := i; j < n; j++ {
-                       connect(switches, i, j)
-               }
-       }
-
-       return switches
-}
-
 // Connect2Switches will connect switches i and j via net.Pipe()
 // Blocks until a conection is established.
 // NOTE: caller ensures i and j are within bounds
@@ -116,10 +88,8 @@ func (m *mockDiscv) ReadRandomNodes(buf []*discover.Node) (n int) {
        return 0
 }
 
-func MakeSwitch(cfg *cfg.Config, testdb dbm.DB, initSwitch func(*Switch) *Switch) *Switch {
+func MakeSwitch(cfg *cfg.Config, testdb dbm.DB, privKey crypto.PrivKeyEd25519, initSwitch func(*Switch) *Switch) *Switch {
        // new switch, add reactors
-       // TODO: let the config be passed in?
-       privKey := crypto.GenPrivKeyEd25519()
        l, listenAddr := GetListener(cfg.P2P)
        sw, err := newSwitch(cfg, new(mockDiscv), testdb, l, privKey, listenAddr)
        if err != nil {