Search in sources :

Example 6 with MembershipEvent

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

the class UniversalMembershipListenerAdapterDUnitTest method testNoDuplicates.

/**
   * Tests use of history to prevent duplicate events.
   */
@Test
public void testNoDuplicates() throws Exception {
    getSystem();
    final boolean[] fired = new boolean[3];
    final DistributedMember[] member = new DistributedMember[3];
    final String[] memberId = new String[3];
    UniversalMembershipListenerAdapter listener = new UniversalMembershipListenerAdapter() {

        @Override
        public synchronized void memberJoined(MembershipEvent event) {
            assertFalse(fired[JOINED]);
            assertNull(member[JOINED]);
            assertNull(memberId[JOINED]);
            fired[JOINED] = true;
            member[JOINED] = event.getDistributedMember();
            memberId[JOINED] = event.getMemberId();
            notify();
        }

        @Override
        public synchronized void memberLeft(MembershipEvent event) {
            assertFalse(fired[LEFT]);
            assertNull(member[LEFT]);
            assertNull(memberId[LEFT]);
            fired[LEFT] = true;
            member[LEFT] = event.getDistributedMember();
            memberId[LEFT] = event.getMemberId();
            notify();
        }

        @Override
        public synchronized void memberCrashed(MembershipEvent event) {
            // assures no dupes
            assertFalse(fired[CRASHED]);
            assertNull(member[CRASHED]);
            assertNull(memberId[CRASHED]);
            fired[CRASHED] = true;
            member[CRASHED] = event.getDistributedMember();
            memberId[CRASHED] = event.getMemberId();
            notify();
        }
    };
    DistributedMember memberA = new TestDistributedMember("memberA");
    ServerLocation serverA = new ServerLocation("serverA", 0);
    // first join
    InternalClientMembership.notifyClientJoined(memberA);
    synchronized (listener) {
        if (!fired[JOINED]) {
            listener.wait(SYNC_ASYNC_EVENT_WAIT_MILLIS);
        }
    }
    assertTrue(fired[JOINED]);
    assertEquals(memberA, member[JOINED]);
    assertEquals(memberA.getId(), memberId[JOINED]);
    fired[JOINED] = false;
    member[JOINED] = null;
    memberId[JOINED] = null;
    // duplicate join
    InternalClientMembership.notifyClientJoined(memberA);
    Wait.pause(BRIEF_PAUSE_MILLIS);
    assertFalse(fired[JOINED]);
    assertNull(member[JOINED]);
    assertNull(memberId[JOINED]);
    // first left
    InternalClientMembership.notifyClientLeft(memberA);
    synchronized (listener) {
        if (!fired[LEFT]) {
            listener.wait(SYNC_ASYNC_EVENT_WAIT_MILLIS);
        }
    }
    assertTrue(fired[LEFT]);
    assertEquals(memberA, member[LEFT]);
    assertEquals(memberA.getId(), memberId[LEFT]);
    fired[LEFT] = false;
    member[LEFT] = null;
    memberId[LEFT] = null;
    // duplicate left
    InternalClientMembership.notifyClientLeft(memberA);
    Wait.pause(BRIEF_PAUSE_MILLIS);
    assertFalse(fired[LEFT]);
    assertNull(member[LEFT]);
    assertNull(memberId[LEFT]);
    // rejoin
    InternalClientMembership.notifyClientJoined(memberA);
    synchronized (listener) {
        if (!fired[JOINED]) {
            listener.wait(SYNC_ASYNC_EVENT_WAIT_MILLIS);
        }
    }
    assertTrue(fired[JOINED]);
    assertEquals(memberA, member[JOINED]);
    assertEquals(memberA.getId(), memberId[JOINED]);
}
Also used : UniversalMembershipListenerAdapter(org.apache.geode.management.membership.UniversalMembershipListenerAdapter) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ClientMembershipEvent(org.apache.geode.management.membership.ClientMembershipEvent) MembershipEvent(org.apache.geode.management.membership.MembershipEvent) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

ClientMembershipEvent (org.apache.geode.management.membership.ClientMembershipEvent)6 MembershipEvent (org.apache.geode.management.membership.MembershipEvent)6 UniversalMembershipListenerAdapter (org.apache.geode.management.membership.UniversalMembershipListenerAdapter)6 Properties (java.util.Properties)4 AttributesFactory (org.apache.geode.cache.AttributesFactory)4 Region (org.apache.geode.cache.Region)4 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)4 ClientMembershipListener (org.apache.geode.management.membership.ClientMembershipListener)4 Host (org.apache.geode.test.dunit.Host)4 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)4 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)4 VM (org.apache.geode.test.dunit.VM)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 LogWriter (org.apache.geode.LogWriter)2 InternalLogWriter (org.apache.geode.internal.logging.InternalLogWriter)2 LocalLogWriter (org.apache.geode.internal.logging.LocalLogWriter)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)2