Search in sources :

Example 11 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)

Example 12 with ClientMembershipEvent

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

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