github.com/koko1123/flow-go-1@v0.29.6/network/p2p/scoring/subscription_provider_test.go (about) 1 package scoring_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/libp2p/go-libp2p/core/peer" 8 "github.com/stretchr/testify/assert" 9 10 "github.com/koko1123/flow-go-1/network/internal/p2pfixtures" 11 mockp2p "github.com/koko1123/flow-go-1/network/p2p/mock" 12 "github.com/koko1123/flow-go-1/network/p2p/scoring" 13 "github.com/koko1123/flow-go-1/utils/slices" 14 "github.com/koko1123/flow-go-1/utils/unittest" 15 ) 16 17 // TestSubscriptionProvider_GetSubscribedTopics tests that the SubscriptionProvider returns the correct 18 // list of topics a peer is subscribed to. 19 func TestSubscriptionProvider_GetSubscribedTopics(t *testing.T) { 20 tp := mockp2p.NewTopicProvider(t) 21 sp := scoring.NewSubscriptionProvider(unittest.Logger(), tp) 22 23 tp.On("GetTopics").Return([]string{"topic1", "topic2", "topic3"}).Maybe() 24 25 peer1 := p2pfixtures.PeerIdFixture(t) 26 peer2 := p2pfixtures.PeerIdFixture(t) 27 peer3 := p2pfixtures.PeerIdFixture(t) 28 29 // mock peers 1 and 2 subscribed to topic 1 (along with other random peers) 30 tp.On("ListPeers", "topic1").Return(append([]peer.ID{peer1, peer2}, p2pfixtures.PeerIdsFixture(t, 10)...)) 31 // mock peers 2 and 3 subscribed to topic 2 (along with other random peers) 32 tp.On("ListPeers", "topic2").Return(append([]peer.ID{peer2, peer3}, p2pfixtures.PeerIdsFixture(t, 10)...)) 33 // mock peers 1 and 3 subscribed to topic 3 (along with other random peers) 34 tp.On("ListPeers", "topic3").Return(append([]peer.ID{peer1, peer3}, p2pfixtures.PeerIdsFixture(t, 10)...)) 35 36 // As the calls to the TopicProvider are asynchronous, we need to wait for the goroutines to finish. 37 assert.Eventually(t, func() bool { 38 return slices.AreStringSlicesEqual([]string{"topic1", "topic3"}, sp.GetSubscribedTopics(peer1)) 39 }, 1*time.Second, 100*time.Millisecond) 40 41 assert.Eventually(t, func() bool { 42 return slices.AreStringSlicesEqual([]string{"topic1", "topic2"}, sp.GetSubscribedTopics(peer2)) 43 }, 1*time.Second, 100*time.Millisecond) 44 45 assert.Eventually(t, func() bool { 46 return slices.AreStringSlicesEqual([]string{"topic2", "topic3"}, sp.GetSubscribedTopics(peer3)) 47 }, 1*time.Second, 100*time.Millisecond) 48 }