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;
}
}
}
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());
}
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);
}
Aggregations