Search in sources :

Example 11 with ClientMembershipListener

use of org.apache.geode.management.membership.ClientMembershipListener in project geode by apache.

the class ClientMembershipDUnitTest method testClientMembershipEventsInClient.

/**
   * Tests notification of events in client process. Bridge clients detect server joins when the
   * client connects to the server. If the server crashes or departs gracefully, the client will
   * detect this as a crash.
   */
@Test
public void testClientMembershipEventsInClient() throws Exception {
    properties = null;
    getSystem();
    IgnoredException.addIgnoredException("IOException");
    final boolean[] fired = new boolean[3];
    final DistributedMember[] member = new DistributedMember[3];
    final String[] memberId = new String[3];
    final boolean[] isClient = new boolean[3];
    // create and register ClientMembershipListener in controller vm...
    ClientMembershipListener listener = new ClientMembershipListener() {

        public void memberJoined(ClientMembershipEvent event) {
            System.out.println("[testClientMembershipEventsInClient] memberJoined: " + event);
            fired[JOINED] = true;
            member[JOINED] = event.getMember();
            memberId[JOINED] = event.getMemberId();
            isClient[JOINED] = event.isClient();
        }

        public void memberLeft(ClientMembershipEvent event) {
            System.out.println("[testClientMembershipEventsInClient] memberLeft: " + event);
        }

        public void memberCrashed(ClientMembershipEvent event) {
            System.out.println("[testClientMembershipEventsInClient] memberCrashed: " + event);
            fired[CRASHED] = true;
            member[CRASHED] = event.getMember();
            memberId[CRASHED] = event.getMemberId();
            isClient[CRASHED] = event.isClient();
        }
    };
    ClientMembership.registerClientMembershipListener(listener);
    final VM vm0 = Host.getHost(0).getVM(0);
    final String name = this.getUniqueName();
    final int[] ports = new int[1];
    // create BridgeServer in vm0...
    vm0.invoke("create Bridge Server", () -> {
        try {
            System.out.println("[testClientMembershipEventsInClient] Create BridgeServer");
            getSystem();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            Region region = createRegion(name, factory.create());
            assertNotNull(region);
            assertNotNull(getRootRegion().getSubregion(name));
            testClientMembershipEventsInClient_port = startBridgeServer(0);
        } catch (IOException e) {
            getSystem().getLogWriter().fine(new Exception(e));
            fail("Failed to start CacheServer: " + e.getMessage());
        }
    });
    // gather details for later creation of ConnectionPool...
    ports[0] = vm0.invoke("getTestClientMembershipEventsInClient_port", () -> ClientMembershipDUnitTest.getTestClientMembershipEventsInClient_port());
    assertTrue(ports[0] != 0);
    DistributedMember serverMember = (DistributedMember) vm0.invoke("get distributed member", () -> ClientMembershipDUnitTest.getDistributedMember());
    String serverMemberId = serverMember.toString();
    System.out.println("[testClientMembershipEventsInClient] ports[0]=" + ports[0]);
    System.out.println("[testClientMembershipEventsInClient] serverMember=" + serverMember);
    System.out.println("[testClientMembershipEventsInClient] serverMemberId=" + serverMemberId);
    assertFalse(fired[JOINED]);
    assertNull(member[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(member[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(member[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    // sanity check...
    System.out.println("[testClientMembershipEventsInClient] sanity check");
    InternalClientMembership.notifyServerJoined(serverLocation);
    Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(() -> fired[JOINED] || fired[CRASHED]);
    assertTrue(fired[JOINED]);
    assertNotNull(member[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(member[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(member[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // create bridge client in controller vm...
    System.out.println("[testClientMembershipEventsInClient] create bridge client");
    Properties config = new Properties();
    config.setProperty(MCAST_PORT, "0");
    config.setProperty(LOCATORS, "");
    config.setProperty(ENABLE_NETWORK_PARTITION_DETECTION, "false");
    getSystem(config);
    try {
        getCache();
        AttributesFactory factory = new AttributesFactory();
        factory.setScope(Scope.LOCAL);
        ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(Host.getHost(0)), ports, true, -1, -1, null);
        createRegion(name, factory.create());
        assertNotNull(getRootRegion().getSubregion(name));
    } catch (CacheException ex) {
        Assert.fail("While creating Region on Edge", ex);
    }
    Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(() -> fired[JOINED] || fired[CRASHED]);
    System.out.println("[testClientMembershipEventsInClient] assert client detected server join");
    // first check the getCurrentServers() result
    ClientCache clientCache = (ClientCache) getCache();
    Set<InetSocketAddress> servers = clientCache.getCurrentServers();
    assertTrue(!servers.isEmpty());
    InetSocketAddress serverAddr = servers.iterator().next();
    InetSocketAddress expectedAddr = new InetSocketAddress(serverMember.getHost(), ports[0]);
    assertEquals(expectedAddr, serverAddr);
    // now check listener results
    assertTrue(fired[JOINED]);
    assertNotNull(member[JOINED]);
    assertNotNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(member[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(member[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    vm0.invoke("Stop BridgeServer", () -> stopBridgeServers(getCache()));
    Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(() -> fired[JOINED] || fired[CRASHED]);
    System.out.println("[testClientMembershipEventsInClient] assert client detected server departure");
    assertFalse(fired[JOINED]);
    assertNull(member[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(member[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertTrue(fired[CRASHED]);
    assertNotNull(member[CRASHED]);
    assertNotNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // now test that we redisover the bridge server
    vm0.invoke("Recreate BridgeServer", () -> {
        try {
            System.out.println("[testClientMembershipEventsInClient] restarting BridgeServer");
            startBridgeServer(ports[0]);
        } catch (IOException e) {
            getSystem().getLogWriter().fine(new Exception(e));
            fail("Failed to start CacheServer on VM1: " + e.getMessage());
        }
    });
    Awaitility.await().pollInterval(50, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).pollDelay(50, TimeUnit.MILLISECONDS).until(() -> fired[JOINED] || fired[CRASHED]);
    System.out.println("[testClientMembershipEventsInClient] assert client detected server recovery");
    assertTrue(fired[JOINED]);
    assertNotNull(member[JOINED]);
    assertNotNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(member[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(member[CRASHED]);
    assertNull(memberId[CRASHED]);
}
Also used : CacheException(org.apache.geode.cache.CacheException) InetSocketAddress(java.net.InetSocketAddress) ClientMembershipListener(org.apache.geode.management.membership.ClientMembershipListener) IOException(java.io.IOException) ClientCache(org.apache.geode.cache.client.ClientCache) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) ClientMembershipEvent(org.apache.geode.management.membership.ClientMembershipEvent) InternalGemFireException(org.apache.geode.InternalGemFireException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) Region(org.apache.geode.cache.Region) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 12 with ClientMembershipListener

use of org.apache.geode.management.membership.ClientMembershipListener in project geode by apache.

the class ClientMembershipDUnitTest method doTestBasicEvents.

public void doTestBasicEvents() throws Exception {
    final boolean[] fired = new boolean[3];
    final DistributedMember[] member = new DistributedMember[3];
    final String[] memberId = new String[3];
    final boolean[] isClient = new boolean[3];
    ClientMembershipListener listener = new ClientMembershipListener() {

        public void memberJoined(ClientMembershipEvent event) {
            fired[JOINED] = true;
            member[JOINED] = event.getMember();
            memberId[JOINED] = event.getMemberId();
            isClient[JOINED] = event.isClient();
        }

        public void memberLeft(ClientMembershipEvent event) {
            fired[LEFT] = true;
            member[LEFT] = event.getMember();
            memberId[LEFT] = event.getMemberId();
            isClient[LEFT] = event.isClient();
        }

        public void memberCrashed(ClientMembershipEvent event) {
            fired[CRASHED] = true;
            member[CRASHED] = event.getMember();
            memberId[CRASHED] = event.getMemberId();
            isClient[CRASHED] = event.isClient();
        }
    };
    ClientMembership.registerClientMembershipListener(listener);
    // test JOIN for server
    InternalClientMembership.notifyServerJoined(serverLocation);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED];
    });
    assertTrue(fired[JOINED]);
    assertNotNull(member[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // test JOIN for client
    DistributedMember clientJoined = new TestDistributedMember("clientJoined");
    InternalClientMembership.notifyClientJoined(clientJoined);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED];
    });
    assertTrue(fired[JOINED]);
    assertEquals(clientJoined, member[JOINED]);
    assertEquals(clientJoined.getId(), memberId[JOINED]);
    assertTrue(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // test LEFT for server
    InternalClientMembership.notifyServerLeft(serverLocation);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[LEFT];
    });
    assertFalse(fired[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertTrue(fired[LEFT]);
    assertNotNull(member[LEFT]);
    assertFalse(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // test LEFT for client
    DistributedMember clientLeft = new TestDistributedMember("clientLeft");
    InternalClientMembership.notifyClientLeft(clientLeft);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[LEFT];
    });
    assertFalse(fired[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertTrue(fired[LEFT]);
    assertEquals(clientLeft, member[LEFT]);
    assertEquals(clientLeft.getId(), memberId[LEFT]);
    assertTrue(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // test CRASHED for server
    InternalClientMembership.notifyServerCrashed(serverLocation);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[CRASHED];
    });
    assertFalse(fired[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertTrue(fired[CRASHED]);
    assertNotNull(member[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // test CRASHED for client
    DistributedMember clientCrashed = new TestDistributedMember("clientCrashed");
    InternalClientMembership.notifyClientCrashed(clientCrashed);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[CRASHED];
    });
    assertFalse(fired[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertFalse(fired[LEFT]);
    assertNull(memberId[LEFT]);
    assertFalse(isClient[LEFT]);
    assertTrue(fired[CRASHED]);
    assertEquals(clientCrashed, member[CRASHED]);
    assertEquals(clientCrashed.getId(), memberId[CRASHED]);
    assertTrue(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) ClientMembershipListener(org.apache.geode.management.membership.ClientMembershipListener) ClientMembershipEvent(org.apache.geode.management.membership.ClientMembershipEvent)

Example 13 with ClientMembershipListener

use of org.apache.geode.management.membership.ClientMembershipListener in project geode by apache.

the class ClientMembershipDUnitTest method testMultipleListeners.

@Test
public void testMultipleListeners() throws Exception {
    final int NUM_LISTENERS = 4;
    final boolean[] fired = new boolean[NUM_LISTENERS];
    final DistributedMember[] member = new DistributedMember[NUM_LISTENERS];
    final String[] memberId = new String[NUM_LISTENERS];
    final boolean[] isClient = new boolean[NUM_LISTENERS];
    getSystem();
    final ClientMembershipListener[] listeners = new ClientMembershipListener[NUM_LISTENERS];
    for (int i = 0; i < NUM_LISTENERS; i++) {
        final int whichListener = i;
        listeners[i] = new ClientMembershipListener() {

            public void memberJoined(ClientMembershipEvent event) {
                assertFalse(fired[whichListener]);
                assertNull(member[whichListener]);
                assertNull(memberId[whichListener]);
                assertFalse(isClient[whichListener]);
                fired[whichListener] = true;
                member[whichListener] = event.getMember();
                memberId[whichListener] = event.getMemberId();
                isClient[whichListener] = event.isClient();
            }

            public void memberLeft(ClientMembershipEvent event) {
            }

            public void memberCrashed(ClientMembershipEvent event) {
            }
        };
    }
    final DistributedMember clientJoined = new TestDistributedMember("clientJoined");
    InternalClientMembership.notifyClientJoined(clientJoined);
    for (int i = 0; i < NUM_LISTENERS; i++) {
        synchronized (listeners[i]) {
            listeners[i].wait(20);
        }
        assertFalse(fired[i]);
        assertNull(member[i]);
        assertNull(memberId[i]);
        assertFalse(isClient[i]);
    }
    // attempt to register same listener twice... 2nd reg should be ignored
    // failure would cause an assertion failure in memberJoined impl
    ClientMembership.registerClientMembershipListener(listeners[0]);
    ClientMembership.registerClientMembershipListener(listeners[0]);
    ClientMembershipListener[] registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(1, registeredListeners.length);
    assertEquals(listeners[0], registeredListeners[0]);
    ClientMembership.registerClientMembershipListener(listeners[1]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(2, registeredListeners.length);
    assertEquals(listeners[0], registeredListeners[0]);
    assertEquals(listeners[1], registeredListeners[1]);
    InternalClientMembership.notifyClientJoined(clientJoined);
    synchronized (listeners[1]) {
        if (!fired[1]) {
            listeners[1].wait(2000);
        }
    }
    for (int i = 0; i < NUM_LISTENERS; i++) {
        if (i < 2) {
            assertTrue(fired[i]);
            assertEquals(clientJoined, member[i]);
            assertEquals(clientJoined.getId(), memberId[i]);
            assertTrue(isClient[i]);
        } else {
            assertFalse(fired[i]);
            assertNull(member[i]);
            assertNull(memberId[i]);
            assertFalse(isClient[i]);
        }
    }
    resetArraysForTesting(fired, member, memberId, isClient);
    ClientMembership.unregisterClientMembershipListener(listeners[0]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(1, registeredListeners.length);
    assertEquals(listeners[1], registeredListeners[0]);
    InternalClientMembership.notifyClientJoined(clientJoined);
    synchronized (listeners[1]) {
        if (!fired[1]) {
            listeners[1].wait(2000);
        }
    }
    for (int i = 0; i < NUM_LISTENERS; i++) {
        if (i == 1) {
            assertTrue(fired[i]);
            assertEquals(clientJoined, member[i]);
            assertEquals(clientJoined.getId(), memberId[i]);
            assertTrue(isClient[i]);
        } else {
            assertFalse(fired[i]);
            assertNull(member[i]);
            assertNull(memberId[i]);
            assertFalse(isClient[i]);
        }
    }
    resetArraysForTesting(fired, member, memberId, isClient);
    ClientMembership.registerClientMembershipListener(listeners[2]);
    ClientMembership.registerClientMembershipListener(listeners[3]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(3, registeredListeners.length);
    assertEquals(listeners[1], registeredListeners[0]);
    assertEquals(listeners[2], registeredListeners[1]);
    assertEquals(listeners[3], registeredListeners[2]);
    InternalClientMembership.notifyClientJoined(clientJoined);
    synchronized (listeners[3]) {
        if (!fired[3]) {
            listeners[3].wait(2000);
        }
    }
    for (int i = 0; i < NUM_LISTENERS; i++) {
        if (i != 0) {
            assertTrue(fired[i]);
            assertEquals(clientJoined, member[i]);
            assertEquals(clientJoined.getId(), memberId[i]);
            assertTrue(isClient[i]);
        } else {
            assertFalse(fired[i]);
            assertNull(member[i]);
            assertNull(memberId[i]);
            assertFalse(isClient[i]);
        }
    }
    resetArraysForTesting(fired, member, memberId, isClient);
    ClientMembership.registerClientMembershipListener(listeners[0]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(4, registeredListeners.length);
    assertEquals(listeners[1], registeredListeners[0]);
    assertEquals(listeners[2], registeredListeners[1]);
    assertEquals(listeners[3], registeredListeners[2]);
    assertEquals(listeners[0], registeredListeners[3]);
    InternalClientMembership.notifyClientJoined(clientJoined);
    synchronized (listeners[0]) {
        if (!fired[0]) {
            listeners[0].wait(2000);
        }
    }
    for (int i = 0; i < NUM_LISTENERS; i++) {
        assertTrue(fired[i]);
        assertEquals(clientJoined, member[i]);
        assertEquals(clientJoined.getId(), memberId[i]);
        assertTrue(isClient[i]);
    }
    resetArraysForTesting(fired, member, memberId, isClient);
    ClientMembership.unregisterClientMembershipListener(listeners[3]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(3, registeredListeners.length);
    assertEquals(listeners[1], registeredListeners[0]);
    assertEquals(listeners[2], registeredListeners[1]);
    assertEquals(listeners[0], registeredListeners[2]);
    InternalClientMembership.notifyClientJoined(clientJoined);
    synchronized (listeners[0]) {
        if (!fired[0]) {
            listeners[0].wait(2000);
        }
    }
    for (int i = 0; i < NUM_LISTENERS; i++) {
        if (i < 3) {
            assertTrue(fired[i]);
            assertEquals(clientJoined, member[i]);
            assertEquals(clientJoined.getId(), memberId[i]);
            assertTrue(isClient[i]);
        } else {
            assertFalse(fired[i]);
            assertNull(member[i]);
            assertNull(memberId[i]);
            assertFalse(isClient[i]);
        }
    }
    resetArraysForTesting(fired, member, memberId, isClient);
    ClientMembership.unregisterClientMembershipListener(listeners[2]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(2, registeredListeners.length);
    assertEquals(listeners[1], registeredListeners[0]);
    assertEquals(listeners[0], registeredListeners[1]);
    InternalClientMembership.notifyClientJoined(clientJoined);
    synchronized (listeners[0]) {
        if (!fired[0]) {
            listeners[0].wait(2000);
        }
    }
    for (int i = 0; i < NUM_LISTENERS; i++) {
        if (i < 2) {
            assertTrue(fired[i]);
            assertEquals(clientJoined, member[i]);
            assertEquals(clientJoined.getId(), memberId[i]);
            assertTrue(isClient[i]);
        } else {
            assertFalse(fired[i]);
            assertNull(member[i]);
            assertNull(memberId[i]);
            assertFalse(isClient[i]);
        }
    }
    resetArraysForTesting(fired, member, memberId, isClient);
    ClientMembership.unregisterClientMembershipListener(listeners[1]);
    ClientMembership.unregisterClientMembershipListener(listeners[0]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(0, registeredListeners.length);
    InternalClientMembership.notifyClientJoined(clientJoined);
    for (int i = 0; i < NUM_LISTENERS; i++) {
        synchronized (listeners[i]) {
            listeners[i].wait(20);
        }
        assertFalse(fired[i]);
        assertNull(member[i]);
        assertNull(memberId[i]);
        assertFalse(isClient[i]);
    }
    resetArraysForTesting(fired, member, memberId, isClient);
    ClientMembership.registerClientMembershipListener(listeners[1]);
    registeredListeners = ClientMembership.getClientMembershipListeners();
    assertEquals(1, registeredListeners.length);
    assertEquals(listeners[1], registeredListeners[0]);
    InternalClientMembership.notifyClientJoined(clientJoined);
    synchronized (listeners[1]) {
        if (!fired[1]) {
            listeners[1].wait(2000);
        }
    }
    for (int i = 0; i < NUM_LISTENERS; i++) {
        if (i == 1) {
            assertTrue(fired[i]);
            assertEquals(clientJoined, member[i]);
            assertEquals(clientJoined.getId(), memberId[i]);
            assertTrue(isClient[i]);
        } else {
            assertFalse(fired[i]);
            assertNull(member[i]);
            assertNull(memberId[i]);
            assertFalse(isClient[i]);
        }
    }
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) ClientMembershipListener(org.apache.geode.management.membership.ClientMembershipListener) ClientMembershipEvent(org.apache.geode.management.membership.ClientMembershipEvent) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 14 with ClientMembershipListener

use of org.apache.geode.management.membership.ClientMembershipListener in project geode by apache.

the class ClientMembershipDUnitTest method testLifecycle.

/**
   * Tests registration and event notification in conjunction with disconnecting and reconnecting to
   * DistributedSystem.
   */
@Test
public void testLifecycle() throws Exception {
    final boolean[] fired = new boolean[3];
    final DistributedMember[] member = new DistributedMember[3];
    final String[] memberId = new String[3];
    final boolean[] isClient = new boolean[3];
    // create and register ClientMembershipListener in controller vm...
    ClientMembershipListener listener = new ClientMembershipListener() {

        public void memberJoined(ClientMembershipEvent event) {
            assertFalse(fired[JOINED]);
            assertNull(member[JOINED]);
            assertNull(memberId[JOINED]);
            assertFalse(isClient[JOINED]);
            fired[JOINED] = true;
            member[JOINED] = event.getMember();
            memberId[JOINED] = event.getMemberId();
            isClient[JOINED] = event.isClient();
        }

        public void memberLeft(ClientMembershipEvent event) {
        }

        public void memberCrashed(ClientMembershipEvent event) {
        }
    };
    ClientMembership.registerClientMembershipListener(listener);
    // create loner in controller vm...
    Properties config = new Properties();
    config.setProperty(MCAST_PORT, "0");
    config.setProperty(LOCATORS, "");
    properties = config;
    getSystem(config);
    // assert that event is fired while connected
    InternalClientMembership.notifyServerJoined(serverLocation);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED];
    });
    assertTrue(fired[JOINED]);
    assertNotNull(member[JOINED]);
    assertFalse(isClient[JOINED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // assert that event is NOT fired while disconnected
    disconnectFromDS();
    InternalClientMembership.notifyServerJoined(serverLocation);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> {
        return true;
    });
    assertFalse(fired[JOINED]);
    assertNull(member[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // assert that event is fired again after reconnecting
    properties = config;
    InternalDistributedSystem sys = getSystem(config);
    assertTrue(sys.isConnected());
    InternalClientMembership.notifyServerJoined(serverLocation);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED];
    });
    assertTrue(fired[JOINED]);
    assertNotNull(member[JOINED]);
    assertFalse(isClient[JOINED]);
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) ClientMembershipListener(org.apache.geode.management.membership.ClientMembershipListener) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) ClientMembershipEvent(org.apache.geode.management.membership.ClientMembershipEvent) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Aggregations

ClientMembershipListener (org.apache.geode.management.membership.ClientMembershipListener)14 ClientMembershipEvent (org.apache.geode.management.membership.ClientMembershipEvent)11 Properties (java.util.Properties)7 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)7 AttributesFactory (org.apache.geode.cache.AttributesFactory)6 Region (org.apache.geode.cache.Region)6 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)6 VM (org.apache.geode.test.dunit.VM)6 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)6 Test (org.junit.Test)6 Host (org.apache.geode.test.dunit.Host)5 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)5 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)5 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)5 MembershipEvent (org.apache.geode.management.membership.MembershipEvent)4 UniversalMembershipListenerAdapter (org.apache.geode.management.membership.UniversalMembershipListenerAdapter)4 PoolImpl (org.apache.geode.cache.client.internal.PoolImpl)3 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)3 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)3 MembershipListener (org.apache.geode.management.membership.MembershipListener)3