Search in sources :

Example 1 with RestCounterClient

use of org.infinispan.client.rest.RestCounterClient in project infinispan by infinispan.

the class BackupManagerIT method createCounter.

private void createCounter(String name, Element type, Storage storage, RestClient client, long delta) {
    String config = String.format("{\n" + "    \"%s\":{\n" + "        \"initial-value\":0,\n" + "        \"storage\":\"%s\"\n" + "    }\n" + "}", type, storage.toString());
    RestCounterClient counterClient = client.counter(name);
    RestResponse rsp = await(counterClient.create(RestEntity.create(MediaType.APPLICATION_JSON, config)));
    assertEquals(200, rsp.getStatus());
    if (delta != 0) {
        rsp = await(counterClient.add(delta));
        assertEquals(name, name.contains("strong") ? 200 : 204, rsp.getStatus());
        assertNotNull(rsp.getBody());
    }
}
Also used : RestCounterClient(org.infinispan.client.rest.RestCounterClient) RestResponse(org.infinispan.client.rest.RestResponse)

Example 2 with RestCounterClient

use of org.infinispan.client.rest.RestCounterClient in project infinispan by infinispan.

the class RestOperations method testCounter.

@Test
public void testCounter() {
    RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder();
    builder.protocol(protocol);
    RestClient client = SERVER_TEST.rest().withClientConfiguration(builder).create();
    CounterConfiguration configuration = CounterConfiguration.builder(CounterType.WEAK).initialValue(5).concurrencyLevel(1).build();
    AbstractCounterConfiguration config = ConvertUtil.configToParsedConfig("test-counter", configuration);
    String configJson = AbstractRestResourceTest.counterConfigToJson(config);
    RestCounterClient counter = client.counter("test");
    RestResponse rsp = sync(counter.create(RestEntity.create(MediaType.APPLICATION_JSON, configJson)));
    assertEquals(HttpResponseStatus.OK.code(), rsp.getStatus());
    rsp = sync(counter.get());
    assertEquals("5", rsp.getBody());
}
Also used : RestCounterClient(org.infinispan.client.rest.RestCounterClient) RestClientConfigurationBuilder(org.infinispan.client.rest.configuration.RestClientConfigurationBuilder) RestResponse(org.infinispan.client.rest.RestResponse) RestClient(org.infinispan.client.rest.RestClient) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) AbstractRestResourceTest(org.infinispan.rest.resources.AbstractRestResourceTest) Test(org.junit.Test)

Example 3 with RestCounterClient

use of org.infinispan.client.rest.RestCounterClient in project infinispan by infinispan.

the class CounterResourceTest method testWeakCounterOps.

@Test
public void testWeakCounterOps() {
    String name = "weak-test";
    createCounter(name, CounterConfiguration.builder(CounterType.WEAK).initialValue(5).build());
    RestCounterClient counterClient = client.counter(name);
    CompletionStage<RestResponse> response = counterClient.increment();
    assertThat(response).hasNoContent();
    waitForCounterToReach(name, 6);
    response = counterClient.increment();
    assertThat(response).hasNoContent();
    waitForCounterToReach(name, 7);
    response = counterClient.decrement();
    assertThat(response).hasNoContent();
    waitForCounterToReach(name, 6);
    response = counterClient.decrement();
    assertThat(response).hasNoContent();
    waitForCounterToReach(name, 5);
    response = counterClient.add(10);
    assertThat(response).hasNoContent();
    waitForCounterToReach(name, 15);
    response = counterClient.reset();
    assertThat(response).hasNoContent();
    waitForCounterToReach(name, 5);
}
Also used : RestCounterClient(org.infinispan.client.rest.RestCounterClient) RestResponse(org.infinispan.client.rest.RestResponse) Test(org.testng.annotations.Test)

Example 4 with RestCounterClient

use of org.infinispan.client.rest.RestCounterClient in project infinispan by infinispan.

the class CounterResourceTest method waitForCounterToReach.

private void waitForCounterToReach(String name, int i) {
    RestCounterClient counterClient = client.counter(name);
    eventually(() -> {
        RestResponse r = join(counterClient.get());
        assertThat(r).isOk();
        long value = Long.parseLong(r.getBody());
        return value == i;
    });
}
Also used : RestCounterClient(org.infinispan.client.rest.RestCounterClient) RestResponse(org.infinispan.client.rest.RestResponse)

Example 5 with RestCounterClient

use of org.infinispan.client.rest.RestCounterClient in project infinispan by infinispan.

the class CounterResourceTest method testWeakCounterLifecycle.

@Test
public void testWeakCounterLifecycle() {
    CounterConfiguration counterConfig = CounterConfiguration.builder(CounterType.WEAK).initialValue(5).storage(Storage.VOLATILE).concurrencyLevel(6).build();
    createCounter("sample-counter", counterConfig);
    RestCounterClient counterClient = client.counter("sample-counter");
    RestResponse response = join(counterClient.configuration(APPLICATION_JSON_TYPE));
    Json jsonNode = Json.read(response.getBody());
    Json config = jsonNode.at("weak-counter");
    assertEquals(config.at("initial-value").asInteger(), 5);
    assertEquals(config.at("storage").asString(), "VOLATILE");
    assertEquals(config.at("concurrency-level").asInteger(), 6);
    response = join(counterClient.delete());
    assertThat(response).isOk();
    response = join(counterClient.configuration());
    assertThat(response).isNotFound();
}
Also used : RestCounterClient(org.infinispan.client.rest.RestCounterClient) RestResponse(org.infinispan.client.rest.RestResponse) Json(org.infinispan.commons.dataconversion.internal.Json) AbstractCounterConfiguration(org.infinispan.counter.configuration.AbstractCounterConfiguration) CounterConfiguration(org.infinispan.counter.api.CounterConfiguration) Test(org.testng.annotations.Test)

Aggregations

RestCounterClient (org.infinispan.client.rest.RestCounterClient)8 RestResponse (org.infinispan.client.rest.RestResponse)8 Test (org.testng.annotations.Test)3 CounterConfiguration (org.infinispan.counter.api.CounterConfiguration)2 AbstractCounterConfiguration (org.infinispan.counter.configuration.AbstractCounterConfiguration)2 RestClient (org.infinispan.client.rest.RestClient)1 RestClientConfigurationBuilder (org.infinispan.client.rest.configuration.RestClientConfigurationBuilder)1 Json (org.infinispan.commons.dataconversion.internal.Json)1 AbstractRestResourceTest (org.infinispan.rest.resources.AbstractRestResourceTest)1 Test (org.junit.Test)1