Skip to content

Commit

Permalink
fix ut
Browse files Browse the repository at this point in the history
  • Loading branch information
yxq committed Jun 29, 2021
1 parent 4e44d10 commit 5c736fa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ clone_folder: c:\gopath\src\github.com\33cn\chain33
environment:
GOPATH: c:\gopath
DEPTESTBYPASS501: 1
GOVERSION: 1.14.15
GOVERSION: 1.14.14
GO111MODULE: on

#init:
Expand Down
58 changes: 33 additions & 25 deletions system/p2p/dht/protocol/p2pstore/p2pstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,30 +293,15 @@ func initEnv(t *testing.T, q queue.Queue) *Protocol {
if err != nil {
panic(err)
}
env1 := protocol.P2PEnv{
Ctx: context.Background(),
ChainCfg: cfg,
API: mockAPI1,
QueueClient: client1,
Host: host1,
SubConfig: mcfg,
RoutingDiscovery: discovery.NewRoutingDiscovery(kademliaDHT1),
RoutingTable: kademliaDHT1.RoutingTable(),
PeerInfoManager: &peerInfoManager{},
DB: dbm.NewDB("p2pstore1", "leveldb", dataDir, 128),
}
InitProtocol(&env1)

kademliaDHT2, err := dht.New(context.Background(), host2)
if err != nil {
panic(err)
}
addr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/13806/p2p/%s", host1.ID().Pretty()))
addr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d/p2p/%s", port1, host1.ID().Pretty()))
peerinfo, _ := peer.AddrInfoFromP2pAddr(addr)
err = host2.Connect(context.Background(), *peerinfo)
require.Nil(t, err)
//err = kademliaDHT2.Bootstrap(context.Background())
//require.Nil(t, err)

mockAPI2, err := client.New(client2, nil)
if err != nil {
Expand Down Expand Up @@ -348,9 +333,7 @@ func initEnv(t *testing.T, q queue.Queue) *Protocol {
chunkProviderCache: make(map[string]map[peer.ID]time.Time),
extendRoutingTable: exRT,
}
//go p2.updateRoutine()
go p2.refreshRoutine()
go p2.processLocalChunk()

//注册p2p通信协议,用于处理节点之间请求
protocol.RegisterStreamHandler(p2.Host, requestPeerInfoForChunk, p2.handleStreamRequestPeerInfoForChunk)
protocol.RegisterStreamHandler(p2.Host, responsePeerInfoForChunk, p2.handleStreamResponsePeerInfoForChunk)
Expand Down Expand Up @@ -387,13 +370,38 @@ func initEnv(t *testing.T, q queue.Queue) *Protocol {
}
}()

_, _ = kademliaDHT1.RoutingTable().TryAddPeer(host2.ID(), true, false)
_, _ = kademliaDHT2.RoutingTable().TryAddPeer(host1.ID(), true, false)
fmt.Println("size1:", kademliaDHT1.RoutingTable().Size(), "size2:", kademliaDHT2.RoutingTable().Size())
time.Sleep(time.Second)
env1 := protocol.P2PEnv{
Ctx: context.Background(),
ChainCfg: cfg,
API: mockAPI1,
QueueClient: client1,
Host: host1,
SubConfig: mcfg,
RoutingDiscovery: discovery.NewRoutingDiscovery(kademliaDHT1),
RoutingTable: kademliaDHT1.RoutingTable(),
PeerInfoManager: &peerInfoManager{},
DB: dbm.NewDB("p2pstore1", "leveldb", dataDir, 128),
}
InitProtocol(&env1)

go func() {
for {
if kademliaDHT1.RoutingTable().Size() == 0 {
ok, err := kademliaDHT1.RoutingTable().TryAddPeer(host2.ID(), true, false)
t.Log("host1 try add peer", ok, err)
}
if kademliaDHT2.RoutingTable().Size() == 0 {
ok, err := kademliaDHT2.RoutingTable().TryAddPeer(host1.ID(), true, false)
t.Log("host2 try add peer", ok, err)
}
time.Sleep(time.Second / 10)
}
}()
go p2.refreshRoutine()
go p2.processLocalChunk()

// 等待路由表节点稳定
for kademliaDHT1.RoutingTable().Size() == 0 {
_, _ = kademliaDHT1.RoutingTable().TryAddPeer(host2.ID(), true, false)
for kademliaDHT1.RoutingTable().Size() == 0 || kademliaDHT2.RoutingTable().Size() == 0 {
time.Sleep(time.Second)
}
// 等待数据过期
Expand Down
16 changes: 14 additions & 2 deletions system/p2p/dht/protocol/peer/peerinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,20 @@ func initEnv(t *testing.T, q queue.Queue) (*Protocol, context.CancelFunc) {
if err != nil {
t.Fatal("connect error", err)
}
_, err = kademliaDHT2.RoutingTable().TryAddPeer(host1.ID(), true, true)
require.Nil(t, err)
go func() {
for {
if kademliaDHT1.RoutingTable().Size() == 0 {
_, err = kademliaDHT1.RoutingTable().TryAddPeer(hosts.ID(), true, false)
require.Nil(t, err)
}
if kademliaDHT2.RoutingTable().Size() == 0 {
_, err = kademliaDHT2.RoutingTable().TryAddPeer(host1.ID(), true, false)
require.Nil(t, err)
}
time.Sleep(time.Second / 10)
}
}()

ps2, err := extension.NewPubSub(ctx, host2, &p2pty.PubSubConfig{})
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 5c736fa

Please sign in to comment.