Search in sources :

Example 1 with BaseServer

use of org.jgroups.blocks.cs.BaseServer in project JGroups by belaban.

the class ServerUnitTest method testReuseOfConnection.

/**
 * A connects to B and B connects to A at the same time. This test makes sure we only have <em>one</em> connection,
 * not two, e.g. a spurious connection. Tests http://jira.jboss.com/jira/browse/JGRP-549.<p/>
 * Turned concurrent test into a simple sequential test. We're going to replace this code with NIO2 soon anyway...
 */
public void testReuseOfConnection() throws Exception {
    for (boolean nio : new boolean[] { false, true }) {
        try (BaseServer a = create(nio, 0);
            BaseServer b = create(nio, 0)) {
            int num_conns;
            num_conns = a.getNumConnections();
            assert num_conns == 0;
            num_conns = b.getNumConnections();
            assert num_conns == 0;
            Address addr1 = a.localAddress(), addr2 = b.localAddress();
            byte[] data = "hello world".getBytes();
            send(data, a, addr2);
            send(data, b, addr1);
            String msg = "A: " + a + "\nB: " + b;
            System.out.println(msg);
            waitForOpenConns(1, a, b);
            // done in a loop because connect() might be non-blocking (NioServer)
            connectionEstablished(a, addr2);
            connectionEstablished(b, addr1);
        }
    }
}
Also used : BaseServer(org.jgroups.blocks.cs.BaseServer) InetAddress(java.net.InetAddress) Address(org.jgroups.Address)

Example 2 with BaseServer

use of org.jgroups.blocks.cs.BaseServer in project JGroups by belaban.

the class ServerUnitTest method testSendToAll.

public void testSendToAll() throws Exception {
    for (boolean nio : new boolean[] { false, true }) {
        try (BaseServer a = create(nio, 0);
            BaseServer b = create(nio, 0)) {
            long NUM = 1000, total_time;
            MyReceiver r1 = new MyReceiver(a, NUM, false);
            MyReceiver r2 = new MyReceiver(b, NUM, false);
            byte[] data = "hello world".getBytes();
            // send uncast to establish connection to s2:
            // a.send(b.localAddress(), new byte[1000], 0, 1000);
            send(data, a, b.localAddress());
            Util.sleep(1000);
            a.receiver(r1);
            b.receiver(r2);
            for (int i = 0; i < NUM; i++) send(data, a, null);
            log("sent " + NUM + " msgs");
            r2.waitForCompletion(20000);
            total_time = r2.stop_time - r2.start_time;
            log("number expected=" + r2.getNumExpected() + ", number received=" + r2.getNumReceived() + ", total time=" + total_time + " (" + (double) total_time / r2.getNumReceived() + " ms/msg)");
            Assert.assertEquals(r2.getNumExpected(), r2.getNumReceived());
            assert r1.getNumReceived() == 0 || r1.getNumReceived() > 0;
        }
    }
}
Also used : BaseServer(org.jgroups.blocks.cs.BaseServer)

Example 3 with BaseServer

use of org.jgroups.blocks.cs.BaseServer in project JGroups by belaban.

the class ServerUnitTest method testSendEmptyData.

public void testSendEmptyData() throws Exception {
    for (boolean nio : new boolean[] { false, true }) {
        try (BaseServer a = create(nio, 0)) {
            byte[] data = new byte[0];
            Address myself = a.localAddress();
            a.receiver(new ReceiverAdapter() {
            });
            send(data, a, myself);
        }
    }
}
Also used : BaseServer(org.jgroups.blocks.cs.BaseServer) InetAddress(java.net.InetAddress) Address(org.jgroups.Address) ReceiverAdapter(org.jgroups.blocks.cs.ReceiverAdapter)

Example 4 with BaseServer

use of org.jgroups.blocks.cs.BaseServer in project JGroups by belaban.

the class ServerUnitTest method create.

protected static BaseServer create(boolean nio, int port) {
    try {
        BaseServer retval = nio ? new NioServer(bind_addr, port).maxSendBuffers(1024) : new TcpServer(bind_addr, port);
        retval.usePeerConnections(true);
        retval.start();
        return retval;
    } catch (Exception ex) {
        return null;
    }
}
Also used : BaseServer(org.jgroups.blocks.cs.BaseServer) NioServer(org.jgroups.blocks.cs.NioServer) TcpServer(org.jgroups.blocks.cs.TcpServer) UnknownHostException(java.net.UnknownHostException)

Example 5 with BaseServer

use of org.jgroups.blocks.cs.BaseServer in project JGroups by belaban.

the class ServerUnitTest method testConnectionCountOnStop.

public void testConnectionCountOnStop() throws Exception {
    for (boolean nio : new boolean[] { false, true }) {
        try (BaseServer a = create(nio, 0);
            BaseServer b = create(nio, 0)) {
            Address addr1 = a.localAddress(), addr2 = b.localAddress();
            byte[] data = "hello world".getBytes();
            // send to self
            send(data, a, addr1);
            assert a.getNumConnections() == 0;
            // send to other
            send(data, a, addr2);
            // send to self
            send(data, b, addr2);
            // send to other
            send(data, b, addr1);
            System.out.println("A:\n" + a + "\nB:\n" + b);
            int num_conns_table1 = a.getNumConnections(), num_conns_table2 = b.getNumConnections();
            assert num_conns_table1 == 1 : "A should have 1 connection, but has " + num_conns_table1 + ": " + a;
            assert num_conns_table2 == 1 : "B should have 1 connection, but has " + num_conns_table2 + ": " + b;
            Util.close(b, a);
            waitForOpenConns(0, a, b);
        }
    }
}
Also used : BaseServer(org.jgroups.blocks.cs.BaseServer) InetAddress(java.net.InetAddress) Address(org.jgroups.Address)

Aggregations

BaseServer (org.jgroups.blocks.cs.BaseServer)10 InetAddress (java.net.InetAddress)7 Address (org.jgroups.Address)7 UnknownHostException (java.net.UnknownHostException)1 NioServer (org.jgroups.blocks.cs.NioServer)1 ReceiverAdapter (org.jgroups.blocks.cs.ReceiverAdapter)1 TcpServer (org.jgroups.blocks.cs.TcpServer)1