Search in sources :

Example 1 with Leadership

use of io.atomix.core.election.Leadership in project atomix by atomix.

the class LeaderElectorTest method testWithdraw.

@Test
public void testWithdraw() throws Throwable {
    AsyncLeaderElector<NodeId> elector1 = atomix().<NodeId>leaderElectorBuilder("test-elector-withdraw", protocol()).build().async();
    elector1.run("foo", node1).join();
    AsyncLeaderElector<NodeId> elector2 = atomix().<NodeId>leaderElectorBuilder("test-elector-withdraw", protocol()).build().async();
    elector2.run("foo", node2).join();
    LeaderEventListener listener1 = new LeaderEventListener();
    elector1.addListener("foo", listener1).join();
    LeaderEventListener listener2 = new LeaderEventListener();
    elector2.addListener("foo", listener2).join();
    LeaderEventListener listener3 = new LeaderEventListener();
    elector1.addListener("bar", listener3);
    elector2.addListener("bar", listener3);
    elector1.withdraw("foo", node1).join();
    listener1.nextEvent().thenAccept(result -> {
        assertEquals(node2, result.newLeadership().leader().id());
        assertEquals(2, result.newLeadership().leader().term());
        assertEquals(1, result.newLeadership().candidates().size());
        assertEquals(node2, result.newLeadership().candidates().get(0));
    }).join();
    listener2.nextEvent().thenAccept(result -> {
        assertEquals(node2, result.newLeadership().leader().id());
        assertEquals(2, result.newLeadership().leader().term());
        assertEquals(1, result.newLeadership().candidates().size());
        assertEquals(node2, result.newLeadership().candidates().get(0));
    }).join();
    assertFalse(listener3.hasEvent());
    Leadership leadership1 = elector1.getLeadership("foo").join();
    assertEquals(node2, leadership1.leader().id());
    assertEquals(1, leadership1.candidates().size());
    Leadership leadership2 = elector2.getLeadership("foo").join();
    assertEquals(node2, leadership2.leader().id());
    assertEquals(1, leadership2.candidates().size());
}
Also used : NodeId(io.atomix.cluster.NodeId) AsyncLeaderElector(io.atomix.core.election.AsyncLeaderElector) LeadershipEvent(io.atomix.core.election.LeadershipEvent) LeadershipEventListener(io.atomix.core.election.LeadershipEventListener) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Queue(java.util.Queue) LinkedList(java.util.LinkedList) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest) Leadership(io.atomix.core.election.Leadership) Assert.assertEquals(org.junit.Assert.assertEquals) Leadership(io.atomix.core.election.Leadership) NodeId(io.atomix.cluster.NodeId) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Aggregations

NodeId (io.atomix.cluster.NodeId)1 AbstractPrimitiveTest (io.atomix.core.AbstractPrimitiveTest)1 AsyncLeaderElector (io.atomix.core.election.AsyncLeaderElector)1 Leadership (io.atomix.core.election.Leadership)1 LeadershipEvent (io.atomix.core.election.LeadershipEvent)1 LeadershipEventListener (io.atomix.core.election.LeadershipEventListener)1 LinkedList (java.util.LinkedList)1 Queue (java.util.Queue)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1