Java Code Examples for io.grpc.reflection.v1alpha.ServiceResponse

The following examples show how to use io.grpc.reflection.v1alpha.ServiceResponse. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: grpc-nebula-java   Source File: ProtoReflectionService.java    License: Apache License 2.0 5 votes vote down vote up
private void listServices(ServerReflectionRequest request) {
  ListServiceResponse.Builder builder = ListServiceResponse.newBuilder();
  for (String serviceName : serverReflectionIndex.getServiceNames()) {
    builder.addService(ServiceResponse.newBuilder().setName(serviceName));
  }
  serverCallStreamObserver.onNext(
      ServerReflectionResponse.newBuilder()
          .setValidHost(request.getHost())
          .setOriginalRequest(request)
          .setListServicesResponse(builder)
          .build());
}
 
Example 2
private void assertServiceResponseEquals(Set<ServiceResponse> goldenResponse) throws Exception {
  ServerReflectionRequest request =
      ServerReflectionRequest.newBuilder().setHost(TEST_HOST).setListServices("services").build();
  StreamRecorder<ServerReflectionResponse> responseObserver = StreamRecorder.create();
  StreamObserver<ServerReflectionRequest> requestObserver =
      stub.serverReflectionInfo(responseObserver);
  requestObserver.onNext(request);
  requestObserver.onCompleted();
  List<ServiceResponse> response =
      responseObserver.firstValue().get().getListServicesResponse().getServiceList();
  assertEquals(goldenResponse.size(), response.size());
  assertEquals(goldenResponse, new HashSet<ServiceResponse>(response));
}
 
Example 3
Source Project: quarkus   Source File: GrpcReflectionTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRetrievingListOfServices() {
    ServerReflectionRequest request = ServerReflectionRequest.newBuilder().setHost("localhost")
            .setListServices("").build();

    ServerReflectionResponse response = invoke(request);
    List<ServiceResponse> list = response.getListServicesResponse().getServiceList();
    assertThat(list).hasSize(2)
            .anySatisfy(r -> assertThat(r.getName()).isEqualTo("grpc.reflection.testing.ReflectableService"))
            .anySatisfy(r -> assertThat(r.getName()).isEqualTo("grpc.health.v1.Health"));
}
 
Example 4
Source Project: quarkus   Source File: ReflectionService.java    License: Apache License 2.0 5 votes vote down vote up
private ServerReflectionResponse getServiceList(ServerReflectionRequest request) {
    ListServiceResponse response = index.getServiceNames().stream()
            .map(new Function<String, ServiceResponse>() { // NOSONAR
                @Override
                public ServiceResponse apply(String s) {
                    return ServiceResponse.newBuilder().setName(s).build();
                }
            })
            .collect(new Supplier<ListServiceResponse.Builder>() {
                @Override
                public ListServiceResponse.Builder get() {
                    return ListServiceResponse.newBuilder();
                }
            },
                    new BiConsumer<ListServiceResponse.Builder, ServiceResponse>() {
                        @Override
                        public void accept(ListServiceResponse.Builder builder, ServiceResponse value) {
                            builder.addService(value);
                        }
                    },
                    new BiConsumer<ListServiceResponse.Builder, ListServiceResponse.Builder>() { // NOSONAR
                        @Override
                        public void accept(ListServiceResponse.Builder b1,
                                ListServiceResponse.Builder b2) {
                            b1.addAllService(b2.getServiceList());
                        }
                    })
            .build();

    return ServerReflectionResponse.newBuilder()
            .setValidHost(request.getHost())
            .setOriginalRequest(request)
            .setListServicesResponse(response)
            .build();
}
 
Example 5
Source Project: grpc-spring-boot-starter   Source File: DemoAppTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testReflection() throws InterruptedException {
    List<String> discoveredServiceNames = new ArrayList<>();
    ServerReflectionRequest request = ServerReflectionRequest.newBuilder().setListServices("services").setHost("localhost").build();
    CountDownLatch latch = new CountDownLatch(1);
    ServerReflectionGrpc.newStub(channel).serverReflectionInfo(new StreamObserver<ServerReflectionResponse>() {
        @Override
        public void onNext(ServerReflectionResponse value) {
            List<ServiceResponse> serviceList = value.getListServicesResponse().getServiceList();
            for (ServiceResponse serviceResponse : serviceList) {
                discoveredServiceNames.add(serviceResponse.getName());
            }
        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    }).onNext(request);

    latch.await(3, TimeUnit.SECONDS);
    assertFalse(discoveredServiceNames.isEmpty());
}
 
Example 6
Source Project: grpc-java   Source File: ProtoReflectionService.java    License: Apache License 2.0 5 votes vote down vote up
private void listServices(ServerReflectionRequest request) {
  ListServiceResponse.Builder builder = ListServiceResponse.newBuilder();
  for (String serviceName : serverReflectionIndex.getServiceNames()) {
    builder.addService(ServiceResponse.newBuilder().setName(serviceName));
  }
  serverCallStreamObserver.onNext(
      ServerReflectionResponse.newBuilder()
          .setValidHost(request.getHost())
          .setOriginalRequest(request)
          .setListServicesResponse(builder)
          .build());
}
 
Example 7
Source Project: grpc-java   Source File: ProtoReflectionServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void sharedServiceBetweenServers()
    throws IOException, ExecutionException, InterruptedException {
  Server anotherServer = InProcessServerBuilder.forName("proto-reflection-test-2")
      .directExecutor()
      .addService(reflectionService)
      .addService(new AnotherReflectableServiceGrpc.AnotherReflectableServiceImplBase() {})
      .build()
      .start();
  grpcCleanupRule.register(anotherServer);
  ManagedChannel anotherChannel = grpcCleanupRule.register(
      InProcessChannelBuilder.forName("proto-reflection-test-2").directExecutor().build());
  ServerReflectionGrpc.ServerReflectionStub stub2 = ServerReflectionGrpc.newStub(anotherChannel);

  ServerReflectionRequest request =
      ServerReflectionRequest.newBuilder().setHost(TEST_HOST).setListServices("services").build();
  StreamRecorder<ServerReflectionResponse> responseObserver = StreamRecorder.create();
  StreamObserver<ServerReflectionRequest> requestObserver =
      stub2.serverReflectionInfo(responseObserver);
  requestObserver.onNext(request);
  requestObserver.onCompleted();
  List<ServiceResponse> response =
      responseObserver.firstValue().get().getListServicesResponse().getServiceList();
  assertEquals(new HashSet<>(
      Arrays.asList(
          ServiceResponse.newBuilder()
              .setName("grpc.reflection.v1alpha.ServerReflection")
              .build(),
          ServiceResponse.newBuilder()
              .setName("grpc.reflection.testing.AnotherReflectableService")
              .build())),
      new HashSet<>(response));
}
 
Example 8
Source Project: grpc-java   Source File: ProtoReflectionServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
private void assertServiceResponseEquals(Set<ServiceResponse> goldenResponse) throws Exception {
  ServerReflectionRequest request =
      ServerReflectionRequest.newBuilder().setHost(TEST_HOST).setListServices("services").build();
  StreamRecorder<ServerReflectionResponse> responseObserver = StreamRecorder.create();
  StreamObserver<ServerReflectionRequest> requestObserver =
      stub.serverReflectionInfo(responseObserver);
  requestObserver.onNext(request);
  requestObserver.onCompleted();
  List<ServiceResponse> response =
      responseObserver.firstValue().get().getListServicesResponse().getServiceList();
  assertEquals(goldenResponse.size(), response.size());
  assertEquals(goldenResponse, new HashSet<>(response));
}
 
Example 9
@Test
public void listServices() throws Exception {
  Set<ServiceResponse> originalServices =
      new HashSet<ServiceResponse>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build()));
  assertServiceResponseEquals(originalServices);

  handlerRegistry.addService(dynamicService);
  assertServiceResponseEquals(
      new HashSet<ServiceResponse>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.DynamicService")
                  .build())));

  handlerRegistry.addService(anotherDynamicService);
  assertServiceResponseEquals(
      new HashSet<ServiceResponse>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.DynamicService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.AnotherDynamicService")
                  .build())));

  handlerRegistry.removeService(dynamicService);
  assertServiceResponseEquals(
      new HashSet<ServiceResponse>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.AnotherDynamicService")
                  .build())));

  handlerRegistry.removeService(anotherDynamicService);
  assertServiceResponseEquals(originalServices);
}
 
Example 10
Source Project: grpc-java   Source File: ProtoReflectionServiceTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void listServices() throws Exception {
  Set<ServiceResponse> originalServices =
      new HashSet<>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build()));
  assertServiceResponseEquals(originalServices);

  handlerRegistry.addService(dynamicService);
  assertServiceResponseEquals(
      new HashSet<>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.DynamicService")
                  .build())));

  handlerRegistry.addService(anotherDynamicService);
  assertServiceResponseEquals(
      new HashSet<>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.DynamicService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.AnotherDynamicService")
                  .build())));

  handlerRegistry.removeService(dynamicService);
  assertServiceResponseEquals(
      new HashSet<>(
          Arrays.asList(
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.v1alpha.ServerReflection")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.ReflectableService")
                  .build(),
              ServiceResponse.newBuilder()
                  .setName("grpc.reflection.testing.AnotherDynamicService")
                  .build())));

  handlerRegistry.removeService(anotherDynamicService);
  assertServiceResponseEquals(originalServices);
}