use of com.alibaba.nacos.api.remote.response.ErrorResponse in project nacos by alibaba.
the class GrpcConnection method requestFuture.
@Override
public RequestFuture requestFuture(Request request) throws NacosException {
Payload grpcRequest = GrpcUtils.convert(request);
final ListenableFuture<Payload> requestFuture = grpcFutureServiceStub.request(grpcRequest);
return new RequestFuture() {
@Override
public boolean isDone() {
return requestFuture.isDone();
}
@Override
public Response get() throws Exception {
Payload grpcResponse = requestFuture.get();
Response response = (Response) GrpcUtils.parse(grpcResponse);
if (response instanceof ErrorResponse) {
throw new NacosException(response.getErrorCode(), response.getMessage());
}
return response;
}
@Override
public Response get(long timeout) throws Exception {
Payload grpcResponse = requestFuture.get(timeout, TimeUnit.MILLISECONDS);
Response response = (Response) GrpcUtils.parse(grpcResponse);
if (response instanceof ErrorResponse) {
throw new NacosException(response.getErrorCode(), response.getMessage());
}
return response;
}
};
}
use of com.alibaba.nacos.api.remote.response.ErrorResponse in project nacos by alibaba.
the class RpcClientTest method testRequestWhenResponseUnregisterThenSwitchServer.
@Test
public void testRequestWhenResponseUnregisterThenSwitchServer() throws NacosException {
rpcClient.rpcClientStatus.set(RpcClientStatus.RUNNING);
rpcClient.currentConnection = connection;
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setErrorCode(NacosException.UN_REGISTER);
doReturn(errorResponse).when(connection).request(any(), anyLong());
Exception exception = null;
try {
rpcClient.request(mock(Request.class), 10000);
} catch (Exception e) {
exception = e;
}
Assert.assertEquals(RpcClientStatus.UNHEALTHY, rpcClient.rpcClientStatus.get());
verify(rpcClient).switchServerAsync();
Assert.assertNotNull(exception);
}
use of com.alibaba.nacos.api.remote.response.ErrorResponse in project nacos by alibaba.
the class GrpcRequestAcceptorTest method testApplicationUnStarted.
@Test
public void testApplicationUnStarted() {
RequestMeta metadata = new RequestMeta();
metadata.setClientIp("127.0.0.1");
metadata.setConnectionId(connectId);
ServerCheckRequest serverCheckRequest = new ServerCheckRequest();
serverCheckRequest.setRequestId(requestId);
Payload request = GrpcUtils.convert(serverCheckRequest, metadata);
StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {
@Override
public void onNext(Payload payload) {
System.out.println("Receive data from server: " + payload);
Object res = GrpcUtils.parse(payload);
Assert.assertTrue(res instanceof ErrorResponse);
ErrorResponse errorResponse = (ErrorResponse) res;
Assert.assertEquals(errorResponse.getErrorCode(), NacosException.INVALID_SERVER_STATUS);
}
@Override
public void onError(Throwable throwable) {
Assert.fail(throwable.getMessage());
}
@Override
public void onCompleted() {
System.out.println("complete");
}
};
streamStub.request(request, streamObserver);
}
use of com.alibaba.nacos.api.remote.response.ErrorResponse in project nacos by alibaba.
the class GrpcRequestAcceptorTest method testNoRequestHandler.
@Test
public void testNoRequestHandler() {
ApplicationUtils.setStarted(true);
RequestMeta metadata = new RequestMeta();
metadata.setClientIp("127.0.0.1");
metadata.setConnectionId(connectId);
InstanceRequest instanceRequest = new InstanceRequest();
instanceRequest.setRequestId(requestId);
Payload request = GrpcUtils.convert(instanceRequest, metadata);
StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {
@Override
public void onNext(Payload payload) {
System.out.println("Receive data from server: " + payload);
Object res = GrpcUtils.parse(payload);
Assert.assertTrue(res instanceof ErrorResponse);
ErrorResponse errorResponse = (ErrorResponse) res;
Assert.assertEquals(errorResponse.getErrorCode(), NacosException.NO_HANDLER);
}
@Override
public void onError(Throwable throwable) {
Assert.fail(throwable.getMessage());
}
@Override
public void onCompleted() {
System.out.println("complete");
}
};
streamStub.request(request, streamObserver);
ApplicationUtils.setStarted(false);
}
use of com.alibaba.nacos.api.remote.response.ErrorResponse in project nacos by alibaba.
the class GrpcRequestAcceptorTest method testRequestContentError.
@Test
public void testRequestContentError() {
ApplicationUtils.setStarted(true);
Mockito.when(requestHandlerRegistry.getByRequestType(Mockito.anyString())).thenReturn(mockHandler);
Mockito.when(connectionManager.checkValid(Mockito.any())).thenReturn(true);
StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {
@Override
public void onNext(Payload payload) {
System.out.println("Receive data from server: " + payload);
Object res = GrpcUtils.parse(payload);
Assert.assertTrue(res instanceof ErrorResponse);
ErrorResponse errorResponse = (ErrorResponse) res;
Assert.assertEquals(errorResponse.getErrorCode(), NacosException.BAD_GATEWAY);
}
@Override
public void onError(Throwable throwable) {
Assert.fail(throwable.getMessage());
}
@Override
public void onCompleted() {
System.out.println("complete");
}
};
streamStub.request(null, streamObserver);
ApplicationUtils.setStarted(false);
}
Aggregations