Search in sources :

Example 6 with RpcContext

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

use of com.alipay.sofa.jraft.rpc.RpcContext 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)

Example 8 with RpcContext

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

the class AppendEntriesRequestProcessorTest method testTooManyPendingResponses.

@Test
public void testTooManyPendingResponses() {
    final PeerId peer = mockNode();
    NodeManager.getInstance().get(this.groupId, peer).getRaftOptions().setMaxReplicatorInflightMsgs(2);
    final RpcContext asyncContext = Mockito.mock(RpcContext.class);
    final AppendEntriesRequestProcessor processor = (AppendEntriesRequestProcessor) newProcessor();
    final PeerPair pair = processor.pairOf(this.peerIdStr, this.serverId);
    final PingRequest msg = TestUtils.createPingRequest();
    final Connection conn = Mockito.mock(Connection.class);
    Mockito.when(asyncContext.getConnection()).thenReturn(conn);
    final PeerRequestContext ctx = processor.getOrCreatePeerRequestContext(this.groupId, pair, conn);
    assertNotNull(ctx);
    processor.sendSequenceResponse(this.groupId, pair, 1, asyncContext, msg);
    processor.sendSequenceResponse(this.groupId, pair, 2, asyncContext, msg);
    processor.sendSequenceResponse(this.groupId, pair, 3, asyncContext, msg);
    Mockito.verify(asyncContext, Mockito.never()).sendResponse(msg);
    Mockito.verify(conn).close();
    final PeerRequestContext newCtx = processor.getOrCreatePeerRequestContext(this.groupId, pair, conn);
    assertNotNull(newCtx);
    assertNotSame(ctx, newCtx);
}
Also used : RpcContext(com.alipay.sofa.jraft.rpc.RpcContext) PingRequest(com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest) PeerPair(com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair) Connection(com.alipay.sofa.jraft.rpc.Connection) PeerRequestContext(com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerRequestContext) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Aggregations

RpcContext (com.alipay.sofa.jraft.rpc.RpcContext)8 Test (org.junit.Test)7 RpcRequests (com.alipay.sofa.jraft.rpc.RpcRequests)5 Message (com.google.protobuf.Message)5 RpcRequestClosure (com.alipay.sofa.jraft.rpc.RpcRequestClosure)4 Connection (com.alipay.sofa.jraft.rpc.Connection)2 PingRequest (com.alipay.sofa.jraft.rpc.RpcRequests.PingRequest)2 PeerPair (com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerPair)2 Response (com.alibaba.nacos.consistency.entity.Response)1 JRaftServer (com.alibaba.nacos.core.distributed.raft.JRaftServer)1 PeerId (com.alipay.sofa.jraft.entity.PeerId)1 RpcProcessor (com.alipay.sofa.jraft.rpc.RpcProcessor)1 PeerRequestContext (com.alipay.sofa.jraft.rpc.impl.core.AppendEntriesRequestProcessor.PeerRequestContext)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1 SocketAddress (java.net.SocketAddress)1 Executor (java.util.concurrent.Executor)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1