Move some test code and add junit tests for TCP channel and socket communication

This commit is contained in:
2015-06-15 22:57:19 +01:00
parent 2775033012
commit 139bb64b0b
17 changed files with 453 additions and 6 deletions

View File

@@ -0,0 +1,24 @@
package test.server;
import test.server.dummy.DummyChannelTcpClient;
import test.server.dummy.DummySocketTcpClient;
import test.server.dummy.DummyTcpServerClient;
import test.server.dummy.DummyWriter;
import base.server.channel.TcpServer;
public class TestTcpCommunication {
public static void main(String[] args) {
try {
// Test client > server
new TcpServer(1234, DummyTcpServerClient.class).start();
DummySocketTcpClient client1 = new DummySocketTcpClient("localhost", 1234);
DummyChannelTcpClient client2 = new DummyChannelTcpClient("localhost", 1234);
client1.start();
client2.start();
Thread.sleep(1000);
client1.send("Succes!".getBytes());
new DummyWriter(client1).start();
} catch (Exception e) {}
}
}

View File

@@ -0,0 +1,24 @@
package test.server;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import test.server.dummy.DummyUdpServer;
import base.sender.UdpSender;
import base.server.datagram.UdpServer;
public class TestUdpCommunication {
public static void main(String[] args) {
// Test Client > Server
UdpServer server = new DummyUdpServer(1234);
server.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
try {
UdpSender sender = new UdpSender("255.255.255.255", 1234);
sender.send("Ciao!".getBytes(Charset.defaultCharset()));
} catch (UnknownHostException e) {}
}
}

View File

@@ -0,0 +1,27 @@
package test.server;
import test.server.dummy.DummyUdpListen;
import test.server.dummy.DummyWriter;
import base.server.datagram.UdpMulticastClient;
import base.server.datagram.UdpMulticastServer;
public class TestUdpMulticastCommunication {
public static void main(String[] args) {
// Test Client (multicast) < Server
String host = "239.255.255.255";
int port = 4446;
UdpMulticastServer y = new UdpMulticastServer(host, port);
y.start();
UdpMulticastClient x = new UdpMulticastClient(host, port);
x.start();
DummyUdpListen z = new DummyUdpListen();
x.register(z);
z.start();
new DummyWriter(y).start();
try {
Thread.sleep(100000);
} catch (InterruptedException e) {}
}
}

View File

@@ -0,0 +1,13 @@
package test.server.dummy;
import base.server.socket.TcpClient;
public class DummyChannelTcpClient extends TcpClient {
public DummyChannelTcpClient(String host, int port) {
super(host, port);
}
public void input(byte[] buffer) {
System.out.println("Client: " + new String(buffer).trim());
}
}

View File

@@ -0,0 +1,10 @@
package test.server.dummy;
import base.work.Listen;
public class DummyListen extends Listen<byte[]> {
public void receive(byte[] buffer) {
String output = new String(buffer).trim();
System.out.println("Client: message read: " + output);
}
}

View File

@@ -0,0 +1,13 @@
package test.server.dummy;
import base.server.socket.TcpClient;
public class DummySocketTcpClient extends TcpClient {
public DummySocketTcpClient(String host, int port) {
super(host, port);
}
public void input(byte[] buffer) {
System.out.println("Client: " + new String(buffer).trim());
}
}

View File

@@ -0,0 +1,17 @@
package test.server.dummy;
import java.io.IOException;
import java.nio.channels.SocketChannel;
import base.server.channel.TcpServer;
import base.server.channel.TcpServerClient;
public class DummyTcpServerClient extends TcpServerClient {
public DummyTcpServerClient(TcpServer server, SocketChannel socketChannel, Integer bufferSize) {
super(server, socketChannel, bufferSize);
}
public void input(byte[] buffer) {
server.input(this, buffer);
}
}

View File

@@ -0,0 +1,14 @@
package test.server.dummy;
import base.work.Listen;
public class DummyUdpListen extends Listen<byte[]> {
public DummyUdpListen() {
super();
}
public void input(byte[] buffer) {
String received = new String(buffer).trim();
System.out.println("Quote of the Moment: " + received);
}
}

View File

@@ -0,0 +1,13 @@
package test.server.dummy;
import base.server.datagram.UdpServer;
public class DummyUdpServer extends UdpServer {
public DummyUdpServer(int port) {
super(port);
}
protected void listen(byte[] buffer) {
logger.debug("Server: " + new String(buffer).trim());
}
}

View File

@@ -0,0 +1,32 @@
package test.server.dummy;
import java.io.IOException;
import base.sender.Sender;
import base.work.Work;
public class DummyWriter extends Work implements Sender {
private Sender sender;
public DummyWriter(Sender sender) {
this.sender = sender;
}
public void work() {
System.out.println("Client sending messages to server...");
String [] messages = new String[] {"Time goes fast.", "What now?", "Bye."};
try {
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i]);
send(new String(messages[i]).getBytes());
sleep(200);
}
stop();
} catch (Exception e) {}
}
public void send(byte[] buffer) throws IOException {
sender.send(buffer);
}
}

View File

@@ -0,0 +1,14 @@
package junit;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
TestTcpSocketCommunication.class,
TestTcpChannelCommunication.class
})
public class AllTests {}
// Should test start()/stop() of components, check implementation

View File

@@ -0,0 +1,94 @@
package junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import base.server.channel.TcpClient;
import base.server.channel.TcpServer;
import base.server.channel.TcpServerClient;
public class TestTcpChannelCommunication {
protected TestTcpServer server;
protected TestTcpClient client;
@Before
public void setUp() throws Exception {
server = new TestTcpServer(1234);
server.start();
client = new TestTcpClient(1234);
client.start();
}
@After
public void tearDown() throws Exception {
client.exit();
server.exit();
// Should add blocking stop and exit to worker
while (client.active() || server.active()) {
Thread.sleep(100);
}
}
@Test
public void testSendClientToServer() throws Exception {
String message = "test";
client.send(message.getBytes());
synchronized (server) {
server.wait(2000);
}
byte[] buffer = server.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
@Test
public void testSendServerToClient() throws Exception {
// If client can send, connection has been established
client.send("init".getBytes());
String message = "test";
server.send(message.getBytes());
synchronized (client) {
client.wait(2000);
}
byte[] buffer = client.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
class TestTcpServer extends TcpServer {
public byte[] buffer;
public TestTcpServer(int port) {
super(port);
}
public void input(TcpServerClient client, byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
class TestTcpClient extends TcpClient {
public byte[] buffer;
public TestTcpClient(int port) {
super(port);
}
protected void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
}

View File

@@ -0,0 +1,94 @@
package junit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import base.server.socket.TcpClient;
import base.server.socket.TcpServer;
import base.server.socket.TcpServerClient;
public class TestTcpSocketCommunication {
protected TestTcpServer server;
protected TestTcpClient client;
@Before
public void setUp() throws Exception {
server = new TestTcpServer(1234);
server.start();
client = new TestTcpClient(1234);
client.start();
}
@After
public void tearDown() throws Exception {
client.exit();
server.exit();
// Should add blocking stop and exit to worker
while (client.active() || server.active()) {
Thread.sleep(100);
}
}
@Test
public void testSendClientToServer() throws Exception {
String message = "test";
client.send(message.getBytes());
synchronized (server) {
server.wait(2000);
}
byte[] buffer = server.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
@Test
public void testSendServerToClient() throws Exception {
// If client can send, connection has been established
client.send("init".getBytes());
String message = "test";
server.send(message.getBytes());
synchronized (client) {
client.wait(2000);
}
byte[] buffer = client.buffer;
assertNotNull("Received input", buffer);
assertEquals("Message intact", message, new String(buffer).trim());
}
class TestTcpServer extends TcpServer {
public byte[] buffer;
public TestTcpServer(int port) {
super(port);
}
public void input(TcpServerClient client, byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
class TestTcpClient extends TcpClient {
public byte[] buffer;
public TestTcpClient(int port) {
super(port);
}
protected void input(byte[] buffer) {
this.buffer = buffer;
synchronized (this) {
notifyAll();
}
}
}
}

View File

@@ -0,0 +1,54 @@
package test;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
public class Test {
public static void main(String[] args) {
try {
new Test().start();
} catch (Throwable e) {
e.printStackTrace();
}
}
private void start() throws Throwable {
input((Object) new A());
input((Object) new B());
input((Object) new String[] {"a", "b"});
}
public void input(Object object) throws Throwable {
System.out.println("Object");
MethodType methodType = MethodType.methodType(void.class, object.getClass());
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle methodHandle = lookup.findVirtual(getClass(), "input", methodType);
try {
methodHandle.invoke(this, object);
} catch (NoSuchMethodException e) {
e.printStackTrace();
System.exit(1);
}
}
public void input(A object) {
System.out.println("A");
}
public void input(B object) {
System.out.println("B");
}
public void input(String[] object) {
System.out.println("String[]");
}
public class A {
}
public class B {
}
}

View File

@@ -4,7 +4,7 @@ import worker.dummy.DummyListen;
public class TestListen {
public static void main(String[] args) {
DummyListen listen = new DummyListen(0);
DummyListen<Integer> listen = new DummyListen<Integer>(0);
listen.start();
for (int i = 0; i < 10; ++i) {
listen.add(i);

View File

@@ -20,16 +20,16 @@ public class TestPooledListen {
public static void main(String[] args) {
ListenerPool<Integer> listenerPool = new ListenerPool<Integer>(5);
List<DummyListen> listenList = new ArrayList<DummyListen>();
List<DummyListen<Integer>> listenList = new ArrayList<DummyListen<Integer>>();
for (int i = 0; i < 20; ++i) {
DummyListen listen = new DummyListen(listenerPool, i + 1);
DummyListen<Integer> listen = new DummyListen<Integer>(listenerPool, i + 1);
listenList.add(listen);
}
listenerPool.start();
System.out.println("Starting to give out elements!");
for (int i = 0; i < 100; ++i) {
DummyListen randomListen = listenList.get((new Random()).nextInt(listenList.size()));
DummyListen<Integer> randomListen = listenList.get((new Random()).nextInt(listenList.size()));
randomListen.add(i);
}

View File

@@ -3,10 +3,10 @@ package worker.dummy;
import base.work.Listen;
import base.worker.pool.ListenerPool;
public class DummyListen extends Listen<Integer> {
public class DummyListen<T> extends Listen<T> {
protected int id;
public DummyListen(ListenerPool<Integer> listenerPool, int id) {
public DummyListen(ListenerPool<T> listenerPool, int id) {
super(listenerPool);
this.id = id;
}
@@ -19,4 +19,8 @@ public class DummyListen extends Listen<Integer> {
public void input(Integer input) {
System.out.println("#" + id + ", input = " + input);
}
public void input(byte[] input) {
System.out.println("#" + id + ", input = " + new String(input).trim());
}
}