github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/channels/channels_test.go (about) 1 package channels 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 "github.com/onflow/flow-go/model/flow" 10 ) 11 12 // TestGetRolesByChannel_NonClusterChannel evaluates correctness of RolesByChannel function against 13 // inclusion and exclusion of roles. Essentially, the test evaluates that RolesByChannel 14 // operates on top of channelRoleMap. 15 func TestGetRolesByChannel_NonClusterChannel(t *testing.T) { 16 // asserts existing topic with its role 17 // the roles list should contain collection and consensus roles 18 roles, ok := RolesByChannel(PushGuarantees) 19 assert.True(t, ok) 20 assert.Len(t, roles, 2) 21 assert.Contains(t, roles, flow.RoleConsensus) 22 assert.Contains(t, roles, flow.RoleCollection) 23 assert.NotContains(t, roles, flow.RoleExecution) 24 assert.NotContains(t, roles, flow.RoleVerification) 25 assert.NotContains(t, roles, flow.RoleAccess) 26 27 // asserts a non-existing topic 28 roles, ok = RolesByChannel("non-existing-topic") 29 assert.False(t, ok) 30 assert.Nil(t, roles) 31 } 32 33 // TestGetRolesByChannel_ClusterChannel evaluates correctness of RolesByChannel function against 34 // cluster channels. Essentially, the test evaluates that RolesByChannel 35 // operates on top of channelRoleMap, and correctly identifies and strips of the cluster channel. 36 func TestGetRolesByChannel_ClusterChannel(t *testing.T) { 37 // creates a cluster channel. 38 conClusterChannel := ConsensusCluster("some-consensus-cluster-id") 39 40 // the roles list should contain collection 41 roles, ok := RolesByChannel(conClusterChannel) 42 assert.True(t, ok) 43 assert.Len(t, roles, 1) 44 assert.Contains(t, roles, flow.RoleCollection) 45 } 46 47 // TestGetChannelByRole evaluates retrieving channels associated with a role from the 48 // channelRoleMap using ChannelsByRole. Essentially it evaluates that ChannelsByRole 49 // operates on top of channelRoleMap. 50 func TestGetChannelByRole(t *testing.T) { 51 // asserts topics by the role for verification node 52 // it should have the topics of 53 // - PushBlocks 54 // - PushReceipts 55 // - PushApprovals 56 // - ProvideApprovalsByChunk 57 // - ProvideChunks 58 // - TestNetworkChannel 59 // - TestMetric 60 // the roles list should contain collection and consensus roles 61 topics := ChannelsByRole(flow.RoleVerification) 62 assert.Len(t, topics, 8) 63 assert.Contains(t, topics, PushBlocks) 64 assert.Contains(t, topics, PushReceipts) 65 assert.Contains(t, topics, PushApprovals) 66 assert.Contains(t, topics, ProvideApprovalsByChunk) 67 assert.Contains(t, topics, RequestChunks) 68 assert.Contains(t, topics, TestMetricsChannel) 69 assert.Contains(t, topics, TestNetworkChannel) 70 assert.Contains(t, topics, SyncCommittee) 71 } 72 73 // TestIsClusterChannel verifies the correctness of ClusterChannel method 74 // against cluster and non-cluster channel. 75 func TestIsClusterChannel(t *testing.T) { 76 // creates a consensus cluster channel and verifies it 77 conClusterChannel := ConsensusCluster("some-consensus-cluster-id") 78 ok := IsClusterChannel(conClusterChannel) 79 require.True(t, ok) 80 81 // creates a sync cluster channel and verifies it 82 syncClusterChannel := SyncCluster("some-sync-cluster-id") 83 ok = IsClusterChannel(syncClusterChannel) 84 require.True(t, ok) 85 86 // non-cluster channel should not be verified 87 ok = IsClusterChannel("non-cluster-channel-id") 88 require.False(t, ok) 89 } 90 91 // TestUniqueChannels_Uniqueness verifies that non-cluster channels returned by 92 // UniqueChannels are unique based on their set of involved roles. 93 // We use the identifier of RoleList to determine their uniqueness. 94 func TestUniqueChannels_Uniqueness(t *testing.T) { 95 for _, role := range flow.Roles() { 96 uniques := UniqueChannels(ChannelsByRole(role)) 97 98 visited := make(map[flow.Identifier]struct{}) 99 for _, channel := range uniques { 100 101 if IsClusterChannel(channel) { 102 continue //only considering non-cluster channel in this test case 103 } 104 105 // non-cluster channels should be unique based on their RoleList identifier. 106 id := channelRoleMap[channel].ID() 107 _, duplicate := visited[id] 108 require.False(t, duplicate) 109 110 visited[id] = struct{}{} 111 } 112 } 113 } 114 115 // TestUniqueChannels_ClusterChannels verifies that if cluster channels have the RoleList the same as 116 // single non-cluster channel, then all cluster channels as well as the one non-cluster channel are returned 117 // by the UniqueChannels. In other words, neither cluster channels nor non-cluster ones are de-duplicated in the 118 // favor of each other. 119 // We use the identifier of RoleList to determine their uniqueness. 120 func TestUniqueChannels_ClusterChannels(t *testing.T) { 121 channels := ChannelsByRole(flow.RoleCollection) 122 consensusCluster := ConsensusCluster(flow.Emulator) 123 syncCluster := SyncCluster(flow.Emulator) 124 channels = append(channels, consensusCluster, syncCluster) 125 uniques := UniqueChannels(channels) 126 // collection role has two cluster and one non-cluster channels all with the same RoleList. 127 // Hence all of them should be returned as unique channels. 128 require.Contains(t, uniques, syncCluster) // cluster channel 129 require.Contains(t, uniques, consensusCluster) // cluster channel 130 require.Contains(t, uniques, PushTransactions) // non-cluster channel 131 }