/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package io.atomix;

import io.atomix.coordination.DistributedMembershipGroup;
import io.atomix.coordination.LocalGroupMember;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.function.Function;

/**
 * Atomix membership group test.
 *
 * @author <a href="http://github.com/kuujo>Jordan Halterman</a>
 */
@Test
public class AtomixMembershipGroupTest extends AbstractAtomixTest {
  @BeforeClass
  protected void setupCluster() throws Throwable {
     createReplicas(5);
  }
  
  public void testClientMembershipGroupGet() throws Throwable {
    Atomix client1 = createClient();
    Atomix client2 = createClient();
    testMembershipGroup(client1, client2, get("test-client-group-get", DistributedMembershipGroup.class));
  }

  public void testReplicaMembershipGroupGet() throws Throwable {
    testMembershipGroup(replicas.get(0), replicas.get(1), get("test-replica-group-get", DistributedMembershipGroup.class));
  }

  /**
   * Tests a membership group.
   */
  private void testMembershipGroup(Atomix client1, Atomix client2, Function<Atomix, DistributedMembershipGroup> factory) throws Throwable {
    DistributedMembershipGroup group1 = factory.apply(client1);
    DistributedMembershipGroup group2 = factory.apply(client2);

    LocalGroupMember localMember = group2.join().get();
    group2.members().thenAccept(members -> {
      threadAssertEquals(members.size(), 1);
      resume();
    });

    await(5000);

    group1.join().thenRun(() -> {
      group1.members().thenAccept(members -> {
        threadAssertEquals(members.size(), 2);
        resume();
      });
      group2.members().thenAccept(members -> {
        threadAssertEquals(members.size(), 2);
        resume();
      });
    });

    await(5000, 2);

    group1.onLeave(member -> resume());
    localMember.leave().thenRun(this::resume);

    await(5000, 2);
  }

}

@@ -846,16 +846,52 @@
 ction;%0A%0A
+import static org.testng.Assert.*;%0A%0A
 /**%0A * A
@@ -1982,62 +1982,9 @@
     
-group2.members().thenAccept(members -%3E %7B%0A      threadA
+a
 sser
@@ -1983,39 +1983,48 @@
    assertEquals(
+group2.
 members
+()
 .size(), 1);%0A   
@@ -2024,134 +2024,43 @@
 1);%0A
-      resume();%0A    %7D);%0A%0A    await(5000);%0A%0A    group1.join().thenRun(() -%3E %7B%0A      group1.members().thenAccept(members -%3E %7B%0A  
+%0A    group1.join().thenRun(() -%3E %7B%0A
     
@@ -2072,39 +2072,48 @@
 eadAssertEquals(
+group1.
 members
+()
 .size(), 2);%0A   
@@ -2113,85 +2113,8 @@
 2);%0A
-        resume();%0A      %7D);%0A      group2.members().thenAccept(members -%3E %7B%0A  
     
@@ -2134,23 +2134,32 @@
 tEquals(
+group2.
 members
+()
 .size(),
@@ -2169,18 +2169,16 @@
 ;%0A      
-  
 resume()
@@ -2183,18 +2183,8 @@
 ();%0A
-      %7D);%0A
     
@@ -2202,19 +2202,16 @@
 ait(5000
-, 2
 );%0A%0A    

Update Atomix membership group tests.