use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.
the class CopySessionTest method testOnRpcReturnedRetry.
@Test
public void testOnRpcReturnedRetry() throws Exception {
assertNull(this.session.getTimer());
assertNull(this.session.getRpcCall());
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
this.session.setDestBuf(bufRef);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
this.session.onRpcReturned(new Status(RaftError.EINTR, "test"), null);
assertNotNull(this.session.getTimer());
Thread.sleep(this.copyOpts.getRetryIntervalMs() + 100);
assertNotNull(this.session.getRpcCall());
assertSame(future, this.session.getRpcCall());
assertNull(this.session.getTimer());
}
use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.
the class CopySessionTest method testOnRpcReturnedOK.
@Test
public void testOnRpcReturnedOK() {
assertNull(this.session.getRpcCall());
final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
this.session.setDestBuf(bufRef);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(100).setReadPartly(true);
Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
this.session.onRpcReturned(Status.OK(), RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(false).setData(ByteString.copyFrom(new byte[100])).build());
assertEquals(100, bufRef.capacity());
assertEquals(100, bufRef.getBuffer().position());
assertNotNull(this.session.getRpcCall());
// send next request
assertSame(future, this.session.getRpcCall());
}
use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.
the class DefaultRaftClientService method onConnectionFail.
// fail-fast when no connection
private Future<Message> onConnectionFail(final Endpoint endpoint, final Message request, Closure done, final Executor executor) {
final FutureImpl<Message> future = new FutureImpl<>();
executor.execute(() -> {
final String fmt = "Check connection[%s] fail and try to create new one";
if (done != null) {
try {
done.run(new Status(RaftError.EINTERNAL, fmt, endpoint));
} catch (final Throwable t) {
LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
}
}
if (!future.isDone()) {
future.failure(new RemotingException(String.format(fmt, endpoint)));
}
});
return future;
}
use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.
the class LocalSnapshotCopierTest method testInterrupt.
@Test
public void testInterrupt() throws Exception {
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
final ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
this.copier.start();
Thread.sleep(10);
Utils.runInThread(new Runnable() {
@Override
public void run() {
LocalSnapshotCopierTest.this.copier.cancel();
}
});
this.copier.join();
// start timer
final SnapshotReader reader = this.copier.getReader();
assertNull(reader);
Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
Assert.assertEquals("Cancel the copier manually.", this.copier.getErrorMsg());
}
Aggregations