Skip to content

Commit

Permalink
[FAB-2901] Fixing election test
Browse files Browse the repository at this point in the history
During latest changes leader election callback invocation
was changed from sync to async (go routine). It caused to tests
that check if callback was invoked run before it was actually
invoked. Test changed to handle this - if result is not as expected,
we wait for a while and check again.

Change-Id: Ib26dc5c7d48110c36d39a216e83bcc4a99981389
Signed-off-by: Gennady Laventman <[email protected]>
  • Loading branch information
gennadylaventman committed Mar 29, 2017
1 parent 09115c0 commit 331c0d6
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions gossip/election/election_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func TestInitPeersAtSameTime(t *testing.T) {
leaders := waitForLeaderElection(t, peers)
isP0leader := peers[len(peers)-1].IsLeader()
assert.True(t, isP0leader, "p0 isn't a leader. Leaders are: %v", leaders)
assert.True(t, peers[len(peers)-1].isLeaderFromCallback(), "p0 didn't got leaderhip change callback invoked")
assert.Len(t, leaders, 1, "More than 1 leader elected")
waitForBoolFunc(t, peers[len(peers)-1].isLeaderFromCallback, true, "Leadership callback result is wrong for ", peers[len(peers)-1].id)
}

func TestInitPeersStartAtIntervals(t *testing.T) {
Expand Down Expand Up @@ -289,12 +289,12 @@ func TestConvergence(t *testing.T) {

for _, p := range combinedPeers {
if p.id == finalLeaders[0] {
assert.True(t, p.isLeaderFromCallback(), "Leadership callback result is wrong for ", p.id)
assert.True(t, p.isCallbackInvoked(), "Leadership callback wasn't invoked for ", p.id)
waitForBoolFunc(t, p.isLeaderFromCallback, true, "Leadership callback result is wrong for ", p.id)
waitForBoolFunc(t, p.isCallbackInvoked, true, "Leadership callback wasn't invoked for ", p.id)
} else {
assert.False(t, p.isLeaderFromCallback(), "Leadership callback result is wrong for ", p.id)
waitForBoolFunc(t, p.isLeaderFromCallback, false, "Leadership callback result is wrong for ", p.id)
if p.id == leaders2[0] {
assert.True(t, p.isCallbackInvoked(), "Leadership callback wasn't invoked for ", p.id)
waitForBoolFunc(t, p.isCallbackInvoked, true, "Leadership callback wasn't invoked for ", p.id)
}
}
}
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestPartition(t *testing.T) {
leaders := waitForLeaderElection(t, peers)
assert.Len(t, leaders, 1, "Only 1 leader should have been elected")
assert.Equal(t, "p0", leaders[0])
assert.True(t, peers[len(peers)-1].isLeaderFromCallback(), "Leadership callback result is wrong for %s", peers[len(peers)-1].id)
waitForBoolFunc(t, peers[len(peers)-1].isLeaderFromCallback, true, "Leadership callback result is wrong for %s", peers[len(peers)-1].id)

for _, p := range peers {
p.On("Peers").Return([]Peer{})
Expand All @@ -337,7 +337,7 @@ func TestPartition(t *testing.T) {
leaders = waitForMultipleLeadersElection(t, peers, 6)
assert.Len(t, leaders, 6)
for _, p := range peers {
assert.True(t, p.isLeaderFromCallback(), "Leadership callback result is wrong for %s", p.id)
waitForBoolFunc(t, p.isLeaderFromCallback, true, "Leadership callback result is wrong for %s", p.id)
}

for _, p := range peers {
Expand All @@ -352,10 +352,10 @@ func TestPartition(t *testing.T) {
assert.Equal(t, "p0", leaders[0])
for _, p := range peers {
if p.id == leaders[0] {
assert.True(t, p.isLeaderFromCallback(), "Leadership callback result is wrong for %s", p.id)
waitForBoolFunc(t, p.isLeaderFromCallback, true, "Leadership callback result is wrong for %s", p.id)
} else {
assert.False(t, p.isLeaderFromCallback(), "Leadership callback result is wrong for %s", p.id)
assert.True(t, p.isCallbackInvoked(), "Leadership callback wasn't invoked for %s", p.id)
waitForBoolFunc(t, p.isLeaderFromCallback, false, "Leadership callback result is wrong for %s", p.id)
waitForBoolFunc(t, p.isCallbackInvoked, true, "Leadership callback wasn't invoked for %s", p.id)
}
}

Expand Down Expand Up @@ -398,3 +398,14 @@ func TestConfigFromFile(t *testing.T) {
assert.Equal(t, time.Second*5, getLeaderElectionDuration())
assert.Equal(t, getLeaderAliveThreshold()/2, getLeadershipDeclarationInterval())
}

func waitForBoolFunc(t *testing.T, f func() bool, expectedValue bool, msgAndArgs ...interface{}) {
end := time.Now().Add(testTimeout)
for time.Now().Before(end) {
if f() == expectedValue {
return
}
time.Sleep(testPollInterval)
}
assert.Fail(t, fmt.Sprintf("Should be %t", expectedValue), msgAndArgs...)
}

0 comments on commit 331c0d6

Please sign in to comment.