/*
 * 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 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 {

  public void testClientMembershipGroupGet() throws Throwable {
    Atomix client1 = createClient();
    Atomix client2 = createClient();
    testMembershipGroup(client1, client2, get("test-client-group-get", DistributedMembershipGroup.TYPE));
  }

  public void testClientMembershipGroupCreate() throws Throwable {
    Atomix client1 = createClient();
    Atomix client2 = createClient();
    testMembershipGroup(client1, client2, create("test-client-group-create", DistributedMembershipGroup.TYPE));
  }

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

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

  public void testMixMembershipGroup() throws Throwable {
    Atomix client = createClient();
    testMembershipGroup(replicas.get(0), client, create("test-group-mix", DistributedMembershipGroup.TYPE));
  }

  /**
   * 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);

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

    await();

    group1.join().thenRun(() -> {
      threadAssertEquals(group1.members().size(), 2);
      threadAssertEquals(group2.members().size(), 2);
      group1.onLeave(member -> resume());
      group2.leave().thenRun(this::resume);
    });

    await(0, 2);
  }

}

@@ -2387,32 +2387,81 @@
 thenRun(() -%3E %7B%0A
+      group2.members().thenAccept(members -%3E %7B%0A  
       threadAsse
@@ -2469,32 +2469,23 @@
 tEquals(
-group2.
 members
-()
 .size(),
@@ -2495,16 +2495,18 @@
 ;%0A      
+ 
+ 
 resume()
@@ -2503,24 +2503,34 @@
   resume();%0A
+      %7D);%0A
     %7D);%0A%0A   
@@ -2536,16 +2536,20 @@
   await(
+5000
 );%0A%0A    
@@ -2588,33 +2588,136 @@
     
-threadAssertEquals(
+group1.members().thenAccept(members -%3E %7B%0A        threadAssertEquals(members.size(), 2);%0A        resume();%0A      %7D);%0A      
 group
-1
+2
 .mem
@@ -2719,36 +2719,50 @@
 2.members().
-size(), 2);%0A
+thenAccept(members -%3E %7B%0A  
       thread
@@ -2774,32 +2774,23 @@
 tEquals(
-group2.
 members
-()
 .size(),
@@ -2792,24 +2792,80 @@
 ze(), 2);%0A  
+      resume();%0A      %7D);%0A    %7D);%0A%0A    await(5000, 2);%0A%0A
     group1.o
@@ -2892,18 +2892,16 @@
 ume());%0A
-  
     grou
@@ -2933,24 +2933,16 @@
 resume);
-%0A    %7D);
 %0A%0A    aw
@@ -2945,16 +2945,19 @@
   await(
+500
 0, 2);%0A 

Clean up Atomix membership group tests.