mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
swarm/network/stream: added unit test; rpc test optimizations; PR comments
This commit is contained in:
parent
180c33bd67
commit
961f3b98c1
2 changed files with 68 additions and 10 deletions
|
|
@ -1122,16 +1122,71 @@ func TestRequestPeerSubscriptions(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestGetSubscriptions is a unit test for the api.GetPeerSubscriptions() function
|
||||
func TestGetSubscriptions(t *testing.T) {
|
||||
// create an amount of dummy peers
|
||||
testPeerCount := 8
|
||||
// every peer will have this amount of dummy servers
|
||||
testServerCount := 4
|
||||
// the peerMap which will store this data for the registry
|
||||
peerMap := make(map[enode.ID]*Peer)
|
||||
// create the registry
|
||||
r := &Registry{}
|
||||
api := NewAPI(r)
|
||||
// call once, at this point should be empty
|
||||
regs := api.GetPeerSubscriptions()
|
||||
if len(regs) != 0 {
|
||||
t.Fatal("Expected subscription count to be 0, but it is not")
|
||||
}
|
||||
|
||||
// now create a number of dummy servers for each node
|
||||
for i := 0; i < testPeerCount; i++ {
|
||||
addr := network.RandomAddr()
|
||||
id := addr.ID()
|
||||
p := &Peer{}
|
||||
p.servers = make(map[Stream]*server)
|
||||
for k := 0; k < testServerCount; k++ {
|
||||
s := Stream{
|
||||
Name: strconv.Itoa(k),
|
||||
Key: "",
|
||||
Live: false,
|
||||
}
|
||||
p.servers[s] = &server{}
|
||||
}
|
||||
peerMap[id] = p
|
||||
}
|
||||
r.peers = peerMap
|
||||
|
||||
// call the subscriptions again
|
||||
regs = api.GetPeerSubscriptions()
|
||||
// count how many (fake) subscriptions there are
|
||||
cnt := 0
|
||||
for _, reg := range regs {
|
||||
for range reg {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
// check expected value
|
||||
expectedCount := testPeerCount * testServerCount
|
||||
if cnt != expectedCount {
|
||||
t.Fatalf("Expected %d subscriptions, but got %d", expectedCount, cnt)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
TestGetSubscriptionsRPC sets up a simulation network of 16 nodes,
|
||||
TestGetSubscriptionsRPC sets up a simulation network of `nodeCount` nodes,
|
||||
starts the simulation, waits for SyncUpdateDelay in order to kick off
|
||||
stream registration, then tests that there are subscriptions.
|
||||
*/
|
||||
func TestGetSubscriptionsRPC(t *testing.T) {
|
||||
// arbitrarily set to 16
|
||||
nodeCount := 16
|
||||
// arbitrarily set to 4
|
||||
nodeCount := 4
|
||||
// run with more nodes if `longrunning` flag is set
|
||||
if *longrunning {
|
||||
nodeCount = 64
|
||||
}
|
||||
// set the syncUpdateDelay for sync registrations to start
|
||||
syncUpdateDelay := 500 * time.Millisecond
|
||||
syncUpdateDelay := 200 * time.Millisecond
|
||||
// holds the msg code for SubscribeMsg
|
||||
var subscribeMsgCode uint64
|
||||
var ok bool
|
||||
|
|
@ -1209,13 +1264,15 @@ func TestGetSubscriptionsRPC(t *testing.T) {
|
|||
)
|
||||
|
||||
// strategy: listen to all SubscribeMsg events; after every event we wait
|
||||
// if after 1 second no more messages are being received, we assume the
|
||||
// if after `waitDuration` no more messages are being received, we assume the
|
||||
// subscription phase has terminated!
|
||||
|
||||
// the loop in this go routine will either wait for new message events
|
||||
// or times out after 1 second, which signals that we are not receiving
|
||||
// any new subscriptions any more
|
||||
go func() {
|
||||
//for long running sims, waiting 1 sec will not be enough
|
||||
waitDuration := time.Duration(nodeCount/16) * time.Second
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
|
@ -1226,7 +1283,7 @@ func TestGetSubscriptionsRPC(t *testing.T) {
|
|||
continue
|
||||
}
|
||||
log.Trace("stream message", "node", m.NodeID, "peer", m.PeerID)
|
||||
case <-time.After(time.Second):
|
||||
case <-time.After(waitDuration):
|
||||
// one second passed, don't assume more subscriptions
|
||||
allSubscriptionsDone <- struct{}{}
|
||||
log.Info("All subscriptions received")
|
||||
|
|
@ -1265,11 +1322,11 @@ func TestGetSubscriptionsRPC(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
//length of the subscriptions can not be smaller than number of peers
|
||||
log.Debug(fmt.Sprintf("node %s subscriptions:", node.String()))
|
||||
log.Debug("node subscriptions:", "node", node.String())
|
||||
for p, ps := range pstreams {
|
||||
log.Debug(fmt.Sprintf("...with node %s: ", p))
|
||||
log.Debug("... with: ", "peer", p)
|
||||
for _, s := range ps {
|
||||
log.Debug(fmt.Sprintf("......%s", s))
|
||||
log.Debug(".......", "stream", s)
|
||||
// each node also has subscriptions to RETRIEVE_REQUEST streams,
|
||||
// we need to ignore those, we are only counting SYNC streams
|
||||
if !strings.HasPrefix(s, "RETRIEVE_REQUEST") {
|
||||
|
|
@ -1280,7 +1337,7 @@ func TestGetSubscriptionsRPC(t *testing.T) {
|
|||
}
|
||||
// every node is mutually subscribed to each other, so the actual count is half of it
|
||||
if realCount/2 != expectedMsgCount {
|
||||
return errors.New(fmt.Sprintf("Real subscriptions and expected amount don't match; real: %d, expected: %d", realCount/2, expectedMsgCount))
|
||||
return fmt.Errorf("Real subscriptions and expected amount don't match; real: %d, expected: %d", realCount/2, expectedMsgCount)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
|
|
|||
1
swarm/network/stream/testing/snapshot_4.json
Normal file
1
swarm/network/stream/testing/snapshot_4.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"nodes":[{"node":{"config":{"id":"73d6ad4a75069dced660fa4cb98143ee5573df7cb15d9a295acf1655e9683384","private_key":"e567b7d9c554e5102cdc99b6523bace02dbb8951415c8816d82ba2d2e97fa23b","name":"node01","services":["bzz","pss"],"enable_msg_events":false,"port":0},"up":true}},{"node":{"config":{"id":"6e8da86abb894ab35044c8c455147225df96cab498da067a118f1fb9a417f9e3","private_key":"c7526db70acd02f36d3b201ef3e1d85e38c52bee6931453213dbc5edec4d0976","name":"node02","services":["bzz","pss"],"enable_msg_events":false,"port":0},"up":true}},{"node":{"config":{"id":"8a1eb78ff13df318e7f8116dffee98cd7d9905650fa53f16766b754a63f387ac","private_key":"61b5728f59bc43080c3b8eb0458fb30d7723e2747355b6dc980f35f3ed431199","name":"node03","services":["bzz","pss"],"enable_msg_events":false,"port":0},"up":true}},{"node":{"config":{"id":"d7768334f79d626adb433f44b703a818555e3331056036ef3f8d1282586bf044","private_key":"075b07c29ceac4ffa2a114afd67b21dfc438126bc169bf7c154be6d81d86ed38","name":"node04","services":["bzz","pss"],"enable_msg_events":false,"port":0},"up":true}}],"conns":[{"one":"6e8da86abb894ab35044c8c455147225df96cab498da067a118f1fb9a417f9e3","other":"8a1eb78ff13df318e7f8116dffee98cd7d9905650fa53f16766b754a63f387ac","up":true},{"one":"73d6ad4a75069dced660fa4cb98143ee5573df7cb15d9a295acf1655e9683384","other":"6e8da86abb894ab35044c8c455147225df96cab498da067a118f1fb9a417f9e3","up":true},{"one":"8a1eb78ff13df318e7f8116dffee98cd7d9905650fa53f16766b754a63f387ac","other":"d7768334f79d626adb433f44b703a818555e3331056036ef3f8d1282586bf044","up":true}]}
|
||||
Loading…
Reference in a new issue