Search in sources :

Example 1 with InstanceRequest

use of com.alibaba.nacos.api.naming.remote.request.InstanceRequest in project nacos by alibaba.

the class InstanceRequestHandlerTest method testHandle.

@Test
public void testHandle() throws NacosException {
    InstanceRequest instanceRequest = new InstanceRequest();
    instanceRequest.setType(NamingRemoteConstants.REGISTER_INSTANCE);
    Instance instance = new Instance();
    instanceRequest.setInstance(instance);
    RequestMeta requestMeta = new RequestMeta();
    instanceRequestHandler.handle(instanceRequest, requestMeta);
    Mockito.verify(clientOperationService).registerInstance(Mockito.any(), Mockito.any(), Mockito.anyString());
    instanceRequest.setType(NamingRemoteConstants.DE_REGISTER_INSTANCE);
    instanceRequestHandler.handle(instanceRequest, requestMeta);
    Mockito.verify(clientOperationService).deregisterInstance(Mockito.any(), Mockito.any(), Mockito.anyString());
    instanceRequest.setType("xxx");
    try {
        instanceRequestHandler.handle(instanceRequest, requestMeta);
    } catch (Exception e) {
        Assert.assertEquals(((NacosException) e).getErrCode(), NacosException.INVALID_PARAM);
    }
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Example 2 with InstanceRequest

use of com.alibaba.nacos.api.naming.remote.request.InstanceRequest in project nacos by alibaba.

the class NamingGrpcClientProxy method doDeregisterService.

/**
 * Execute deregister operation.
 *
 * @param serviceName service name
 * @param groupName   group name
 * @param instance    instance
 * @throws NacosException nacos exception
 */
public void doDeregisterService(String serviceName, String groupName, Instance instance) throws NacosException {
    InstanceRequest request = new InstanceRequest(namespaceId, serviceName, groupName, NamingRemoteConstants.DE_REGISTER_INSTANCE, instance);
    requestToServer(request, Response.class);
    redoService.removeInstanceForRedo(serviceName, groupName);
}
Also used : InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest)

Example 3 with InstanceRequest

use of com.alibaba.nacos.api.naming.remote.request.InstanceRequest 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 4 with InstanceRequest

use of com.alibaba.nacos.api.naming.remote.request.InstanceRequest in project nacos by alibaba.

the class NamingGrpcClientProxy method doRegisterService.

/**
 * Execute register operation.
 *
 * @param serviceName name of service
 * @param groupName   group of service
 * @param instance    instance to register
 * @throws NacosException nacos exception
 */
public void doRegisterService(String serviceName, String groupName, Instance instance) throws NacosException {
    InstanceRequest request = new InstanceRequest(namespaceId, serviceName, groupName, NamingRemoteConstants.REGISTER_INSTANCE, instance);
    requestToServer(request, Response.class);
    redoService.instanceRegistered(serviceName, groupName);
}
Also used : InstanceRequest(com.alibaba.nacos.api.naming.remote.request.InstanceRequest)

Example 5 with InstanceRequest

use of com.alibaba.nacos.api.naming.remote.request.InstanceRequest in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testHandleRequestError.

@Test
public void testHandleRequestError() {
    ApplicationUtils.setStarted(true);
    Mockito.when(requestHandlerRegistry.getByRequestType(Mockito.anyString())).thenReturn(mockHandler);
    Mockito.when(connectionManager.checkValid(Mockito.any())).thenReturn(true);
    RequestMeta metadata = new RequestMeta();
    metadata.setClientIp("127.0.0.1");
    metadata.setConnectionId(connectId);
    InstanceRequest instanceRequest = new InstanceRequest();
    Payload payload = 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.SERVER_ERROR);
        }

        @Override
        public void onError(Throwable throwable) {
            Assert.fail(throwable.getMessage());
        }

        @Override
        public void onCompleted() {
            System.out.println("complete");
        }
    };
    streamStub.request(payload, 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)

Aggregations

InstanceRequest (com.alibaba.nacos.api.naming.remote.request.InstanceRequest)7 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)5 Test (org.junit.Test)5 Payload (com.alibaba.nacos.api.grpc.auto.Payload)3 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)3 StreamObserver (io.grpc.stub.StreamObserver)3 NacosException (com.alibaba.nacos.api.exception.NacosException)2 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 Response (com.alibaba.nacos.api.remote.response.Response)1