@Test public void testStrongCounterOps() { String name = "strong-test"; createCounter(name, CounterConfiguration.builder(CounterType.BOUNDED_STRONG).lowerBound(0).upperBound(100) .initialValue(0).build()); RestCounterClient counterClient = client.counter(name); CompletionStage<RestResponse> response = counterClient.increment(); assertThat(response).hasReturnedText("1"); response = counterClient.increment(); assertThat(response).hasReturnedText("2"); response = counterClient.decrement(); assertThat(response).hasReturnedText("1"); response = counterClient.decrement(); assertThat(response).hasReturnedText("0"); response = counterClient.add(35); assertThat(response).hasReturnedText("35"); waitForCounterToReach(name, 35); response = counterClient.compareAndSet(5, 32); assertThat(response).hasReturnedText("false"); response = counterClient.compareAndSet(35, 50); assertThat(response).hasReturnedText("true"); waitForCounterToReach(name, 50); response = counterClient.compareAndSwap(50, 90); assertThat(response).hasReturnedText("50"); response = counterClient.get(); assertThat(response).hasReturnedText("90"); }
private CompletionStage<RestResponse> createCounter(RestRequest request) throws RestResponseException { NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder(); String counterName = request.variables().get("counterName"); String contents = request.contents().asString(); if (contents == null || contents.length() == 0) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST); responseBuilder.entity("Configuration not provided"); return completedFuture(responseBuilder.build()); } CounterConfiguration configuration = createCounterConfiguration(contents); if (configuration == null) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST).entity("Invalid configuration"); return completedFuture(responseBuilder.build()); } return counterManager.defineCounterAsync(counterName, configuration).thenApply(r -> responseBuilder.build()); }
CounterResource implements ResourceHandler { private CompletionStage<RestResponse> createCounter(RestRequest request) throws RestResponseException { NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder(); String counterName = request.variables().get("counterName"); String contents = request.contents().asString(); if (contents == null || contents.length() == 0) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST); responseBuilder.entity("Configuration not provided"); return completedFuture(responseBuilder.build()); } CounterConfiguration configuration = createCounterConfiguration(contents); if (configuration == null) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST).entity("Invalid configuration"); return completedFuture(responseBuilder.build()); } return counterManager.defineCounterAsync(counterName, configuration).thenApply(r -> responseBuilder.build()); } }
CounterResource implements ResourceHandler { private CompletionStage<RestResponse> createCounter(RestRequest request) throws RestResponseException { NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder(); String counterName = request.variables().get("counterName"); String contents = request.contents().asString(); if (contents == null || contents.length() == 0) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST); responseBuilder.entity("Configuration not provided"); return completedFuture(responseBuilder.build()); } CounterConfiguration configuration = createCounterConfiguration(contents); if (configuration == null) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST).entity("Invalid configuration"); return completedFuture(responseBuilder.build()); } return counterManager.defineCounterAsync(counterName, configuration).thenApply(r -> responseBuilder.build()); } CounterResource(InvocationHelper invocationHelper); }
CounterResource implements ResourceHandler { private CompletionStage<RestResponse> createCounter(RestRequest request) throws RestResponseException { NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder(); String counterName = request.variables().get("counterName"); String contents = request.contents().asString(); if (contents == null || contents.length() == 0) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST); responseBuilder.entity("Configuration not provided"); return completedFuture(responseBuilder.build()); } CounterConfiguration configuration = createCounterConfiguration(contents); if (configuration == null) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST).entity("Invalid configuration"); return completedFuture(responseBuilder.build()); } return counterManager.defineCounterAsync(counterName, configuration).thenApply(r -> responseBuilder.build()); } CounterResource(InvocationHelper invocationHelper); @Override Invocations getInvocations(); }
CounterResource implements ResourceHandler { private CompletionStage<RestResponse> createCounter(RestRequest request) throws RestResponseException { NettyRestResponse.Builder responseBuilder = new NettyRestResponse.Builder(); String counterName = request.variables().get("counterName"); String contents = request.contents().asString(); if (contents == null || contents.length() == 0) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST); responseBuilder.entity("Configuration not provided"); return completedFuture(responseBuilder.build()); } CounterConfiguration configuration = createCounterConfiguration(contents); if (configuration == null) { responseBuilder.status(HttpResponseStatus.BAD_REQUEST).entity("Invalid configuration"); return completedFuture(responseBuilder.build()); } return counterManager.defineCounterAsync(counterName, configuration).thenApply(r -> responseBuilder.build()); } CounterResource(InvocationHelper invocationHelper); @Override Invocations getInvocations();  }