Search in sources :

Example 1 with ErrorResponse

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;
        }
    };
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) RequestFuture(com.alibaba.nacos.api.remote.RequestFuture) Payload(com.alibaba.nacos.api.grpc.auto.Payload) NacosException(com.alibaba.nacos.api.exception.NacosException) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse)

Example 2 with ErrorResponse

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);
}
Also used : Request(com.alibaba.nacos.api.remote.request.Request) NacosException(com.alibaba.nacos.api.exception.NacosException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Test(org.junit.Test)

Example 3 with ErrorResponse

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);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCheckRequest(com.alibaba.nacos.api.remote.request.ServerCheckRequest) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Test(org.junit.Test)

Example 4 with ErrorResponse

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);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Test(org.junit.Test)

Example 5 with ErrorResponse

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);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) Test(org.junit.Test)

Aggregations

ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)10 Payload (com.alibaba.nacos.api.grpc.auto.Payload)7 Test (org.junit.Test)7 StreamObserver (io.grpc.stub.StreamObserver)5 NacosException (com.alibaba.nacos.api.exception.NacosException)4 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)4 InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)3 Response (com.alibaba.nacos.api.remote.response.Response)3 RequestFuture (com.alibaba.nacos.api.remote.RequestFuture)1 Request (com.alibaba.nacos.api.remote.request.Request)1 ServerCheckRequest (com.alibaba.nacos.api.remote.request.ServerCheckRequest)1 ClientDetectionResponse (com.alibaba.nacos.api.remote.response.ClientDetectionResponse)1 ConnectResetResponse (com.alibaba.nacos.api.remote.response.ConnectResetResponse)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CancellationException (java.util.concurrent.CancellationException)1 TimeoutException (java.util.concurrent.TimeoutException)1