Search in sources :

Example 1 with MockAsyncContext

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

the class SnapshotExecutorTest method testRetryInstallSnapshot.

@Test
public void testRetryInstallSnapshot() throws Exception {
    final RpcRequests.InstallSnapshotRequest.Builder irb = RpcRequests.InstallSnapshotRequest.newBuilder();
    irb.setGroupId("test");
    irb.setPeerId(this.addr.toString());
    irb.setServerId("localhost:8080");
    irb.setUri("remote://localhost:8080/99");
    irb.setTerm(0);
    irb.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(2));
    Mockito.when(this.raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
    final FutureImpl<Message> future = new FutureImpl<>();
    final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
    // mock get metadata
    ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    final CountDownLatch retryLatch = new CountDownLatch(1);
    final CountDownLatch answerLatch = new CountDownLatch(1);
    Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenAnswer(new Answer<Future<Message>>() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public Future<Message> answer(InvocationOnMock invocation) throws Throwable {
            if (count.incrementAndGet() == 1) {
                retryLatch.countDown();
                answerLatch.await();
                Thread.sleep(1000);
                return future;
            } else {
                throw new IllegalStateException("shouldn't be called more than once");
            }
        }
    });
    final MockAsyncContext installContext = new MockAsyncContext();
    final MockAsyncContext retryInstallContext = new MockAsyncContext();
    Utils.runInThread(new Runnable() {

        @Override
        public void run() {
            SnapshotExecutorTest.this.executor.installSnapshot(irb.build(), RpcRequests.InstallSnapshotResponse.newBuilder(), new RpcRequestClosure(installContext));
        }
    });
    Thread.sleep(500);
    retryLatch.await();
    Utils.runInThread(new Runnable() {

        @Override
        public void run() {
            answerLatch.countDown();
            SnapshotExecutorTest.this.executor.installSnapshot(irb.build(), RpcRequests.InstallSnapshotResponse.newBuilder(), new RpcRequestClosure(retryInstallContext));
        }
    });
    RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
    final ByteBuffer metaBuf = this.table.saveToByteBufferAsRemote();
    closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(metaBuf.remaining()).setEof(true).setData(ByteString.copyFrom(metaBuf)).build());
    // mock get file
    argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    rb.setFilename("testFile");
    rb.setCount(this.raftOptions.getMaxByteCountPerRpc());
    Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    closure.run(Status.OK());
    Thread.sleep(500);
    closure = argument.getValue();
    closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(true).setData(ByteString.copyFrom(new byte[100])).build());
    final ArgumentCaptor<LoadSnapshotClosure> loadSnapshotArg = ArgumentCaptor.forClass(LoadSnapshotClosure.class);
    Mockito.when(this.fSMCaller.onSnapshotLoad(loadSnapshotArg.capture())).thenReturn(true);
    closure.run(Status.OK());
    Thread.sleep(2000);
    final LoadSnapshotClosure done = loadSnapshotArg.getValue();
    final SnapshotReader reader = done.start();
    assertNotNull(reader);
    assertEquals(1, reader.listFiles().size());
    assertTrue(reader.listFiles().contains("testFile"));
    done.run(Status.OK());
    this.executor.join();
    assertEquals(2, this.executor.getLastSnapshotTerm());
    assertEquals(1, this.executor.getLastSnapshotIndex());
    assertNotNull(installContext.getResponseObject());
    assertNotNull(retryInstallContext.getResponseObject());
    assertEquals(installContext.as(RpcRequests.ErrorResponse.class).getErrorCode(), RaftError.EINTR.getNumber());
    assertTrue(retryInstallContext.as(RpcRequests.InstallSnapshotResponse.class).hasSuccess());
}
Also used : Message(com.google.protobuf.Message) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Endpoint(com.alipay.sofa.jraft.util.Endpoint) MockAsyncContext(com.alipay.sofa.jraft.test.MockAsyncContext) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Future(java.util.concurrent.Future) LocalSnapshotReader(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test)

Example 2 with MockAsyncContext

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

the class PingRequestProcessorTest method testHandlePing.

@Test
public void testHandlePing() throws Exception {
    PingRequestProcessor processor = new PingRequestProcessor();
    MockAsyncContext ctx = new MockAsyncContext();
    processor.handleRequest(ctx, TestUtils.createPingRequest());
    ErrorResponse response = (ErrorResponse) ctx.getResponseObject();
    assertEquals(0, response.getErrorCode());
}
Also used : MockAsyncContext(com.alipay.sofa.jraft.test.MockAsyncContext) ErrorResponse(com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse) Test(org.junit.Test)

Example 3 with MockAsyncContext

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

the class AppendEntriesRequestProcessorTest method setup.

@Override
public void setup() {
    super.setup();
    this.asyncContext = new MockAsyncContext() {

        @Override
        public Connection getConnection() {
            return AppendEntriesRequestProcessorTest.this.conn;
        }
    };
    Set<PeerPair> pairs = new ConcurrentHashSet<>();
    pairs.add(new PeerPair(this.peerIdStr, this.serverId));
    Mockito.when(this.conn.getAttribute(AppendEntriesRequestProcessor.PAIR_ATTR)).thenReturn(pairs);
}
Also used : PeerPair(com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair) ConcurrentHashSet(com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet) MockAsyncContext(com.alipay.sofa.jraft.test.MockAsyncContext) Connection(com.alipay.sofa.jraft.rpc.Connection)

Example 4 with MockAsyncContext

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

the class BaseCliRequestProcessorTest method setup.

@Before
public void setup() {
    this.asyncContext = new MockAsyncContext();
    this.peer = JRaftUtils.getPeerId("localhost:8081");
    this.processor = new MockCliRequestProcessor(this.peer.toString(), "test");
}
Also used : MockAsyncContext(com.alipay.sofa.jraft.test.MockAsyncContext) Before(org.junit.Before)

Example 5 with MockAsyncContext

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

the class NodeRequestProcessorTest method setup.

@Before
public void setup() {
    this.asyncContext = new MockAsyncContext();
    this.processor = new MockRequestProcessor("localhost:8081", "test");
}
Also used : MockAsyncContext(com.alipay.sofa.jraft.test.MockAsyncContext) Before(org.junit.Before)

Aggregations

MockAsyncContext (com.alipay.sofa.jraft.test.MockAsyncContext)5 Before (org.junit.Before)2 Test (org.junit.Test)2 LoadSnapshotClosure (com.alipay.sofa.jraft.closure.LoadSnapshotClosure)1 Connection (com.alipay.sofa.jraft.rpc.Connection)1 RpcRequestClosure (com.alipay.sofa.jraft.rpc.RpcRequestClosure)1 RpcRequests (com.alipay.sofa.jraft.rpc.RpcRequests)1 ErrorResponse (com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse)1 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)1 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)1 PeerPair (com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair)1 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)1 LocalSnapshotReader (com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 ConcurrentHashSet (com.alipay.sofa.jraft.util.concurrent.ConcurrentHashSet)1 Message (com.google.protobuf.Message)1 ByteBuffer (java.nio.ByteBuffer)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1