addPeer waits only for add event, because we're adding a peer to the prio client

This commit is contained in:
Adam Schmideg 2020-02-13 13:54:20 +01:00
parent 16a2401cff
commit c267cee021

View file

@ -29,7 +29,7 @@ func (g *gethrpc) callRPC(result interface{}, method string, args ...interface{}
} }
} }
func (g *gethrpc) addPeer(peer *gethrpc, eventsToWait int) { func (g *gethrpc) addPeer(peer *gethrpc) {
g.geth.Logf("%v.addPeer(%v)", g.name, peer.name) g.geth.Logf("%v.addPeer(%v)", g.name, peer.name)
enode := peer.getNodeInfo().Enode enode := peer.getNodeInfo().Enode
peerCh := make(chan *p2p.PeerEvent) peerCh := make(chan *p2p.PeerEvent)
@ -41,17 +41,13 @@ func (g *gethrpc) addPeer(peer *gethrpc, eventsToWait int) {
g.callRPC(nil, "admin_addPeer", enode) g.callRPC(nil, "admin_addPeer", enode)
dur := 14 * time.Second dur := 14 * time.Second
timeout := time.After(dur) timeout := time.After(dur)
for i := 0; i < eventsToWait; i++ { select {
select { case ev := <-peerCh:
case ev := <-peerCh: g.geth.Logf("%v received event: type=%v, peer=%v", g.name, ev.Type, ev.Peer)
g.geth.Logf("%v received event: type=%v, peer=%v", g.name, ev.Type, ev.Peer) case err := <-sub.Err():
case err := <-sub.Err(): g.geth.Fatalf("%v sub error: %v", g.name, err)
g.geth.Fatalf("%v sub error: %v", g.name, err) case <-timeout:
return g.geth.Error("timeout adding peer after", dur)
case <-timeout:
g.geth.Error("timeout adding peer after", dur)
return
}
} }
} }
@ -136,7 +132,7 @@ func startLightServer(t *testing.T) *gethrpc {
} }
func startClient(t *testing.T, name string) *gethrpc { func startClient(t *testing.T, name string) *gethrpc {
runGeth(t, "init", "./testdata/genesis.json").WaitExit() runGeth(t, "init", "./testdata/genesis.json").WaitExit()
g := startGethWithRpc(t, name, "--nodiscover", "--syncmode=light") g := startGethWithRpc(t, name, "--nodiscover", "--syncmode=light", "--nat=extip:127.0.0.1")
return g return g
} }
@ -151,32 +147,30 @@ func TestPriorityClient(t *testing.T) {
// Make the lightServer sync to the server // Make the lightServer sync to the server
// This is the only way to make the lightServer synced // This is the only way to make the lightServer synced
lightServer.addPeer(server, 1) lightServer.addPeer(server)
lightServer.waitSynced() lightServer.waitSynced()
// Start client and add lightServer as peer // Start client and add lightServer as peer
client := startClient(t, "client") freeCli := startClient(t, "freeCli")
defer client.killAndWait() defer freeCli.killAndWait()
client.addPeer(lightServer, 1) freeCli.addPeer(lightServer)
var peers []*p2p.PeerInfo var peers []*p2p.PeerInfo
client.callRPC(&peers, "admin_peers") freeCli.callRPC(&peers, "admin_peers")
if len(peers) != 1 { if len(peers) != 1 {
t.Errorf("Expected: # of client peers == 1, actual: %v", len(peers)) t.Errorf("Expected: # of client peers == 1, actual: %v", len(peers))
return return
} }
// Set up priority client, get its nodeID, increase its balance on the lightServer // Set up priority client, get its nodeID, increase its balance on the lightServer
prio := startClient(t, "prio") prioCli := startClient(t, "prioCli")
defer prio.killAndWait() defer prioCli.killAndWait()
// 3_000_000_000 once we move to Go 1.13 // 3_000_000_000 once we move to Go 1.13
tokens := 3000000000 tokens := 3000000000
lightServer.callRPC(nil, "les_addBalance", prio.getNodeInfo().ID, tokens, "foobar") lightServer.callRPC(nil, "les_addBalance", prioCli.getNodeInfo().ID, tokens, "foobar")
// We expect two events, adding prio and removing the old client prioCli.addPeer(lightServer)
// TODO: s/1/2/ or understand why we don't receive a message when client is removed
prio.addPeer(lightServer, 1)
// Check if priority client is actually syncing and the regular client got kicked out // Check if priority client is actually syncing and the regular client got kicked out
prio.callRPC(&peers, "admin_peers") prioCli.callRPC(&peers, "admin_peers")
if len(peers) != 1 { if len(peers) != 1 {
t.Errorf("Expected: # of prio peers == 1, actual: %v", len(peers)) t.Errorf("Expected: # of prio peers == 1, actual: %v", len(peers))
} }
@ -184,23 +178,18 @@ func TestPriorityClient(t *testing.T) {
nodes := map[string]*gethrpc{ nodes := map[string]*gethrpc{
server.getNodeInfo().ID: server, server.getNodeInfo().ID: server,
lightServer.getNodeInfo().ID: lightServer, lightServer.getNodeInfo().ID: lightServer,
client.getNodeInfo().ID: client, freeCli.getNodeInfo().ID: freeCli,
prio.getNodeInfo().ID: prio, prioCli.getNodeInfo().ID: prioCli,
} }
lightServer.callRPC(&peers, "admin_peers") lightServer.callRPC(&peers, "admin_peers")
peersWithNames := make(map[string]string) peersWithNames := make(map[string]string)
for _, p := range peers { for _, p := range peers {
peersWithNames[nodes[p.ID].name] = p.ID peersWithNames[nodes[p.ID].name] = p.ID
} }
if _, freeClientFound := peersWithNames[client.name]; freeClientFound { if _, freeClientFound := peersWithNames[freeCli.name]; freeClientFound {
t.Error("client is still a peer of lightServer", peersWithNames) t.Error("client is still a peer of lightServer", peersWithNames)
} }
if _, prioClientFound := peersWithNames[prio.name]; !prioClientFound { if _, prioClientFound := peersWithNames[prioCli.name]; !prioClientFound {
t.Error("prio client is not among lightServer peers", peersWithNames) t.Error("prio client is not among lightServer peers", peersWithNames)
} }
client.callRPC(&peers, "admin_peers")
if len(peers) != 0 {
t.Errorf("Expected: # of client peers == 0, actual: %v", len(peers))
}
} }