Search in sources :

Example 6 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class AddPeerRequestProcessorTest method verify.

@Override
public void verify(String interest, Node node, ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, AddPeerRequest.class.getName());
    Mockito.verify(node).addPeer(eq(new PeerId("test", 8181)), doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083]", this.asyncContext.as(AddPeerResponse.class).getOldPeersList().toString());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083, test:8181]", this.asyncContext.as(AddPeerResponse.class).getNewPeersList().toString());
}
Also used : Closure(com.alipay.sofa.jraft.Closure) AddPeerRequest(com.alipay.sofa.jraft.rpc.CliRequests.AddPeerRequest) AddPeerResponse(com.alipay.sofa.jraft.rpc.CliRequests.AddPeerResponse) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 7 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class RemoveLearnersRequestProcessorTest method verify.

@Override
public void verify(final String interest, final Node node, final ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, RemoveLearnersRequest.class.getName());
    Mockito.verify(node).removeLearners(eq(Arrays.asList(new PeerId("learner", 8082), new PeerId("test", 8183))), doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[learner:8081, learner:8082, learner:8083]", this.asyncContext.as(LearnersOpResponse.class).getOldLearnersList().toString());
    assertEquals("[learner:8081, learner:8083]", this.asyncContext.as(LearnersOpResponse.class).getNewLearnersList().toString());
}
Also used : RemoveLearnersRequest(com.alipay.sofa.jraft.rpc.CliRequests.RemoveLearnersRequest) Closure(com.alipay.sofa.jraft.Closure) LearnersOpResponse(com.alipay.sofa.jraft.rpc.CliRequests.LearnersOpResponse) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 8 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class RemovePeerRequestProcessorTest method verify.

@Override
public void verify(String interest, Node node, ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, RemovePeerRequest.class.getName());
    Mockito.verify(node).removePeer(eq(new PeerId("localhost", 8082)), doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083]", this.asyncContext.as(RemovePeerResponse.class).getOldPeersList().toString());
    assertEquals("[localhost:8081, localhost:8083]", this.asyncContext.as(RemovePeerResponse.class).getNewPeersList().toString());
}
Also used : Closure(com.alipay.sofa.jraft.Closure) RemovePeerResponse(com.alipay.sofa.jraft.rpc.CliRequests.RemovePeerResponse) RemovePeerRequest(com.alipay.sofa.jraft.rpc.CliRequests.RemovePeerRequest) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 9 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class IteratorImplTest method testRunTheRestClosureWithError.

@Test
public void testRunTheRestClosureWithError() throws Exception {
    testSetErrorAndRollback();
    for (final Closure closure : this.closures) {
        final MockClosure mc = (MockClosure) closure;
        assertNull(mc.s);
    }
    this.iter.runTheRestClosureWithError();
    Thread.sleep(500);
    int i = 0;
    for (final Closure closure : this.closures) {
        i++;
        final MockClosure mc = (MockClosure) closure;
        if (i < 7) {
            assertNull(mc.s);
        } else {
            final Status s = mc.s;
            Assert.assertEquals(RaftError.ESTATEMACHINE.getNumber(), s.getCode());
            assertEquals("StateMachine meet critical error when applying one or more tasks since index=6, Status[UNKNOWN<-1>: test]", s.getErrorMsg());
        }
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Closure(com.alipay.sofa.jraft.Closure) Test(org.junit.Test)

Example 10 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class BallotBoxTest method testCommitAt.

@Test
public void testCommitAt() {
    assertFalse(this.box.commitAt(1, 3, new PeerId("localhost", 8081)));
    assertTrue(box.resetPendingIndex(1));
    assertTrue(this.box.appendPendingTask(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083"), JRaftUtils.getConfiguration("localhost:8081"), new Closure() {

        @Override
        public void run(Status status) {
        }
    }));
    assertEquals(0, this.box.getLastCommittedIndex());
    try {
        this.box.commitAt(1, 3, new PeerId("localhost", 8081));
        fail();
    } catch (ArrayIndexOutOfBoundsException e) {
    }
    assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8081)));
    assertEquals(0, this.box.getLastCommittedIndex());
    assertEquals(1, this.box.getPendingIndex());
    assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8082)));
    assertEquals(1, this.box.getLastCommittedIndex());
    assertEquals(2, this.box.getPendingIndex());
    Mockito.verify(this.waiter, Mockito.only()).onCommitted(1);
}
Also used : Status(com.alipay.sofa.jraft.Status) Closure(com.alipay.sofa.jraft.Closure) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

Closure (com.alipay.sofa.jraft.Closure)28 Status (com.alipay.sofa.jraft.Status)16 PeerId (com.alipay.sofa.jraft.entity.PeerId)11 List (java.util.List)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ArrayList (java.util.ArrayList)7 Configuration (com.alipay.sofa.jraft.conf.Configuration)6 Map (java.util.Map)6 RaftError (com.alipay.sofa.jraft.error.RaftError)5 TimeUnit (java.util.concurrent.TimeUnit)5 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)4 Node (com.alipay.sofa.jraft.Node)3 LeaderChangeContext (com.alipay.sofa.jraft.entity.LeaderChangeContext)3 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 LogId (com.alipay.sofa.jraft.entity.LogId)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)3 KeyValueTool.makeKey (com.alipay.sofa.jraft.rhea.KeyValueTool.makeKey)3