Search in sources :

Example 1 with AtomicCounter

use of io.atomix.core.counter.AtomicCounter in project atomix by atomix.

the class AtomixTest method testLogBasedPrimitives.

@Test
public void testLogBasedPrimitives() throws Exception {
    CompletableFuture<Atomix> future1 = startAtomix(1, Arrays.asList(1, 2), builder -> builder.withManagementGroup(RaftPartitionGroup.builder("system").withNumPartitions(1).withMembers(String.valueOf(1), String.valueOf(2)).withDataDirectory(new File(new File(DATA_DIR, "log"), "1")).build()).withPartitionGroups(LogPartitionGroup.builder("log").withNumPartitions(3).build()).build());
    CompletableFuture<Atomix> future2 = startAtomix(2, Arrays.asList(1, 2), builder -> builder.withManagementGroup(RaftPartitionGroup.builder("system").withNumPartitions(1).withMembers(String.valueOf(1), String.valueOf(2)).withDataDirectory(new File(new File(DATA_DIR, "log"), "2")).build()).withPartitionGroups(LogPartitionGroup.builder("log").withNumPartitions(3).build()).build());
    Atomix atomix1 = future1.get();
    Atomix atomix2 = future2.get();
    DistributedMap<String, String> map1 = atomix1.<String, String>mapBuilder("test-map").withProtocol(DistributedLogProtocol.builder().build()).build();
    DistributedMap<String, String> map2 = atomix2.<String, String>mapBuilder("test-map").withProtocol(DistributedLogProtocol.builder().build()).build();
    CountDownLatch latch = new CountDownLatch(1);
    map2.addListener(event -> {
        map2.async().get("foo").thenAccept(value -> {
            assertEquals("bar", value);
            latch.countDown();
        });
    });
    map1.put("foo", "bar");
    latch.await(10, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
    AtomicCounter counter1 = atomix1.atomicCounterBuilder("test-counter").withProtocol(DistributedLogProtocol.builder().build()).build();
    AtomicCounter counter2 = atomix2.atomicCounterBuilder("test-counter").withProtocol(DistributedLogProtocol.builder().build()).build();
    assertEquals(1, counter1.incrementAndGet());
    assertEquals(1, counter1.get());
    Thread.sleep(1000);
    assertEquals(1, counter2.get());
    assertEquals(2, counter2.incrementAndGet());
}
Also used : AtomicCounter(io.atomix.core.counter.AtomicCounter) CountDownLatch(java.util.concurrent.CountDownLatch) File(java.io.File) Test(org.junit.Test)

Aggregations

AtomicCounter (io.atomix.core.counter.AtomicCounter)1 File (java.io.File)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1