HyperbahnClient { public TFuture<JsonResponse<AdvertiseResponse>> advertise() { final AdvertiseRequest advertiseRequest = new AdvertiseRequest(); advertiseRequest.addService(service, 0); final JsonRequest<AdvertiseRequest> request = new JsonRequest.Builder<AdvertiseRequest>( HYPERBAHN_SERVICE_NAME, HYPERBAHN_ADVERTISE_ENDPOINT ) .setBody(advertiseRequest) .setTimeout(REQUEST_TIMEOUT) .setRetryLimit(4) .build(); final TFuture<JsonResponse<AdvertiseResponse>> future = hyperbahnChannel.send(request); future.addCallback(new TFutureCallback<JsonResponse<AdvertiseResponse>>() { @Override public void onResponse(JsonResponse<AdvertiseResponse> response) { if (response.isError()) { try (ErrorResponse error = response.getError()) { logger.error( "Failed to advertise to Hyperbahn: {} - {}", error.getErrorType(), error.getMessage() ); } } if (destroyed.get()) { return; } scheduleAdvertise(); } }); return future; } private  HyperbahnClient(Builder builder); SubChannel makeClientChannel(String service); TFuture<JsonResponse<AdvertiseResponse>> advertise(); void stopAdvertising(); void shutdown(); public AtomicBoolean destroyed; }
@Test public void testAdvertise() throws Exception { AdvertiseResponseHandler responseHandler = new AdvertiseResponseHandler(); TChannel server = createMockHyperbahn(responseHandler); TChannel tchannel = new TChannel.Builder("hyperbahn-service").build(); List<InetSocketAddress> routers = new ArrayList<InetSocketAddress>() {{ add(new InetSocketAddress("127.0.0.1", 8888)); }}; HyperbahnClient hyperbahnClient = new HyperbahnClient.Builder("service", tchannel) .setRouters(routers) .build(); TFuture<JsonResponse<AdvertiseResponse>> responseFuture = hyperbahnClient.advertise(); try (JsonResponse<AdvertiseResponse> response = responseFuture.get()) { assertNotNull(response); assertEquals(1, responseHandler.requestsReceived); } tchannel.shutdown(); server.shutdown(); }