-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FABG-724] Don't Query all the peers for selection
Change-Id: I17805a695f61e04d923cf75e7249d7e7524c54c7 Signed-off-by: Sandra Vrtikapa <[email protected]>
- Loading branch information
Showing
5 changed files
with
174 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package random | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" | ||
) | ||
|
||
//PickRandomNPeerConfigs picks N random unique peer configs from given channel peer list | ||
func PickRandomNPeerConfigs(chPeers []fab.ChannelPeer, n int) []fab.PeerConfig { | ||
|
||
var result []fab.PeerConfig | ||
for _, index := range rand.Perm(len(chPeers)) { | ||
result = append(result, chPeers[index].PeerConfig) | ||
if len(result) == n { | ||
break | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package random | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
pfab "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPickRandomNPeerConfigs(t *testing.T) { | ||
counter := 20 | ||
allChPeers := createNChannelPeers(counter) | ||
|
||
result := PickRandomNPeerConfigs(allChPeers, 4) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 4, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 1) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 1, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 19) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 19, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 20) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 20, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 21) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 20, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 24) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 20, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
counter = 7 | ||
allChPeers = createNChannelPeers(counter) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 6) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 6, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 7) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 7, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 8) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 7, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
counter = 2 | ||
allChPeers = createNChannelPeers(counter) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 2) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 2, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 24) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 2, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
counter = 1 | ||
allChPeers = createNChannelPeers(counter) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 1) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 1, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 2) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 1, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
result = PickRandomNPeerConfigs(allChPeers, 24) | ||
assert.NotNil(t, result) | ||
assert.NotEmpty(t, result) | ||
assert.Equal(t, 1, len(result)) | ||
verifyDuplicates(t, result) | ||
|
||
} | ||
|
||
func createNChannelPeers(n int) []pfab.ChannelPeer { | ||
allChPeers := make([]pfab.ChannelPeer, n) | ||
for i := 0; i < n; i++ { | ||
allChPeers[i] = pfab.ChannelPeer{ | ||
NetworkPeer: pfab.NetworkPeer{ | ||
PeerConfig: pfab.PeerConfig{URL: fmt.Sprintf("URL-%d", i)}, | ||
}, | ||
} | ||
} | ||
return allChPeers | ||
} | ||
|
||
func verifyDuplicates(t *testing.T, chPeers []pfab.PeerConfig) { | ||
seen := make(map[string]bool) | ||
for _, v := range chPeers { | ||
if seen[v.URL] { | ||
t.Fatalf("found duplicate channel peer: %s", v.URL) | ||
} | ||
seen[v.URL] = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters