Search in sources :

Example 1 with SnapshotMeta

use of com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta in project sofa-jraft by sofastack.

the class FSMCallerTest method testOnSnapshotSaveEmptyConf.

@Test
public void testOnSnapshotSaveEmptyConf() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotSave(new SaveSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals("Empty conf entry for lastAppliedIndex=10", status.getErrorMsg());
            latch.countDown();
        }

        @Override
        public SnapshotWriter start(final SnapshotMeta meta) {
            // TODO Auto-generated method stub
            return null;
        }
    });
    latch.await();
}
Also used : Status(com.alipay.sofa.jraft.Status) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with SnapshotMeta

use of com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta 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 3 with SnapshotMeta

use of com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta 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 4 with SnapshotMeta

use of com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta in project sofa-jraft by sofastack.

the class FSMCallerTest method testOnSnapshotSave.

@Test
public void testOnSnapshotSave() throws Exception {
    final SnapshotWriter writer = Mockito.mock(SnapshotWriter.class);
    Mockito.when(this.logManager.getConfiguration(10)).thenReturn(TestUtils.getConfEntry("localhost:8081,localhost:8082,localhost:8083", "localhost:8081"));
    final SaveSnapshotClosure done = new SaveSnapshotClosure() {

        @Override
        public void run(final Status status) {
        }

        @Override
        public SnapshotWriter start(final SnapshotMeta meta) {
            assertEquals(10, meta.getLastIncludedIndex());
            return writer;
        }
    };
    this.fsmCaller.onSnapshotSave(done);
    this.fsmCaller.flush();
    Mockito.verify(this.fsm).onSnapshotSave(writer, done);
}
Also used : Status(com.alipay.sofa.jraft.Status) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) Test(org.junit.Test)

Example 5 with SnapshotMeta

use of com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta in project sofa-jraft by sofastack.

the class FSMCallerTest method testOnSnapshotLoadFSMError.

@Test
public void testOnSnapshotLoadFSMError() 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(false);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals(-1, status.getCode());
            assertEquals("StateMachine onSnapshotLoad failed", status.getErrorMsg());
            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)

Aggregations

SnapshotMeta (com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta)8 Test (org.junit.Test)7 Status (com.alipay.sofa.jraft.Status)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 LoadSnapshotClosure (com.alipay.sofa.jraft.closure.LoadSnapshotClosure)3 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)3 SaveSnapshotClosure (com.alipay.sofa.jraft.closure.SaveSnapshotClosure)2 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)2