package org.jgroups.tests;


import org.testng.annotations.*;
import org.jgroups.Address;
import org.jgroups.Channel;
import org.jgroups.ChannelException;
import org.jgroups.View;
import org.jgroups.blocks.ReplicatedHashMap;
import org.jgroups.util.Util;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 * Tests concurrent startup or replicated hashmap.
 * 
 * @author vlada
 * @version $Id: ReplicatedHashMapStartupTest.java,v 1.5 2008/04/09 15:01:35 belaban Exp $
 */
public class ReplicatedHashMapStartupTest extends ChannelTestBase {

    private static final int FACTORY_COUNT = 8;


    protected int getMuxFactoryCount() {
        return FACTORY_COUNT;
    }

    protected boolean useBlocking() {
        return true;
    }

    @Test
    public void testConcurrentStartup4Members() {
        concurrentStartupHelper(4);
    }

    @Test
    public void testConcurrentStartup8Members() {
        concurrentStartupHelper(8);
    }

    protected void concurrentStartupHelper(int channelCount) {
        List<ReplicatedHashMap<Address, Integer>> channels = new ArrayList<ReplicatedHashMap<Address, Integer>>(channelCount);        
        MyNotification<Address, Integer> n = new MyNotification<Address, Integer>();
       
        for (int i = 0; i < channelCount; i++) {
            try {
                Channel c = createChannel();
                c.setOpt(Channel.AUTO_RECONNECT, true);                
                ReplicatedHashMap<Address, Integer> map = new ReplicatedHashMap<Address, Integer>(c);
                channels.add(map);
                map.addNotifier(n);
                map.setBlockingUpdates(true);                              
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }
        
        //do a very concurrent startup
        for (ReplicatedHashMap<Address, Integer> map : channels) {
            try {
                map.getChannel().connect("bla");
                map.start(0);
                map.put(map.getChannel().getLocalAddress(), new Integer(1));
                Util.sleep(1000);
            } catch (ChannelException e) {               
                e.printStackTrace();
            }
        }
        
        //verify all view are correct
        for (ReplicatedHashMap<Address, Integer> map : channels) {
            Assert.assertEquals(map.getChannel().getView().size(), channelCount, "Correct view");
        }
               
        for (ReplicatedHashMap<Address, Integer> map : channels) {
            map.removeNotifier(n);
        }
        
        //verify all maps have all elements
        for (ReplicatedHashMap<Address, Integer> map : channels) {
            Assert.assertEquals(map.size(), channelCount, "Correct size");
        }
       
        System.out.println("stopping");
        for (ReplicatedHashMap<Address, Integer> map : channels) {
            map.stop();
            Util.sleep(1000);
        }
              
    }
    private static class MyNotification<K extends Serializable, V extends Serializable>
            implements org.jgroups.blocks.ReplicatedHashMap.Notification<K, V> {

        public void contentsCleared() {           
        }

        public void contentsSet(Map<K, V> new_entries) {       
        }

        public void entryRemoved(K key) {     
        }

        public void entrySet(K key, V value) {                    
        }

        public void viewChange(View view, Vector<Address> new_mbrs,
                Vector<Address> old_mbrs) {
            System.out.println("Got view " + view);                      
        }

      
    }

}

@@ -33,33 +33,22 @@
 ort org.
-testng.annotation
+jgroup
 s.*;%0Aimp
@@ -67,150 +67,113 @@
 ups.
-Address;%0Aimport org.jgroups.Channel;%0Aimport org.jgroups.ChannelException;%0Aimport org.jgroups.View;%0Aimport org.jgroups.blocks.ReplicatedHashMap
+blocks.ReplicatedHashMap;%0Aimport org.jgroups.protocols.pbcast.GMS;%0Aimport org.jgroups.stack.ProtocolStack
 ;%0Aim
@@ -260,20 +260,12 @@
 ons.
-BeforeMethod
+Test
 ;%0A%0Ai
@@ -526,29 +526,29 @@
 v 1.
-5
+6
  2008/0
-4
+6
 /09 1
-5:01:35
+3:03:06
  bel
@@ -562,16 +562,55 @@
 p $%0A */%0A
+@Test(groups=%7B%22temp%22%7D,sequential=true)%0A
 public c
@@ -674,135 +674,8 @@
  %7B%0A%0A
-    private static final int FACTORY_COUNT = 8;%0A%0A%0A    protected int getMuxFactoryCount() %7B%0A        return FACTORY_COUNT;%0A    %7D%0A
 %0A   
@@ -733,34 +733,24 @@
 rue;%0A    %7D%0A%0A
-    @Test%0A
     public v
@@ -834,18 +834,8 @@
  %7D%0A%0A
-    @Test%0A
     
@@ -1206,23 +1206,45 @@
 ger%3E();%0A
+%0A
        
+ JChannel first=null;
 %0A       
@@ -1319,16 +1319,17 @@
         
+J
 Channel 
@@ -1333,25 +1333,241 @@
 el c
- = createChannel(
+;%0A                if(i == 0) %7B%0A                    c = createChannel(true, channelCount);%0A                    modifyGMS(c);%0A                    first=c;%0A                %7D%0A                else%0A                    c=createChannel(first
 );%0A 
@@ -3177,24 +3177,252 @@
      %0A    %7D%0A
+%0A    private static void modifyGMS(JChannel c) %7B%0A        ProtocolStack stack=c.getProtocolStack();%0A        GMS gms=(GMS)stack.findProtocol(GMS.class);%0A        if(gms != null)%0A            gms.setLogCollectMessages(false);%0A    %7D%0A%0A
     private 

converted to TestNG