Network/test/dorkbox/network/MultipleServerTest.java

130 lines
5.0 KiB
Java
Raw Normal View History

2016-03-07 01:06:43 +01:00
/* Copyright (c) 2008, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2014-08-20 23:44:59 +02:00
package dorkbox.network;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
2014-08-20 23:44:59 +02:00
import dorkbox.network.connection.Connection;
2014-09-26 18:49:52 +02:00
import dorkbox.network.connection.Listener;
import dorkbox.network.serialization.Serialization;
import dorkbox.util.exceptions.SecurityException;
2015-07-17 02:46:00 +02:00
public
class MultipleServerTest extends BaseTest {
2014-08-20 23:44:59 +02:00
AtomicInteger received = new AtomicInteger();
@Test
2015-07-17 02:46:00 +02:00
public
2018-01-27 23:05:45 +01:00
void multipleServers() throws SecurityException, IOException {
2015-07-20 14:18:34 +02:00
Configuration configuration1 = new Configuration();
configuration1.tcpPort = tcpPort;
configuration1.udpPort = udpPort;
configuration1.localChannelName = "chan1";
configuration1.serialization = Serialization.DEFAULT();
configuration1.serialization.register(String[].class);
2014-08-20 23:44:59 +02:00
2015-07-20 14:18:34 +02:00
Server server1 = new Server(configuration1);
2014-08-20 23:44:59 +02:00
addEndPoint(server1);
server1.bind(false);
2015-07-17 02:46:00 +02:00
server1.listeners()
.add(new Listener.OnMessageReceived<Connection, String>() {
2015-07-17 02:46:00 +02:00
@Override
public
void received(Connection connection, String object) {
if (!object.equals("client1")) {
fail();
}
if (MultipleServerTest.this.received.incrementAndGet() == 2) {
stopEndPoints();
}
}
});
2014-08-20 23:44:59 +02:00
2015-07-20 14:18:34 +02:00
Configuration configuration2 = new Configuration();
configuration2.tcpPort = tcpPort + 1;
configuration2.udpPort = udpPort + 1;
configuration2.localChannelName = "chan2";
configuration2.serialization = Serialization.DEFAULT();
configuration2.serialization.register(String[].class);
2014-08-20 23:44:59 +02:00
2015-07-20 14:18:34 +02:00
Server server2 = new Server(configuration2);
2015-07-17 02:46:00 +02:00
2014-08-20 23:44:59 +02:00
addEndPoint(server2);
server2.bind(false);
2015-07-17 02:46:00 +02:00
server2.listeners()
.add(new Listener.OnMessageReceived<Connection, String>() {
2015-07-17 02:46:00 +02:00
@Override
public
void received(Connection connection, String object) {
if (!object.equals("client2")) {
fail();
}
if (MultipleServerTest.this.received.incrementAndGet() == 2) {
stopEndPoints();
}
}
});
2014-08-20 23:44:59 +02:00
// ----
2015-07-20 14:18:34 +02:00
configuration1.localChannelName = null;
configuration1.host = host;
2015-07-20 14:18:34 +02:00
Client client1 = new Client(configuration1);
2014-08-20 23:44:59 +02:00
addEndPoint(client1);
2015-07-17 02:46:00 +02:00
client1.listeners()
.add(new Listener.OnConnected<Connection>() {
2015-07-17 02:46:00 +02:00
@Override
public
void connected(Connection connection) {
connection.send()
.TCP("client1");
}
});
2014-08-20 23:44:59 +02:00
client1.connect(5000);
2015-07-20 14:18:34 +02:00
configuration2.localChannelName = null;
configuration2.host = host;
2015-07-20 14:18:34 +02:00
Client client2 = new Client(configuration2);
2014-08-20 23:44:59 +02:00
addEndPoint(client2);
2015-07-17 02:46:00 +02:00
client2.listeners()
.add(new Listener.OnConnected<Connection>() {
2015-07-17 02:46:00 +02:00
@Override
public
void connected(Connection connection) {
connection.send()
.TCP("client2");
}
});
2014-08-20 23:44:59 +02:00
client2.connect(5000);
waitForThreads(30);
}
}