io.atomix.core.Atomix Java Examples

The following examples show how to use io.atomix.core.Atomix. 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 File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testAcquireRelease() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphore semaphore = atomix.atomicSemaphoreBuilder("test-semaphore-base")
      .withProtocol(protocol())
      .withInitialCapacity(10)
      .build();

  assertEquals(10, semaphore.availablePermits());
  semaphore.acquire();
  assertEquals(9, semaphore.availablePermits());
  semaphore.acquire(9);
  assertEquals(0, semaphore.availablePermits());

  semaphore.release();
  assertEquals(1, semaphore.availablePermits());
  semaphore.release(100);
  assertEquals(101, semaphore.availablePermits());
}
 
Example #2
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testExpire() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphore semaphore =
      atomix.atomicSemaphoreBuilder("test-semaphore-expire")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build();

  assertEquals(10, semaphore.availablePermits());

  CompletableFuture<Optional<Version>> future = semaphore.async().tryAcquire(11, Duration.ofMillis(500));
  Thread.sleep(500);
  assertFalse(future.get(10, TimeUnit.SECONDS).isPresent());
  assertEquals(10, semaphore.availablePermits());
}
 
Example #3
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10000)
public void testQueue() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphore semaphore =
      atomix.atomicSemaphoreBuilder("test-semaphore-queue")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build();

  CompletableFuture<Version> future20 = semaphore.async().acquire(20);
  CompletableFuture<Version> future11 = semaphore.async().acquire(11);

  semaphore.increasePermits(1);
  assertNotNull(future11);
  assertFalse(future20.isDone());
  assertEquals(0, semaphore.availablePermits());

  CompletableFuture<Version> future21 = semaphore.async().acquire(21);
  CompletableFuture<Version> future5 = semaphore.async().acquire(5);

  // wakeup 5 and 20
  semaphore.release(25);
  future5.get(10, TimeUnit.SECONDS);
  future20.get(10, TimeUnit.SECONDS);
  assertFalse(future21.isDone());
}
 
Example #4
Source File: AtomixAgent.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Runs a standalone Atomix agent from the given command line arguments.
 *
 * @param args the program arguments
 * @throws Exception if the supplied arguments are invalid
 */
public static void main(String[] args) throws Exception {
  // Parse the command line arguments.
  final List<String> unknown = new ArrayList<>();
  final Namespace namespace = parseArgs(args, unknown);
  final Namespace extraArgs = parseUnknown(unknown);
  extraArgs.getAttrs().forEach((key, value) -> System.setProperty(key, value.toString()));

  final Logger logger = createLogger(namespace);

  final Atomix atomix = buildAtomix(namespace);
  atomix.start().join();
  logger.info("Atomix listening at {}", atomix.getMembershipService().getLocalMember().address());

  final ManagedRestService rest = buildRestService(atomix, namespace);
  rest.start().join();
  logger.warn("The Atomix HTTP API is BETA and is intended for development and debugging purposes only!");
  logger.info("HTTP server listening at {}", rest.address());

  synchronized (Atomix.class) {
    while (atomix.isRunning()) {
      Atomix.class.wait();
    }
  }
}
 
Example #5
Source File: ClusterConfig.java    From yfs with Apache License 2.0 6 votes vote down vote up
public ClusterConfig() {
    this.clusterProperties = getClusterProperties();
    Member m = gatewayMember.apply(clusterProperties);
    List<Member> ms = gatewayMembers.apply(clusterProperties);
    Atomix atomix = Atomix.builder()
            .withMemberId(clusterProperties.getLocal())
            .withAddress(m.address())
            .withMembershipProvider(BootstrapDiscoveryProvider.builder()
                    .withNodes((Collection) ms)
                    .build())
            .withManagementGroup(gatewayManagementGroup.apply(clusterProperties))
            .withPartitionGroups(gatewayDataGroup.apply(clusterProperties))
            .withZone(CommonConstant.gatewayZone)
            .build();
    atomix.start().join();
    this.atomicMap = atomix.<String, StoreInfo>atomicMapBuilder(CommonConstant.storeInfoMapName)
            .withProtocol(MultiRaftProtocol.builder()
                    .withReadConsistency(ReadConsistency.LINEARIZABLE)
                    .build())
            .withSerializer(CommonConstant.protocolSerializer)
            .build();
    ;
    this.atomix = atomix;
    LOGGER.info("Atomix[{},{}]启动成功", m.id(), m.address().toString());
}
 
Example #6
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testOverflow() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphore semaphore = atomix.atomicSemaphoreBuilder("test-semaphore-overflow")
      .withProtocol(protocol())
      .withInitialCapacity(Integer.MAX_VALUE)
      .build();

  assertEquals(Integer.MAX_VALUE, semaphore.increasePermits(10));
  semaphore.release(10);
  assertEquals(Integer.MAX_VALUE, semaphore.availablePermits());
  assertEquals(Integer.MAX_VALUE, semaphore.drainPermits());

  semaphore.reducePermits(Integer.MAX_VALUE);
  semaphore.reducePermits(Integer.MAX_VALUE);
  assertEquals(Integer.MIN_VALUE, semaphore.availablePermits());
}
 
Example #7
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testInit() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphore semaphore100 = atomix.atomicSemaphoreBuilder("test-semaphore-init-100")
      .withProtocol(protocol())
      .withInitialCapacity(100)
      .build();
  AtomicSemaphore semaphore0 = atomix.atomicSemaphoreBuilder("test-semaphore-init-0")
      .withProtocol(protocol())
      .build();
  assertEquals(100, semaphore100.availablePermits());
  assertEquals(0, semaphore0.availablePermits());

  AtomicSemaphore semaphoreNoInit = atomix.atomicSemaphoreBuilder("test-semaphore-init-100")
      .withProtocol(protocol())
      .build();
  assertEquals(100, semaphoreNoInit.availablePermits());
}
 
Example #8
Source File: AtomicMapPerformanceTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a set of Raft servers.
 */
private List<Atomix> createServers(int nodes) throws Exception {
  List<Atomix> servers = new ArrayList<>();

  for (int i = 0; i < nodes; i++) {
    members.add(nextNode());
  }

  CountDownLatch latch = new CountDownLatch(nodes);
  for (int i = 0; i < nodes; i++) {
    Atomix server = createServer(members.get(i), Lists.newArrayList(members));
    server.start().thenRun(() -> latch.countDown());
    servers.add(server);
  }

  latch.await(1, TimeUnit.MINUTES);

  return servers;
}
 
Example #9
Source File: DistributedSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testExpire() throws Exception {
  Atomix atomix = atomix();
  DistributedSemaphore semaphore =
      atomix.semaphoreBuilder("test-semaphore-expire")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build();

  assertEquals(10, semaphore.availablePermits());

  CompletableFuture<Boolean> future = semaphore.async().tryAcquire(11, Duration.ofMillis(500));
  Thread.sleep(500);
  assertFalse(future.get(10, TimeUnit.SECONDS));
  assertEquals(10, semaphore.availablePermits());
}
 
Example #10
Source File: AtomicMapPerformanceTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an Atomix client.
 */
private Atomix createClient() {
  Member member = nextNode();

  Atomix atomix = Atomix.builder()
      .withMemberId(member.id())
      .withAddress(member.address())
      .withMembershipProvider(BootstrapDiscoveryProvider.builder()
          .withNodes((Collection) members)
          .build())
      .withProfiles(Profile.client())
      .build();

  atomix.start().join();
  clients.add(atomix);
  return atomix;
}
 
Example #11
Source File: DistributedSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 10000)
public void testQueue() throws Exception {
  Atomix atomix = atomix();
  DistributedSemaphore semaphore =
      atomix.semaphoreBuilder("test-semaphore-queue")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build();

  CompletableFuture<Void> future20 = semaphore.async().acquire(20);
  CompletableFuture<Void> future11 = semaphore.async().acquire(11);

  semaphore.increasePermits(1);
  assertNotNull(future11);
  assertFalse(future20.isDone());
  assertEquals(0, semaphore.availablePermits());

  CompletableFuture<Void> future21 = semaphore.async().acquire(21);
  CompletableFuture<Void> future5 = semaphore.async().acquire(5);

  // wakeup 5 and 20
  semaphore.release(25);
  future5.get(10, TimeUnit.SECONDS);
  future20.get(10, TimeUnit.SECONDS);
  assertFalse(future21.isDone());
}
 
Example #12
Source File: DistributedSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testOverflow() throws Exception {
  Atomix atomix = atomix();
  DistributedSemaphore semaphore = atomix.semaphoreBuilder("test-semaphore-overflow")
      .withProtocol(protocol())
      .withInitialCapacity(Integer.MAX_VALUE)
      .build();

  semaphore.increasePermits(10);
  semaphore.release(10);
  assertEquals(Integer.MAX_VALUE, semaphore.availablePermits());
  assertEquals(Integer.MAX_VALUE, semaphore.drainPermits());

  semaphore.reducePermits(Integer.MAX_VALUE);
  semaphore.reducePermits(Integer.MAX_VALUE);
  assertEquals(Integer.MIN_VALUE, semaphore.availablePermits());
}
 
Example #13
Source File: DistributedSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testIncreaseReduceDrain() throws Exception {
  Atomix atomix = atomix();
  DistributedSemaphore semaphore = atomix.semaphoreBuilder("test-semaphore-ird")
      .withProtocol(protocol())
      .withInitialCapacity(-10)
      .build();

  assertEquals(-10, semaphore.availablePermits());
  semaphore.increasePermits(20);
  assertEquals(10, semaphore.availablePermits());
  semaphore.reducePermits(20);
  assertEquals(-10, semaphore.availablePermits());
  assertEquals(-10, semaphore.drainPermits());
  assertEquals(0, semaphore.availablePermits());
}
 
Example #14
Source File: DistributedSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testAcquireRelease() throws Exception {
  Atomix atomix = atomix();
  DistributedSemaphore semaphore = atomix.semaphoreBuilder("test-semaphore-base")
      .withProtocol(protocol())
      .withInitialCapacity(10)
      .build();

  assertEquals(10, semaphore.availablePermits());
  semaphore.acquire();
  assertEquals(9, semaphore.availablePermits());
  semaphore.acquire(9);
  assertEquals(0, semaphore.availablePermits());

  semaphore.release();
  assertEquals(1, semaphore.availablePermits());
  semaphore.release(100);
  assertEquals(101, semaphore.availablePermits());
}
 
Example #15
Source File: DistributedSemaphoreTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 30000)
public void testInit() throws Exception {
  Atomix atomix = atomix();
  DistributedSemaphore semaphore100 = atomix.semaphoreBuilder("test-semaphore-init-100")
      .withProtocol(protocol())
      .withInitialCapacity(100)
      .build();
  DistributedSemaphore semaphore0 = atomix.semaphoreBuilder("test-semaphore-init-0")
      .withProtocol(protocol())
      .build();
  assertEquals(100, semaphore100.availablePermits());
  assertEquals(0, semaphore0.availablePermits());

  DistributedSemaphore semaphoreNoInit = atomix.semaphoreBuilder("test-semaphore-init-100")
      .withProtocol(protocol())
      .build();
  assertEquals(100, semaphoreNoInit.availablePermits());
}
 
Example #16
Source File: YfsConfig.java    From yfs with Apache License 2.0 6 votes vote down vote up
@Bean(name = "gatewayAtomix")
public Atomix getGatewayAtomix() {
    List<Member> ms = gatewayMembers.apply(clusterProperties);
    Atomix atomix = Atomix.builder()
            .withMemberId(clusterProperties.getLocal())
            .withAddress(clusterProperties.getGateway().getIp(), clusterProperties.getGateway().getPort())
            .withMembershipProvider(BootstrapDiscoveryProvider.builder()
                    .withNodes((Collection) ms)
                    .build())
            .withProfiles(Profile.client())
            .withZone(CommonConstant.storeZone)
            .withRack(clusterProperties.getGroup())
            .build();

    atomix.start().join();
    storeInfoMap = atomix.<String, StoreInfo>atomicMapBuilder(CommonConstant.storeInfoMapName)
            .withProtocol(MultiRaftProtocol.builder()
                    .withReadConsistency(ReadConsistency.LINEARIZABLE)
                    .build())
            .withSerializer(CommonConstant.protocolSerializer)
            .build();
    return atomix;
}
 
Example #17
Source File: DistributedSemaphoreTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testExpireRace() throws Exception {
  int testCount = 10000;
  int threads = Runtime.getRuntime().availableProcessors();
  ExecutorService executorService = Executors.newFixedThreadPool(threads);

  Atomix atomix = atomix();

  List<Future<?>> taskFuture = new ArrayList<>(threads);
  AtomicInteger acquired = new AtomicInteger();

  for (int i = 0; i < threads; i++) {
    taskFuture.add(executorService.submit(() -> {
      DistributedSemaphore semaphore =
          atomix.semaphoreBuilder("test-semaphore-race")
              .withProtocol(protocol())
              .withInitialCapacity(testCount)
              .build();
      while (acquired.get() < testCount) {
        try {
          if (semaphore.tryAcquire(Duration.ofMillis(1))) {
            acquired.incrementAndGet();
          }
        } catch (InterruptedException e) {
          fail();
        }
      }
    }));
  }

  for (Future<?> future : taskFuture) {
    future.get();
  }

  executorService.shutdown();
}
 
Example #18
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testIncreaseReduceDrain() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphore semaphore = atomix.atomicSemaphoreBuilder("test-semaphore-ird")
      .withProtocol(protocol())
      .withInitialCapacity(-10)
      .build();

  assertEquals(-10, semaphore.availablePermits());
  assertEquals(10, semaphore.increasePermits(20));
  assertEquals(-10, semaphore.reducePermits(20));
  assertEquals(-10, semaphore.drainPermits());
  assertEquals(0, semaphore.availablePermits());
}
 
Example #19
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testBlocking() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphore semaphore =
      atomix.atomicSemaphoreBuilder("test-semaphore-blocking")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build();

  semaphore.acquire();
  assertEquals(9, semaphore.availablePermits());

  semaphore.release();
  assertEquals(10, semaphore.availablePermits());

  semaphore.increasePermits(10);
  assertEquals(20, semaphore.availablePermits());

  semaphore.reducePermits(10);
  assertEquals(10, semaphore.availablePermits());

  assertFalse(semaphore.tryAcquire(11).isPresent());
  assertFalse(semaphore.tryAcquire(11, Duration.ofMillis(1)).isPresent());
  assertTrue(semaphore.tryAcquire(5).isPresent());

  assertEquals(5, semaphore.drainPermits());

  AtomicSemaphore semaphore2 =
      atomix.atomicSemaphoreBuilder("test-semaphore-blocking")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build();

  assertEquals(0, semaphore2.availablePermits());
  semaphore.close();
  assertEquals(10, semaphore2.availablePermits());
}
 
Example #20
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testHolderStatus() throws Exception {
  Atomix atomix = atomix();
  AtomicSemaphoreProxy semaphore =
      (AtomicSemaphoreProxy) (atomix.atomicSemaphoreBuilder("test-semaphore-holders")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build()
          .async());

  AtomicSemaphoreProxy semaphore2 =
      (AtomicSemaphoreProxy) (atomix.atomicSemaphoreBuilder("test-semaphore-holders")
          .withProtocol(protocol())
          .withInitialCapacity(10)
          .build()
          .async());

  assertEquals(0, semaphore.holderStatus().get().size());

  semaphore.acquire().get(10, TimeUnit.SECONDS);
  semaphore2.acquire().get(10, TimeUnit.SECONDS);

  Map<Long, Integer> status = semaphore.holderStatus().get();
  assertEquals(2, status.size());
  status.values().forEach(permits -> assertEquals(1, permits.intValue()));

  semaphore.acquire().get(10, TimeUnit.SECONDS);
  semaphore2.acquire().get(10, TimeUnit.SECONDS);

  Map<Long, Integer> status2 = semaphore.holderStatus().get();
  assertEquals(2, status2.size());
  status2.values().forEach(permits -> assertEquals(2, permits.intValue()));

  semaphore.release(2).get(10, TimeUnit.SECONDS);
  semaphore2.release(2).get(10, TimeUnit.SECONDS);
  assertEquals(0, semaphore.holderStatus().get().size());
}
 
Example #21
Source File: DistributedMultisetConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  DistributedMultisetConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("multiset");
  assertEquals("multiset", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
  assertSame(Type1.class, config.getElementType());
  assertSame(Type3.class, config.getExtraTypes().get(0));
  assertTrue(config.getNamespaceConfig().isRegistrationRequired());
  assertSame(Type1.class, config.getNamespaceConfig().getTypes().get(0).getType());
  assertSame(Type2.class, config.getNamespaceConfig().getTypes().get(1).getType());
}
 
Example #22
Source File: DistributedMultimapConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  DistributedMultimapConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("multimap");
  assertEquals("multimap", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
  assertTrue(config.getNamespaceConfig().isRegistrationRequired());
  assertSame(Type1.class, config.getKeyType());
  assertSame(Type2.class, config.getValueType());
  assertSame(Type3.class, config.getExtraTypes().get(0));
  assertSame(Type1.class, config.getNamespaceConfig().getTypes().get(0).getType());
  assertSame(Type2.class, config.getNamespaceConfig().getTypes().get(1).getType());
}
 
Example #23
Source File: AtomicMultimapConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  AtomicMultimapConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("atomic-multimap");
  assertEquals("atomic-multimap", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
  assertTrue(config.getNamespaceConfig().isRegistrationRequired());
  assertSame(Type1.class, config.getKeyType());
  assertSame(Type2.class, config.getValueType());
  assertSame(Type3.class, config.getExtraTypes().get(0));
  assertSame(Type1.class, config.getNamespaceConfig().getTypes().get(0).getType());
  assertSame(Type2.class, config.getNamespaceConfig().getTypes().get(1).getType());
}
 
Example #24
Source File: AtomicCounterConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  AtomicCounterConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("atomic-counter");
  assertEquals("atomic-counter", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
}
 
Example #25
Source File: LeaderElectionConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  LeaderElectionConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("leader-election");
  assertEquals("leader-election", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
}
 
Example #26
Source File: LeaderElectorConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  LeaderElectorConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("leader-elector");
  assertEquals("leader-elector", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
}
 
Example #27
Source File: DistributedCyclicBarrierConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  DistributedCyclicBarrierConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("cyclic-barrier");
  assertEquals("cyclic-barrier", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
}
 
Example #28
Source File: AtomicValueConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  AtomicValueConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("atomic-value");
  assertEquals("atomic-value", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
  assertTrue(config.getNamespaceConfig().isRegistrationRequired());
  assertSame(Type1.class, config.getValueType());
  assertSame(Type3.class, config.getExtraTypes().get(0));
  assertSame(Type1.class, config.getNamespaceConfig().getTypes().get(0).getType());
  assertSame(Type2.class, config.getNamespaceConfig().getTypes().get(1).getType());
}
 
Example #29
Source File: DistributedListConfigTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadConfig() throws Exception {
  DistributedListConfig config = Atomix.config(getClass().getClassLoader().getResource("primitives.conf").getPath())
      .getPrimitive("list");
  assertEquals("list", config.getName());
  assertEquals(MultiPrimaryProtocol.TYPE, config.getProtocolConfig().getType());
  assertFalse(config.isReadOnly());
  assertSame(Type1.class, config.getElementType());
  assertSame(Type3.class, config.getExtraTypes().get(0));
  assertTrue(config.getNamespaceConfig().isRegistrationRequired());
  assertSame(Type1.class, config.getNamespaceConfig().getTypes().get(0).getType());
  assertSame(Type2.class, config.getNamespaceConfig().getTypes().get(1).getType());
}
 
Example #30
Source File: AtomicSemaphoreTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testExpireRace() throws Exception {
  int testCount = 10000;
  int threads = Runtime.getRuntime().availableProcessors();
  ExecutorService executorService = Executors.newFixedThreadPool(threads);

  Atomix atomix = atomix();

  List<Future<?>> taskFuture = new ArrayList<>(threads);
  AtomicInteger acquired = new AtomicInteger();

  for (int i = 0; i < threads; i++) {
    taskFuture.add(executorService.submit(() -> {
      AtomicSemaphore semaphore =
          atomix.atomicSemaphoreBuilder("test-semaphore-race")
              .withProtocol(protocol())
              .withInitialCapacity(testCount)
              .build();
      while (acquired.get() < testCount) {
        semaphore.tryAcquire(Duration.ofMillis(1)).ifPresent(v -> acquired.incrementAndGet());
      }
    }));
  }

  for (Future<?> future : taskFuture) {
    future.get();
  }

  executorService.shutdown();
}