Java Code Examples for io.atomix.primitive.session.SessionClient#addEventListener()

The following examples show how to use io.atomix.primitive.session.SessionClient#addEventListener() . 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: PrimaryBackupTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests submitting a sequential event.
 */
private void testSequentialEvent(int nodes, int backups, Replication replication) throws Throwable {
  createServers(nodes);

  AtomicLong count = new AtomicLong();
  AtomicLong index = new AtomicLong();

  PrimaryBackupClient client = createClient();
  SessionClient session = createProxy(client, backups, replication);
  session.<Long>addEventListener(CHANGE_EVENT, event -> {
    threadAssertEquals(count.incrementAndGet(), 2L);
    threadAssertEquals(index.get(), SERIALIZER.decode(event.value()));
    resume();
  });

  session.execute(operation(EVENT, SERIALIZER.encode(true)))
      .<Long>thenApply(SERIALIZER::decode)
      .thenAccept(result -> {
        threadAssertNotNull(result);
        threadAssertEquals(count.incrementAndGet(), 1L);
        index.set(result);
        resume();
      });

  await(5000, 2);
}
 
Example 2
Source File: PrimaryBackupTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests submitting sequential events to all sessions.
 */
private void testEvents(int nodes, int backups, Replication replication) throws Throwable {
  createServers(nodes);

  PrimaryBackupClient client1 = createClient();
  SessionClient session1 = createProxy(client1, backups, replication);
  session1.addEventListener(CHANGE_EVENT, event -> {
    threadAssertNotNull(event);
    resume();
  });

  PrimaryBackupClient client2 = createClient();
  SessionClient session2 = createProxy(client2, backups, replication);
  session2.addEventListener(CHANGE_EVENT, event -> {
    threadAssertNotNull(event);
    resume();
  });

  session1.execute(operation(READ, null)).thenRun(this::resume);
  session2.execute(operation(READ, null)).thenRun(this::resume);
  await(5000, 2);

  session1.execute(operation(EVENT, SERIALIZER.encode(false))).thenRun(this::resume);
  await(5000, 3);
}
 
Example 3
Source File: PrimaryBackupTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests submitting a linearizable event that publishes to all sessions.
 */
private void testManyEvents(int nodes, int backups, Replication replication) throws Throwable {
  createServers(nodes);

  PrimaryBackupClient client = createClient();
  SessionClient session = createProxy(client, backups, replication);
  session.addEventListener(CHANGE_EVENT, message -> {
    threadAssertNotNull(message);
    resume();
  });

  for (int i = 0; i < 10; i++) {
    session.execute(operation(EVENT, SERIALIZER.encode(true))).thenRun(this::resume);

    await(5000, 2);
  }
}
 
Example 4
Source File: PrimaryBackupTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests events after a primary shutdown.
 */
private void testManyEventsAfterPrimaryShutdown(Replication replication) throws Throwable {
  List<PrimaryBackupServer> servers = createServers(5);

  PrimaryBackupClient client = createClient();
  SessionClient session = createProxy(client, 3, replication);
  session.addEventListener(CHANGE_EVENT, event -> {
    threadAssertNotNull(event);
    resume();
  });

  for (int i = 0; i < 10; i++) {
    session.execute(operation(EVENT, SERIALIZER.encode(true))).thenRun(this::resume);

    await(5000, 2);
  }

  PrimaryBackupServer leader = servers.stream().filter(s -> s.getRole() == Role.PRIMARY).findFirst().get();
  leader.stop().get(10, TimeUnit.SECONDS);

  for (int i = 0; i < 10; i++) {
    session.execute(operation(EVENT, SERIALIZER.encode(true))).thenRun(this::resume);

    await(5000, 2);
  }
}
 
Example 5
Source File: PrimaryBackupTest.java    From atomix with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a session closing.
 */
private void testSessionClose(Replication replication) throws Throwable {
  createServers(3);

  PrimaryBackupClient client1 = createClient();
  SessionClient session1 = createProxy(client1, 2, replication);
  PrimaryBackupClient client2 = createClient();
  session1.execute(operation(CLOSE)).thenRun(this::resume);
  await(Duration.ofSeconds(10).toMillis(), 1);
  session1.addEventListener(CLOSE_EVENT, e -> resume());
  SessionClient session2 = createProxy(client2, 2, replication);
  session2.execute(operation(READ)).thenRun(this::resume);
  await(5000);
  session2.close().thenRun(this::resume);
  await(Duration.ofSeconds(10).toMillis(), 2);
}
 
Example 6
Source File: RecoveringSessionClient.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void addEventListener(EventType eventType, Consumer<PrimitiveEvent> consumer) {
  eventListeners.put(eventType.canonicalize(), consumer);
  SessionClient proxy = this.session;
  if (proxy != null) {
    proxy.addEventListener(eventType, consumer);
  }
}
 
Example 7
Source File: PrimaryBackupTest.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * Tests submitting a linearizable event that publishes to all sessions.
 */
private void testManySessionsManyEvents(Replication replication) throws Throwable {
  createServers(3);

  PrimaryBackupClient client = createClient();
  SessionClient session = createProxy(client, 2, replication);
  session.addEventListener(CHANGE_EVENT, event -> {
    threadAssertNotNull(event);
    resume();
  });

  SessionClient session1 = createProxy(createClient(), 2, replication);
  session1.execute(operation(READ)).thenRun(this::resume);
  session1.addEventListener(CHANGE_EVENT, event -> {
    threadAssertNotNull(event);
    resume();
  });

  SessionClient session2 = createProxy(createClient(), 2, replication);
  session2.execute(operation(READ)).thenRun(this::resume);
  session2.addEventListener(CHANGE_EVENT, event -> {
    threadAssertNotNull(event);
    resume();
  });

  await(5000, 2);

  for (int i = 0; i < 10; i++) {
    session.execute(operation(EVENT, SERIALIZER.encode(false))).thenRun(this::resume);

    await(10000, 4);
  }
}