Search in sources :

Example 51 with Status

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

the class FSMCallerTest method testOnSnapshotLoadStale.

@Test
public void testOnSnapshotLoadStale() throws Exception {
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(5).setLastIncludedTerm(1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals(RaftError.ESTALE, status.getRaftError());
            latch.countDown();
        }

        @Override
        public SnapshotReader start() {
            return reader;
        }
    });
    latch.await();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 10);
}
Also used : LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Status(com.alipay.sofa.jraft.Status) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 52 with Status

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

the class FSMCallerTest method testOnSnapshotLoad.

@Test
public void testOnSnapshotLoad() throws Exception {
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(12).setLastIncludedTerm(1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    Mockito.when(this.fsm.onSnapshotLoad(reader)).thenReturn(true);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }

        @Override
        public SnapshotReader start() {
            return reader;
        }
    });
    latch.await();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 12);
    Mockito.verify(this.fsm).onConfigurationCommitted(Mockito.any());
}
Also used : LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Status(com.alipay.sofa.jraft.Status) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 53 with Status

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

the class IteratorImplTest method testSetErrorAndRollback.

@Test
public void testSetErrorAndRollback() {
    testNext();
    assertFalse(iter.hasError());
    this.iter.setErrorAndRollback(5, new Status(-1, "test"));
    assertTrue(iter.hasError());
    Assert.assertEquals(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE, iter.getError().getType());
    Assert.assertEquals(RaftError.ESTATEMACHINE.getNumber(), iter.getError().getStatus().getCode());
    Assert.assertEquals("StateMachine meet critical error when applying one or more tasks since index=6, Status[UNKNOWN<-1>: test]", iter.getError().getStatus().getErrorMsg());
    assertEquals(6, iter.getIndex());
}
Also used : Status(com.alipay.sofa.jraft.Status) Test(org.junit.Test)

Example 54 with Status

use of com.alipay.sofa.jraft.Status 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 55 with Status

use of com.alipay.sofa.jraft.Status 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

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11