package org.jgroups.tests;

import org.jgroups.Global;
import org.jgroups.JChannel;
import org.jgroups.View;
import org.jgroups.stack.GossipRouter;
import org.jgroups.util.StackType;
import org.jgroups.util.Util;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * Tests TCPGOSSIP protocol
 * 
 * @author Vladimir Blagojevic
 * 
 * @version $Id: TCPGOSSIP_Test.java,v 1.3 2010/05/05 15:31:29 belaban Exp $
 **/
@Test(groups = { Global.STACK_INDEPENDENT, Global.GOSSIP_ROUTER }, sequential = true)
public class TCPGOSSIP_Test extends ChannelTestBase {
    private JChannel channel, coordinator;
    private final static String GROUP = "TCPGOSSIP_Test";
    private GossipRouter gossipRouter;
    private static final String props = "tcpgossip.xml";

    @BeforeClass
    void startRouter() throws Exception {
        String bind_addr = Util.getProperty(Global.BIND_ADDR);
        if (bind_addr == null) {
            StackType type = Util.getIpStackType();
            if (type == StackType.IPv6)
                bind_addr = "::1";
            else
                bind_addr = "127.0.0.1";
        }
        gossipRouter = new GossipRouter(12001, bind_addr);
        gossipRouter.start();
    }

    @AfterClass(alwaysRun = true)
    void stopRouter() throws Exception {
        gossipRouter.stop();
    }

    @AfterMethod(alwaysRun = true)
    void tearDown() throws Exception {
        Util.close(channel, coordinator);
    }

    /**
     * Tests connect-disconnect-connect sequence for a group with two members (using default
     * configuration).
     **/
    public void testDisconnectConnectTwo() throws Exception {
        coordinator = new JChannel(props);
        channel = new JChannel(props);
        coordinator.connect(GROUP);
        channel.connect("DisconnectTest.testgroup-1");
        channel.disconnect();
        channel.connect(GROUP);
        View view = channel.getView();
        assert view.size() == 2;
        assert view.containsMember(channel.getAddress());
        assert view.containsMember(coordinator.getAddress());
    }

    public void testConnectThree() throws Exception {
        JChannel third = null;
        try {
            coordinator = new JChannel(props);
            channel = new JChannel(props);
            coordinator.connect(GROUP);
            channel.connect(GROUP);
            third = new JChannel(props);
            third.connect(GROUP);
            View view = channel.getView();
            assert channel.getView().size() == 3;
            assert third.getView().size() == 3;
            assert view.containsMember(channel.getAddress());
            assert view.containsMember(coordinator.getAddress());
        } finally {
            Util.close(third);
        }
    }

    public void testConnectThreeChannelsWithGRDown() throws Exception {
        JChannel third = null;
        try {
            coordinator = new JChannel(props);
            channel = new JChannel(props);
            coordinator.connect("testConnectThreeChannelsWithGRDown");
            channel.connect("testConnectThreeChannelsWithGRDown");

            // kill router
            gossipRouter.stop();
            

            // cannot discover others since GR is down
            third = new JChannel(props);
            third.connect("testConnectThreeChannelsWithGRDown");
           

            // restart and....
            gossipRouter.start();
            Util.blockUntilViewsReceived(60000, 500, coordinator, channel, third);

            // confirm they found each other
            View view = channel.getView();
            assert channel.getView().size() == 3;
            assert third.getView().size() == 3;
            assert view.containsMember(channel.getLocalAddress());
            assert view.containsMember(coordinator.getLocalAddress());
        } finally {
            Util.close(third);
        }
    }

    public void testConnectThreeChannelsWithGRAlreadyDown() throws Exception {
        JChannel third = null;
        try {
            coordinator = new JChannel(props);
            channel = new JChannel(props);
            
            // kill router
            gossipRouter.stop();
            
            // cannot discover others since GR is down
            coordinator.connect("testConnectThreeChannelsWithGRAlreadyDown");          
            channel.connect("testConnectThreeChannelsWithGRAlreadyDown");

            third = new JChannel(props);
            third.connect("testConnectThreeChannelsWithGRAlreadyDown");

            // restart and....
            gossipRouter.start();
            Util.blockUntilViewsReceived(60000, 500, coordinator, channel, third);

            // confirm they found each other
            View view = channel.getView();
            assert channel.getView().size() == 3;
            assert third.getView().size() == 3;
            assert view.containsMember(channel.getLocalAddress());
            assert view.containsMember(coordinator.getLocalAddress());
        } finally {
            Util.close(third);
        }
    }
}
@@ -125,26 +125,101 @@
 ups.
-stack.GossipRouter
+protocols.TCPGOSSIP;%0Aimport org.jgroups.stack.GossipRouter;%0Aimport org.jgroups.stack.Protocol
 ;%0Aim
@@ -558,17 +558,17 @@
 ava,v 1.
-3
+4
  2010/05
@@ -572,27 +572,25 @@
 /05/
-05
+31
  15:
-31:29 belaban
+48:33 vlada
  Exp
@@ -986,32 +986,223 @@
 ows Exception %7B%0A
+        String bind_addr = getRouterBindAddress();%0A        gossipRouter = new GossipRouter(12001, bind_addr);%0A        gossipRouter.start();%0A    %7D%0A%0A    private String getRouterBindAddress() %7B%0A
         String b
@@ -1488,87 +1488,24 @@
     
-gossipRouter = new GossipRouter(12001, bind_addr);%0A        gossipRouter.start()
+return bind_addr
 ;%0A  
@@ -2137,24 +2137,24 @@
 sconnect();%0A
-
         chan
@@ -2366,24 +2366,699 @@
 ss());%0A    %7D
+%0A    %0A    public void testAddInitialHosts() throws Exception %7B%0A        coordinator = new JChannel(props);%0A        channel = new JChannel(props);%0A        coordinator.connect(GROUP);%0A        channel.connect(GROUP);%0A        TCPGOSSIP p = (TCPGOSSIP) channel.getProtocolStack().findProtocol(TCPGOSSIP.class);%0A        String bind_addr = getRouterBindAddress();%0A        assert p.removeInitialHost(bind_addr, 12001);%0A        p.addInitialHost(bind_addr, 12001);%0A               %0A       %0A        View view = channel.getView();%0A        assert view.size() == 2;%0A        assert view.containsMember(channel.getAddress());%0A        assert view.containsMember(coordinator.getAddress());%0A    %7D
 %0A%0A    public

test addInitialHosts