use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class BaseCliRequestProcessorTest method testOK.
@Test
public void testOK() {
Node node = mockNode(false);
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(this.processor.done);
assertSame(this.processor.ctx.node, node);
assertNotNull(resp);
assertEquals(0, resp.getErrorCode());
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class AbstractClientService method connect.
@Override
public boolean connect(final Endpoint endpoint) {
final RpcClient rc = this.rpcClient;
if (rc == null) {
throw new IllegalStateException("Client service is uninitialized.");
}
if (isConnected(rc, endpoint)) {
return true;
}
try {
final PingRequest req = //
PingRequest.newBuilder().setSendTimestamp(//
System.currentTimeMillis()).build();
final ErrorResponse resp = (ErrorResponse) rc.invokeSync(endpoint, req, this.rpcOptions.getRpcConnectTimeoutMs());
return resp.getErrorCode() == 0;
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
return false;
} catch (final RemotingException e) {
LOG.error("Fail to connect {}, remoting exception: {}.", endpoint, e.getMessage());
return false;
}
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class AbstractClientServiceTest method testInvokeWithDOneOnErrorResponse.
@Test
public void testInvokeWithDOneOnErrorResponse() throws Exception {
final InvokeContext invokeCtx = new InvokeContext();
invokeCtx.put(InvokeContext.CRC_SWITCH, false);
final ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
final CliRequests.GetPeersRequest request = //
CliRequests.GetPeersRequest.newBuilder().setGroupId(//
"id").setLeaderId(//
"127.0.0.1:8001").build();
MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, invokeCtx, done, -1);
Mockito.verify(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), eq(invokeCtx), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
InvokeCallback cb = callbackArg.getValue();
assertNotNull(cb);
assertNotNull(future);
assertNull(done.getResponse());
assertNull(done.status);
assertFalse(future.isDone());
final Message resp = this.rpcResponseFactory.newResponse(CliRequests.GetPeersResponse.getDefaultInstance(), new Status(-1, "failed"));
cb.complete(resp, null);
final Message msg = future.get();
assertTrue(msg instanceof ErrorResponse);
assertEquals(((ErrorResponse) msg).getErrorMsg(), "failed");
done.latch.await();
assertNotNull(done.status);
assertTrue(!done.status.isOk());
assertEquals(done.status.getErrorMsg(), "failed");
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class AbstractClientServiceTest method testInvokeWithDoneOnException.
@Test
public void testInvokeWithDoneOnException() throws Exception {
InvokeContext invokeCtx = new InvokeContext();
invokeCtx.put(InvokeContext.CRC_SWITCH, false);
ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
PingRequest request = TestUtils.createPingRequest();
MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, invokeCtx, done, -1);
Mockito.verify(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), eq(invokeCtx), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
InvokeCallback cb = callbackArg.getValue();
assertNotNull(cb);
assertNotNull(future);
assertNull(done.getResponse());
assertNull(done.status);
assertFalse(future.isDone());
cb.complete(null, new InvokeTimeoutException());
try {
future.get();
fail();
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof InvokeTimeoutException);
}
done.latch.await();
assertNotNull(done.status);
assertEquals(RaftError.ETIMEDOUT.getNumber(), done.status.getCode());
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class AbstractClientServiceTest method testInvokeWithDoneException.
@Test
public void testInvokeWithDoneException() throws Exception {
InvokeContext invokeCtx = new InvokeContext();
invokeCtx.put(InvokeContext.CRC_SWITCH, false);
ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
PingRequest request = TestUtils.createPingRequest();
Mockito.doThrow(new RemotingException()).when(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), eq(invokeCtx), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, invokeCtx, done, -1);
InvokeCallback cb = callbackArg.getValue();
assertNotNull(cb);
assertNotNull(future);
assertTrue(future.isDone());
try {
future.get();
fail();
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof RemotingException);
}
done.latch.await();
assertNotNull(done.status);
assertEquals(RaftError.EINTERNAL.getNumber(), done.status.getCode());
}
Aggregations