Skip to content

Commit 5eb5d18

Browse files
committed
[FAB-15463] Wait for full channel membership
Tests that use the leader election process should wait for full membership on the channel of the election before the election process starts. Otherwise, there could be cases where there would be 2 leaders on the same channel and the test might fail or wait longer for one of them to renounce its leadership. In addition, in TestLeaderElectionWithRealGossip(), a wait was added before the leader election in the second channel. Change-Id: I0f7dd2a66f5150bed14efbc34504fdfddae62049 Signed-off-by: ronenschafferibm <ronen.schaffer@ibm.com>
1 parent cfa5eb5 commit 5eb5d18

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

gossip/service/gossip_service_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestLeaderElectionWithDeliverClient(t *testing.T) {
140140
}
141141
addPeersToChannel(t, n, channelName, gossips, peerIndexes)
142142

143-
waitForFullMembershipOrFailNow(t, gossips, n, time.Second*20, time.Second*2)
143+
waitForFullMembershipOrFailNow(t, channelName, gossips, n, time.Second*20, time.Second*2)
144144

145145
services := make([]*electionService, n)
146146

@@ -200,7 +200,7 @@ func TestWithStaticDeliverClientLeader(t *testing.T) {
200200

201201
addPeersToChannel(t, n, channelName, gossips, peerIndexes)
202202

203-
waitForFullMembershipOrFailNow(t, gossips, n, time.Second*30, time.Second*2)
203+
waitForFullMembershipOrFailNow(t, channelName, gossips, n, time.Second*30, time.Second*2)
204204

205205
deliverServiceFactory := &mockDeliverServiceFactory{
206206
service: &mockDeliverService{
@@ -254,7 +254,7 @@ func TestWithStaticDeliverClientNotLeader(t *testing.T) {
254254

255255
addPeersToChannel(t, n, channelName, gossips, peerIndexes)
256256

257-
waitForFullMembershipOrFailNow(t, gossips, n, time.Second*30, time.Second*2)
257+
waitForFullMembershipOrFailNow(t, channelName, gossips, n, time.Second*30, time.Second*2)
258258

259259
deliverServiceFactory := &mockDeliverServiceFactory{
260260
service: &mockDeliverService{
@@ -294,7 +294,7 @@ func TestWithStaticDeliverClientBothStaticAndLeaderElection(t *testing.T) {
294294

295295
addPeersToChannel(t, n, channelName, gossips, peerIndexes)
296296

297-
waitForFullMembershipOrFailNow(t, gossips, n, time.Second*30, time.Second*2)
297+
waitForFullMembershipOrFailNow(t, channelName, gossips, n, time.Second*30, time.Second*2)
298298

299299
deliverServiceFactory := &mockDeliverServiceFactory{
300300
service: &mockDeliverService{
@@ -412,7 +412,7 @@ func TestLeaderElectionWithRealGossip(t *testing.T) {
412412
}
413413
addPeersToChannel(t, n, channelName, gossips, peerIndexes)
414414

415-
waitForFullMembershipOrFailNow(t, gossips, n, time.Second*30, time.Second*2)
415+
waitForFullMembershipOrFailNow(t, channelName, gossips, n, time.Second*30, time.Second*2)
416416

417417
logger.Warning("Starting leader election services")
418418

@@ -448,6 +448,12 @@ func TestLeaderElectionWithRealGossip(t *testing.T) {
448448
secondChannelServices := make([]*electionService, len(secondChannelPeerIndexes))
449449
addPeersToChannel(t, n, secondChannelName, gossips, secondChannelPeerIndexes)
450450

451+
secondChannelGossips := make([]GossipService, 0)
452+
for _, i := range secondChannelPeerIndexes {
453+
secondChannelGossips = append(secondChannelGossips, gossips[i])
454+
}
455+
waitForFullMembershipOrFailNow(t, secondChannelName, secondChannelGossips, len(secondChannelGossips), time.Second*30, time.Millisecond*100)
456+
451457
for idx, i := range secondChannelPeerIndexes {
452458
secondChannelServices[idx] = &electionService{nil, false, 0}
453459
secondChannelServices[idx].LeaderElectionService =
@@ -481,7 +487,8 @@ func TestLeaderElectionWithRealGossip(t *testing.T) {
481487

482488
stopPeers(gossips[:2])
483489

484-
waitForFullMembershipOrFailNow(t, gossips[2:], n-2, time.Second*30, time.Second*2)
490+
waitForFullMembershipOrFailNow(t, channelName, gossips[2:], n-2, time.Second*30, time.Millisecond*100)
491+
waitForFullMembershipOrFailNow(t, secondChannelName, secondChannelGossips[1:], len(secondChannelGossips)-1, time.Second*30, time.Millisecond*100)
485492

486493
assert.True(t, waitForLeaderElection(t, services[2:], time.Second*30, time.Second*2), "One leader should be selected after re-election - chanA")
487494
assert.True(t, waitForLeaderElection(t, secondChannelServices[1:], time.Second*30, time.Second*2), "One leader should be selected after re-election - chanB")
@@ -537,13 +544,13 @@ func (jmc *joinChanMsg) AnchorPeersOf(org api.OrgIdentityType) []api.AnchorPeer
537544
return []api.AnchorPeer{}
538545
}
539546

540-
func waitForFullMembershipOrFailNow(t *testing.T, gossips []GossipService, peersNum int, timeout time.Duration, testPollInterval time.Duration) {
547+
func waitForFullMembershipOrFailNow(t *testing.T, channel string, gossips []GossipService, peersNum int, timeout time.Duration, testPollInterval time.Duration) {
541548
end := time.Now().Add(timeout)
542549
var correctPeers int
543550
for time.Now().Before(end) {
544551
correctPeers = 0
545552
for _, g := range gossips {
546-
if len(g.Peers()) == (peersNum - 1) {
553+
if len(g.PeersOfChannel(gossipCommon.ChainID(channel))) == (peersNum - 1) {
547554
correctPeers++
548555
}
549556
}
@@ -552,7 +559,7 @@ func waitForFullMembershipOrFailNow(t *testing.T, gossips []GossipService, peers
552559
}
553560
time.Sleep(testPollInterval)
554561
}
555-
t.Fatalf("Failed to establish full membership. Only %d out of %d peers have full membership", correctPeers, peersNum)
562+
t.Fatalf("Failed to establish full channel membership. Only %d out of %d peers have full membership", correctPeers, peersNum)
556563
}
557564

558565
func waitForMultipleLeadersElection(t *testing.T, services []*electionService, leadersNum int, timeout time.Duration, testPollInterval time.Duration) bool {

gossip/service/integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func TestLeaderYield(t *testing.T) {
126126
// Add peers to the channel
127127
addPeersToChannel(t, n, channelName, gossips, peerIndexes)
128128
// Prime the membership view of the peers
129-
waitForFullMembershipOrFailNow(t, gossips, n, time.Second*30, time.Millisecond*100)
129+
waitForFullMembershipOrFailNow(t, channelName, gossips, n, time.Second*30, time.Millisecond*100)
130130

131131
endpoint, socket := getAvailablePort(t)
132132
socket.Close()

0 commit comments

Comments
 (0)