Search in sources :

Example 1 with SaveSnapshotClosure

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

the class SnapshotExecutorTest method testDoSnapshotWithIntervalDist.

@Test
public void testDoSnapshotWithIntervalDist() throws Exception {
    final NodeOptions nodeOptions = new NodeOptions();
    nodeOptions.setSnapshotLogIndexMargin(5);
    Mockito.when(this.node.getOptions()).thenReturn(nodeOptions);
    Mockito.when(this.fSMCaller.getLastAppliedIndex()).thenReturn(6L);
    final ArgumentCaptor<SaveSnapshotClosure> saveSnapshotClosureArg = ArgumentCaptor.forClass(SaveSnapshotClosure.class);
    Mockito.when(this.fSMCaller.onSnapshotSave(saveSnapshotClosureArg.capture())).thenReturn(true);
    final SynchronizedClosure done = new SynchronizedClosure();
    this.executor.doSnapshot(done);
    final SaveSnapshotClosure closure = saveSnapshotClosureArg.getValue();
    assertNotNull(closure);
    closure.start(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(6).setLastIncludedTerm(1).build());
    closure.run(Status.OK());
    done.await();
    this.executor.join();
    assertEquals(1, this.executor.getLastSnapshotTerm());
    assertEquals(6, this.executor.getLastSnapshotIndex());
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) Test(org.junit.Test)

Example 2 with SaveSnapshotClosure

use of com.alipay.sofa.jraft.closure.SaveSnapshotClosure 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 3 with SaveSnapshotClosure

use of com.alipay.sofa.jraft.closure.SaveSnapshotClosure 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 4 with SaveSnapshotClosure

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

the class SnapshotExecutorTest method testDoSnapshot.

@Test
public void testDoSnapshot() throws Exception {
    Mockito.when(this.fSMCaller.getLastAppliedIndex()).thenReturn(1L);
    final ArgumentCaptor<SaveSnapshotClosure> saveSnapshotClosureArg = ArgumentCaptor.forClass(SaveSnapshotClosure.class);
    Mockito.when(this.fSMCaller.onSnapshotSave(saveSnapshotClosureArg.capture())).thenReturn(true);
    final SynchronizedClosure done = new SynchronizedClosure();
    this.executor.doSnapshot(done);
    final SaveSnapshotClosure closure = saveSnapshotClosureArg.getValue();
    assertNotNull(closure);
    closure.start(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(2).setLastIncludedTerm(1).build());
    closure.run(Status.OK());
    done.await();
    this.executor.join();
    assertTrue(done.getStatus().isOk());
    assertEquals(1, this.executor.getLastSnapshotTerm());
    assertEquals(2, this.executor.getLastSnapshotIndex());
}
Also used : SynchronizedClosure(com.alipay.sofa.jraft.closure.SynchronizedClosure) SaveSnapshotClosure(com.alipay.sofa.jraft.closure.SaveSnapshotClosure) Test(org.junit.Test)

Aggregations

SaveSnapshotClosure (com.alipay.sofa.jraft.closure.SaveSnapshotClosure)4 Test (org.junit.Test)4 Status (com.alipay.sofa.jraft.Status)2 SynchronizedClosure (com.alipay.sofa.jraft.closure.SynchronizedClosure)2 SnapshotMeta (com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta)2 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)2 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)1 CountDownLatch (java.util.concurrent.CountDownLatch)1