Search in sources :

Example 1 with ClientMembershipEvent

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

the class ClientMembershipDUnitTest method testUnregisterClientMembershipListener.

/**
   * Tests unregisterClientMembershipListener to ensure that no further events are delivered to
   * unregistered listeners.
   */
@Test
public void testUnregisterClientMembershipListener() throws Exception {
    final boolean[] fired = new boolean[1];
    final DistributedMember[] member = new DistributedMember[1];
    final String[] memberId = new String[1];
    final boolean[] isClient = new boolean[1];
    getSystem();
    ClientMembershipListener listener = new ClientMembershipListener() {

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

        public void memberLeft(ClientMembershipEvent event) {
        }

        public void memberCrashed(ClientMembershipEvent event) {
        }
    };
    ClientMembership.registerClientMembershipListener(listener);
    // fire event to make sure listener is registered
    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[0]);
    assertEquals(clientJoined, member[0]);
    assertEquals(clientJoined.getId(), memberId[0]);
    assertTrue(isClient[0]);
    resetArraysForTesting(fired, member, memberId, isClient);
    assertFalse(fired[0]);
    assertNull(memberId[0]);
    assertFalse(isClient[0]);
    // unregister and verify listener is not notified
    ClientMembership.unregisterClientMembershipListener(listener);
    InternalClientMembership.notifyClientJoined(clientJoined);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).until(() -> {
        return true;
    });
    assertFalse(fired[0]);
    assertNull(member[0]);
    assertNull(memberId[0]);
    assertFalse(isClient[0]);
}
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 2 with ClientMembershipEvent

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

the class ClientMembershipDUnitTest method testClientMembershipEventsInServer.

/**
   * Tests notification of events in server process. Bridge servers detect client joins when the
   * client connects to the server.
   */
@Test
public void testClientMembershipEventsInServer() 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) {
            System.out.println("[testClientMembershipEventsInServer] memberJoined: " + event);
            fired[JOINED] = true;
            member[JOINED] = event.getMember();
            memberId[JOINED] = event.getMemberId();
            isClient[JOINED] = event.isClient();
            assertFalse(fired[LEFT] || fired[CRASHED]);
        }

        public void memberLeft(ClientMembershipEvent event) {
            System.out.println("[testClientMembershipEventsInServer] memberLeft: " + event);
            fired[LEFT] = true;
            member[LEFT] = event.getMember();
            memberId[LEFT] = event.getMemberId();
            isClient[LEFT] = event.isClient();
            assertFalse(fired[JOINED] || fired[CRASHED]);
        }

        public void memberCrashed(ClientMembershipEvent event) {
            System.out.println("[testClientMembershipEventsInServer] memberCrashed: " + event);
            fired[CRASHED] = true;
            member[CRASHED] = event.getMember();
            memberId[CRASHED] = event.getMemberId();
            isClient[CRASHED] = event.isClient();
            assertFalse(fired[JOINED] || fired[LEFT]);
        }
    };
    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 controller vm...
    System.out.println("[testClientMembershipEventsInServer] Create BridgeServer");
    getSystem();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    Region region = createRegion(name, factory.create());
    assertNotNull(region);
    assertNotNull(getRootRegion().getSubregion(name));
    ports[0] = startBridgeServer(0);
    assertTrue(ports[0] != 0);
    DistributedMember serverMember = getMemberId();
    String serverMemberId = serverMember.toString();
    System.out.println("[testClientMembershipEventsInServer] ports[0]=" + ports[0]);
    System.out.println("[testClientMembershipEventsInServer] serverMemberId=" + serverMemberId);
    System.out.println("[testClientMembershipEventsInServer] serverMember=" + serverMember);
    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("[testClientMembershipEventsInServer] sanity check");
    DistributedMember test = new TestDistributedMember("test");
    InternalClientMembership.notifyClientJoined(test);
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED] || fired[LEFT] || fired[CRASHED];
    });
    assertTrue(fired[JOINED]);
    assertEquals(test, member[JOINED]);
    assertEquals(test.getId(), memberId[JOINED]);
    assertTrue(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);
    final Host host = Host.getHost(0);
    SerializableCallable createConnectionPool = new SerializableCallable("Create connectionPool") {

        public Object call() {
            System.out.println("[testClientMembershipEventsInServer] create bridge client");
            Properties config = new Properties();
            config.setProperty(MCAST_PORT, "0");
            config.setProperty(LOCATORS, "");
            config.setProperty(ENABLE_NETWORK_PARTITION_DETECTION, "false");
            properties = config;
            DistributedSystem s = getSystem(config);
            AttributesFactory factory = new AttributesFactory();
            Pool pool = ClientServerTestCase.configureConnectionPool(factory, NetworkUtils.getServerHostName(host), ports, true, -1, 2, null);
            createRegion(name, factory.create());
            assertNotNull(getRootRegion().getSubregion(name));
            // see geode-1078
            assertTrue(s == basicGetSystem());
            return getMemberId();
        }
    };
    // create bridge client in vm0...
    DistributedMember clientMember = (DistributedMember) vm0.invoke(createConnectionPool);
    String clientMemberId = clientMember.toString();
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED] || fired[LEFT] || fired[CRASHED];
    });
    System.out.println("[testClientMembershipEventsInServer] assert server detected client join");
    assertTrue(fired[JOINED]);
    assertEquals(member[JOINED] + " should equal " + clientMember, clientMember, member[JOINED]);
    assertEquals(memberId[JOINED] + " should equal " + clientMemberId, clientMemberId, memberId[JOINED]);
    assertTrue(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);
    pauseForClientToJoin();
    vm0.invoke(new SerializableRunnable("Stop bridge client") {

        public void run() {
            System.out.println("[testClientMembershipEventsInServer] Stop bridge client");
            getRootRegion().getSubregion(name).close();
            Map m = PoolManager.getAll();
            Iterator mit = m.values().iterator();
            while (mit.hasNext()) {
                Pool p = (Pool) mit.next();
                p.destroy();
            }
        }
    });
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED] || fired[LEFT] || fired[CRASHED];
    });
    System.out.println("[testClientMembershipEventsInServer] assert server detected client left");
    assertFalse(fired[JOINED]);
    assertNull(member[JOINED]);
    assertNull(memberId[JOINED]);
    assertFalse(isClient[JOINED]);
    assertTrue(fired[LEFT]);
    assertEquals(clientMember, member[LEFT]);
    assertEquals(clientMemberId, memberId[LEFT]);
    assertTrue(isClient[LEFT]);
    assertFalse(fired[CRASHED]);
    assertNull(member[CRASHED]);
    assertNull(memberId[CRASHED]);
    assertFalse(isClient[CRASHED]);
    resetArraysForTesting(fired, member, memberId, isClient);
    // reconnect bridge client to test for crashed event
    clientMemberId = vm0.invoke(createConnectionPool).toString();
    Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
        return fired[JOINED] || fired[LEFT] || fired[CRASHED];
    });
    System.out.println("[testClientMembershipEventsInServer] assert server detected client re-join");
    assertTrue(fired[JOINED]);
    assertEquals(clientMember, member[JOINED]);
    assertEquals(clientMemberId, memberId[JOINED]);
    assertTrue(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);
    pauseForClientToJoin();
    ServerConnection.setForceClientCrashEvent(true);
    try {
        vm0.invoke(new SerializableRunnable("Stop bridge client") {

            public void run() {
                System.out.println("[testClientMembershipEventsInServer] Stop bridge client");
                getRootRegion().getSubregion(name).close();
                Map m = PoolManager.getAll();
                Iterator mit = m.values().iterator();
                while (mit.hasNext()) {
                    Pool p = (Pool) mit.next();
                    p.destroy();
                }
            }
        });
        Awaitility.await().pollInterval(100, TimeUnit.MILLISECONDS).pollDelay(100, TimeUnit.MILLISECONDS).timeout(300, TimeUnit.SECONDS).until(() -> {
            return fired[JOINED] || fired[LEFT] || fired[CRASHED];
        });
        System.out.println("[testClientMembershipEventsInServer] assert server detected client crashed");
        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]);
        assertEquals(clientMember, member[CRASHED]);
        assertEquals(clientMemberId, memberId[CRASHED]);
        assertTrue(isClient[CRASHED]);
    } finally {
        ServerConnection.setForceClientCrashEvent(false);
    }
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) ClientMembershipListener(org.apache.geode.management.membership.ClientMembershipListener) Host(org.apache.geode.test.dunit.Host) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) ClientMembershipEvent(org.apache.geode.management.membership.ClientMembershipEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Pool(org.apache.geode.cache.client.Pool) Map(java.util.Map) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 3 with ClientMembershipEvent

use of org.apache.geode.management.membership.ClientMembershipEvent 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 4 with ClientMembershipEvent

use of org.apache.geode.management.membership.ClientMembershipEvent 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 5 with ClientMembershipEvent

use of org.apache.geode.management.membership.ClientMembershipEvent 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)

Aggregations

ClientMembershipEvent (org.apache.geode.management.membership.ClientMembershipEvent)12 ClientMembershipListener (org.apache.geode.management.membership.ClientMembershipListener)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 GemFireCacheImpl (org.apache.geode.internal.cache.GemFireCacheImpl)3 MembershipListener (org.apache.geode.management.membership.MembershipListener)3 IOException (java.io.IOException)2