Search in sources :

Example 6 with RpcRequestClosure

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

the class FileServiceTest method testGetLargeFileData.

@Test
public void testGetLargeFileData() throws IOException {
    final String data = writeLargeData();
    final long readerId = FileService.getInstance().addReader(this.fileReader);
    int fileOffset = 0;
    while (true) {
        final RpcRequests.GetFileRequest request = // 
        RpcRequests.GetFileRequest.newBuilder().setCount(4096).setFilename(// 
        "data").setOffset(// 
        fileOffset).setReaderId(// 
        readerId).build();
        final RpcContext asyncContext = Mockito.mock(RpcContext.class);
        final Message msg = // 
        FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext));
        assertTrue(msg instanceof RpcRequests.GetFileResponse);
        final RpcRequests.GetFileResponse response = (RpcRequests.GetFileResponse) msg;
        final byte[] sourceArray = data.getBytes();
        final byte[] respData = response.getData().toByteArray();
        final int length = sourceArray.length;
        int offset = 0;
        while (offset + length <= respData.length) {
            final byte[] respArray = new byte[length];
            System.arraycopy(respData, offset, respArray, 0, length);
            assertArrayEquals(sourceArray, respArray);
            offset += length;
        }
        fileOffset += offset;
        if (response.getEof()) {
            break;
        }
    }
}
Also used : RpcContext(com.alipay.sofa.jraft.rpc.RpcContext) Message(com.google.protobuf.Message) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) Test(org.junit.Test)

Example 7 with RpcRequestClosure

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

the class FileServiceTest method testGetFileNotFoundReader.

@Test
public void testGetFileNotFoundReader() {
    RpcRequests.GetFileRequest request = RpcRequests.GetFileRequest.newBuilder().setCount(Integer.MAX_VALUE).setFilename("data").setOffset(0).setReaderId(1).build();
    RpcContext asyncContext = Mockito.mock(RpcContext.class);
    Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext));
    assertTrue(msg instanceof RpcRequests.ErrorResponse);
    RpcRequests.ErrorResponse response = (RpcRequests.ErrorResponse) msg;
    Assert.assertEquals(RaftError.ENOENT.getNumber(), response.getErrorCode());
    assertEquals("Fail to find reader=1", response.getErrorMsg());
}
Also used : RpcContext(com.alipay.sofa.jraft.rpc.RpcContext) Message(com.google.protobuf.Message) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) Test(org.junit.Test)

Aggregations

RpcRequestClosure (com.alipay.sofa.jraft.rpc.RpcRequestClosure)7 Message (com.google.protobuf.Message)7 Test (org.junit.Test)7 RpcRequests (com.alipay.sofa.jraft.rpc.RpcRequests)5 RpcContext (com.alipay.sofa.jraft.rpc.RpcContext)4 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)3 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)3 Endpoint (com.alipay.sofa.jraft.util.Endpoint)3 LoadSnapshotClosure (com.alipay.sofa.jraft.closure.LoadSnapshotClosure)2 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)2 LocalSnapshotReader (com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader)2 ByteBuffer (java.nio.ByteBuffer)2 MockAsyncContext (com.alipay.sofa.jraft.test.MockAsyncContext)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1