Skip to content

Commit

Permalink
Fix flaky test LeaderElectionTest (apache#14776)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaozhangmin authored Mar 22, 2022
1 parent a1037c7 commit 46886b0
Showing 1 changed file with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.pulsar.metadata;

import static org.apache.pulsar.metadata.MetadataCacheTest.assertEqualsAndRetry;
import static org.testng.Assert.assertEquals;
import java.util.EnumSet;
import java.util.Optional;
Expand Down Expand Up @@ -111,21 +112,21 @@ public void multipleMembers(String provider, Supplier<String> urlSupplier) throw

LeaderElectionState les1 = le1.elect("test-1").join();
assertEquals(les1, LeaderElectionState.Leading);
assertEquals(le1.getLeaderValueIfPresent(), Optional.of("test-1"));
assertEqualsAndRetry(() -> le1.getLeaderValueIfPresent(), Optional.of("test-1"), Optional.empty());
assertEquals(le1.getLeaderValue().join(), Optional.of("test-1"));
assertEquals(n1.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);

LeaderElectionState les2 = le2.elect("test-2").join();
assertEquals(les2, LeaderElectionState.Following);
assertEquals(le2.getLeaderValue().join(), Optional.of("test-1"));
assertEquals(le2.getLeaderValueIfPresent(), Optional.of("test-1"));
assertEqualsAndRetry(() -> le2.getLeaderValueIfPresent(), Optional.of("test-1"), Optional.empty());
assertEquals(n2.poll(3, TimeUnit.SECONDS), LeaderElectionState.Following);

le1.close();

assertEquals(n2.poll(3, TimeUnit.SECONDS), LeaderElectionState.Leading);
assertEquals(le2.getState(), LeaderElectionState.Leading);
assertEquals(le2.getLeaderValueIfPresent(), Optional.of("test-2"));
assertEqualsAndRetry(() -> le2.getLeaderValueIfPresent(), Optional.of("test-2"), Optional.empty());
assertEquals(le2.getLeaderValue().join(), Optional.of("test-2"));
}

Expand Down Expand Up @@ -209,7 +210,7 @@ public void revalidateLeaderWithinSameSession(String provider, Supplier<String>
LeaderElectionState les = le.elect("test-2").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(le.getLeaderValue().join(), Optional.of("test-2"));
assertEquals(le.getLeaderValueIfPresent(), Optional.of("test-2"));
assertEqualsAndRetry(() -> le.getLeaderValueIfPresent(), Optional.of("test-2"), Optional.empty());
}

@Test(dataProvider = "impl")
Expand Down Expand Up @@ -244,7 +245,7 @@ public void revalidateLeaderWithDifferentSessionsSameValue(String provider, Supp
LeaderElectionState les = le.elect("test-1").join();
assertEquals(les, LeaderElectionState.Leading);
assertEquals(le.getLeaderValue().join(), Optional.of("test-1"));
assertEquals(le.getLeaderValueIfPresent(), Optional.of("test-1"));
assertEqualsAndRetry(() -> le.getLeaderValueIfPresent(), Optional.of("test-1"), Optional.empty());
}


Expand Down Expand Up @@ -280,6 +281,6 @@ public void revalidateLeaderWithDifferentSessionsDifferentValue(String provider,
LeaderElectionState les = le.elect("test-2").join();
assertEquals(les, LeaderElectionState.Following);
assertEquals(le.getLeaderValue().join(), Optional.of("test-1"));
assertEquals(le.getLeaderValueIfPresent(), Optional.of("test-1"));
assertEqualsAndRetry(() -> le.getLeaderValueIfPresent(), Optional.of("test-1"), Optional.empty());
}
}

0 comments on commit 46886b0

Please sign in to comment.