Initial commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package dslab;
|
||||
|
||||
/**
|
||||
* CheckedConsumer.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface CheckedConsumer<T, E extends Exception> {
|
||||
void accept(T socket) throws E;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package dslab;
|
||||
|
||||
public interface Constants {
|
||||
|
||||
/**
|
||||
* Default time (in milliseconds) to wait after starting a component to test.
|
||||
*/
|
||||
long COMPONENT_STARTUP_WAIT = 3000;
|
||||
|
||||
/**
|
||||
* Default time (in milliseconds) to wait after shutting down a component to test.
|
||||
*/
|
||||
long COMPONENT_TEARDOWN_WAIT = 3000;
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package dslab;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
import org.junit.Assert;
|
||||
import org.junit.rules.ErrorCollector;
|
||||
|
||||
public class JunitSocketClient implements Closeable {
|
||||
|
||||
private ErrorCollector err;
|
||||
|
||||
private Socket socket;
|
||||
private PrintWriter writer;
|
||||
|
||||
private StreamListener listener;
|
||||
|
||||
/**
|
||||
* Creates a new Socket that connects to localhost on the given port and holds the I/O resources.
|
||||
*
|
||||
* @param port the port to connect to
|
||||
* @throws IOException if an I/O error occurred while connecting.
|
||||
*/
|
||||
public JunitSocketClient(int port) throws IOException {
|
||||
this(new Socket("127.0.0.1", port));
|
||||
}
|
||||
|
||||
public JunitSocketClient(Socket socket) throws IOException {
|
||||
this.socket = socket;
|
||||
this.writer = new PrintWriter(socket.getOutputStream());
|
||||
this.listener = new StreamListener(socket.getInputStream());
|
||||
|
||||
new Thread(listener).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Socket that connects to localhost on the given port and holds the I/O resources.
|
||||
*
|
||||
* @param port the port to connect to
|
||||
* @param err the error collector used to verify communication
|
||||
* @throws IOException if an I/O error occurred while connecting.
|
||||
*/
|
||||
|
||||
public JunitSocketClient(int port, ErrorCollector err) throws IOException {
|
||||
this(port);
|
||||
this.err = err;
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public PrintWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
public void send(String message) {
|
||||
writer.println(message);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
public String sendAndListen(String message) {
|
||||
send(message);
|
||||
return listen();
|
||||
}
|
||||
|
||||
public String listen() {
|
||||
return listen(1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public String listen(long time, TimeUnit timeUnit) {
|
||||
return listener.listen(time, timeUnit);
|
||||
}
|
||||
|
||||
public String read() throws IOException {
|
||||
return listener.poll(1, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a line from the input stream and verifies that it contains the given string.
|
||||
*
|
||||
* @param string the partial string to match
|
||||
* @throws IOException on read errors
|
||||
*/
|
||||
public void verify(String string) throws IOException {
|
||||
assertThat(read(), containsString(string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the given string to the output stream, and then behaves like {@link #verify(String)}.
|
||||
*
|
||||
* @param request the request to send
|
||||
* @param response the expected response (partial string match)
|
||||
* @throws IOException on I/O errors
|
||||
*/
|
||||
public void sendAndVerify(String request, String response) throws IOException {
|
||||
assertThat(sendAndRead(request), containsString(response));
|
||||
}
|
||||
|
||||
public String sendAndRead(String message) throws IOException {
|
||||
send(message);
|
||||
return read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closeQuietly(listener);
|
||||
closeQuietly(writer);
|
||||
closeQuietly(socket);
|
||||
}
|
||||
|
||||
private <T> void assertThat(T actual, Matcher<? super T> matcher) {
|
||||
if (err != null) {
|
||||
err.checkThat(actual, matcher);
|
||||
} else {
|
||||
Assert.assertThat(actual, matcher);
|
||||
}
|
||||
}
|
||||
|
||||
private void closeQuietly(Closeable closeable) {
|
||||
if (closeable != null) {
|
||||
try {
|
||||
closeable.close();
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package dslab;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
* This {@link OutputStream} has no destination (file/socket etc.) and all bytes written to it are ignored and lost.
|
||||
*/
|
||||
public final class NullOutputStream extends OutputStream {
|
||||
public static final OutputStream INSTANCE = new NullOutputStream();
|
||||
|
||||
private NullOutputStream() {
|
||||
}
|
||||
|
||||
public static OutputStream getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards the specified byte.
|
||||
*/
|
||||
@Override
|
||||
public void write(int b) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards the specified byte array.
|
||||
*/
|
||||
@Override
|
||||
public void write(byte[] b) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards the specified byte array.
|
||||
*/
|
||||
@Override
|
||||
public void write(byte[] b, int off, int len) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package dslab;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.BindException;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
/**
|
||||
* Util class for checking sockets.
|
||||
*/
|
||||
public final class Sockets {
|
||||
|
||||
private Sockets() {
|
||||
// util class
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits a given time for a TCP server socket to be opened at the given host and port by repeatedly (every 10 ms)
|
||||
* trying to connect to the address.
|
||||
*
|
||||
* @param host the expected server socket host
|
||||
* @param port the expected server socket port
|
||||
* @param ms the time in milliseconds.
|
||||
* @throws SocketTimeoutException if the timeout period was reached
|
||||
*/
|
||||
public static void waitForSocket(String host, int port, long ms) throws SocketTimeoutException {
|
||||
long interval = 10;
|
||||
|
||||
while (ms > 0) {
|
||||
try (Socket ignored = new Socket(host, port)) {
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
ms -= interval;
|
||||
|
||||
try {
|
||||
Thread.sleep(interval);
|
||||
} catch (InterruptedException e1) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new SocketTimeoutException("Gave up waiting for socket " + host + ":" + port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to open a DatagramSocket on the given port. If a BindException is thrown, it indicates that the socket was
|
||||
* open before, and the method will return true. If the socket was opened successfully, the method returns false and
|
||||
* immediately closes the socket.
|
||||
*
|
||||
* @param port the local port
|
||||
* @return true if the socket was already open
|
||||
* @throws SocketException if the socket could not be opened due to some other reason than a bind exception
|
||||
*/
|
||||
public static boolean isDatagramSocketOpen(int port) throws SocketException {
|
||||
try (DatagramSocket socket = new DatagramSocket(port)) {
|
||||
return false;
|
||||
} catch (BindException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to open a ServerSocket on the given port. If a BindException is thrown, it indicates that the socket was
|
||||
* open before, and the method will return true. If the socket was opened successfully, the method returns false and
|
||||
* immediately closes the socket.
|
||||
*
|
||||
* @param port the local port
|
||||
* @return true if the socket was already open
|
||||
* @throws IOException if the socket could not be opened due to some other reason than a bind exception
|
||||
*/
|
||||
public static boolean isServerSocketOpen(int port) throws IOException {
|
||||
try (ServerSocket socket = new ServerSocket(port)) {
|
||||
return false;
|
||||
} catch (BindException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package dslab;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.net.SocketException;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Runnable that reads from a Socket's InputStream line-by-line and writes it to a queue, until the socket is closed.
|
||||
*/
|
||||
public class StreamListener implements Runnable, Closeable {
|
||||
|
||||
public static final String NULL_CHAR = new String(new byte[]{0x0});
|
||||
|
||||
private InputStream in;
|
||||
private LinkedBlockingQueue<String> queue;
|
||||
|
||||
public StreamListener(InputStream in) {
|
||||
this.in = in;
|
||||
this.queue = new LinkedBlockingQueue<>();
|
||||
}
|
||||
|
||||
private static String removeNullBytes(String str) {
|
||||
return str.replace(NULL_CHAR, "");
|
||||
}
|
||||
|
||||
public String poll(long timeout, TimeUnit timeUnit) {
|
||||
try {
|
||||
return queue.poll(timeout, timeUnit);
|
||||
} catch (InterruptedException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String listen(long timeout, TimeUnit timeUnit) {
|
||||
StringBuilder str = new StringBuilder(128);
|
||||
|
||||
String line;
|
||||
while ((line = poll(timeout, timeUnit)) != null) {
|
||||
str.append(removeNullBytes(line)).append("\n");
|
||||
}
|
||||
|
||||
if (str.length() > 0) {
|
||||
// remove trailing whitespace
|
||||
int i = str.length() - 1;
|
||||
if ('\n' == str.charAt(i)) {
|
||||
str.deleteCharAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
|
||||
String line;
|
||||
try {
|
||||
while ((line = reader.readLine()) != null) {
|
||||
queue.offer(line);
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
// socket closed
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
in.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package dslab;
|
||||
|
||||
import org.hamcrest.Factory;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.core.SubstringMatcher;
|
||||
|
||||
/**
|
||||
* String matcher that checks for regex matching.
|
||||
*/
|
||||
public class StringMatches extends SubstringMatcher {
|
||||
|
||||
public StringMatches(String substring) {
|
||||
super(substring);
|
||||
}
|
||||
|
||||
@Factory
|
||||
public static Matcher<String> matchesPattern(String pattern) {
|
||||
return new StringMatches(pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean evalSubstringOf(String string) {
|
||||
return string.matches(substring);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String relationship() {
|
||||
return "matching";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package dslab;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.ErrorCollector;
|
||||
import org.junit.rules.Timeout;
|
||||
|
||||
/**
|
||||
* Contains a generic setup for a unit test.
|
||||
*/
|
||||
public class TestBase {
|
||||
|
||||
@Rule
|
||||
public ErrorCollector err = new ErrorCollector();
|
||||
|
||||
@Rule
|
||||
public Timeout timeout = new Timeout(30, TimeUnit.SECONDS); // fail tests that do not terminate after 30 seconds
|
||||
|
||||
protected TestInputStream in;
|
||||
protected TestOutputStream out;
|
||||
|
||||
@Before
|
||||
public void setUpBase() throws Exception {
|
||||
in = new TestInputStream();
|
||||
out = new TestOutputStream();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package dslab;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Simulates reading lines from an {@link InputStream}.
|
||||
*
|
||||
* Internally, the lines read from the underlying {@link InputStream} are buffered and can be retrieved on demand for
|
||||
* verification purposes.
|
||||
*/
|
||||
public class TestInputStream extends InputStream {
|
||||
private volatile BlockingQueue<String> lines = new LinkedBlockingQueue<>();
|
||||
private InputStream in;
|
||||
|
||||
@Override
|
||||
public synchronized int read() throws IOException {
|
||||
if (in == null) {
|
||||
if ((in = nextLine()) == null) {
|
||||
return -1;
|
||||
}
|
||||
} else if (in.available() <= 0) {
|
||||
try {
|
||||
return -1;
|
||||
} finally {
|
||||
in = null;
|
||||
}
|
||||
}
|
||||
return in != null ? in.read() : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the lines available for reading.
|
||||
*
|
||||
* @return the available lines
|
||||
* @throws IOException if the stream is closed
|
||||
*/
|
||||
public List<String> getLines() throws IOException {
|
||||
List<String> copy = new ArrayList<>();
|
||||
do {
|
||||
try {
|
||||
copy.add(lines.take());
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
} while (lines.size() > 0);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given line to the input queue.
|
||||
*
|
||||
* @param line the line to add
|
||||
*/
|
||||
public void addLine(String line) {
|
||||
if (lines != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the next line available for reading from it.
|
||||
* <p/>
|
||||
* This method blocks until a line is available or the stream becomes closed.
|
||||
*
|
||||
* @return the {@link InputStream} holding the line
|
||||
* @throws IOException if the stream is closed
|
||||
*/
|
||||
private InputStream nextLine() throws IOException {
|
||||
try {
|
||||
String line = null;
|
||||
while (lines != null && line == null) {
|
||||
line = lines.poll(500L, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
if (line != null) {
|
||||
return new ByteArrayInputStream((line.endsWith("\n") ? line : line + '\n').getBytes());
|
||||
} else {
|
||||
return new ByteArrayInputStream("".getBytes());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
if (in != System.in) {
|
||||
super.close();
|
||||
}
|
||||
lines = null;
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package dslab;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Simulates writing lines to an {@link PrintStream}.
|
||||
* <p/>
|
||||
* Internally, the lines written to the underlying {@link PrintStream} are buffered and can be retrieved on demand for
|
||||
* verification purposes.
|
||||
*/
|
||||
public class TestOutputStream extends PrintStream {
|
||||
private final LinkedBlockingQueue<String> lines = new LinkedBlockingQueue<>();
|
||||
private volatile StringBuilder line = new StringBuilder();
|
||||
private PrintStream delegate;
|
||||
|
||||
/**
|
||||
* Creates a new {@code TestOutputStream} instance writing to an {@link NullOutputStream}.
|
||||
*/
|
||||
public TestOutputStream() {
|
||||
this(new PrintStream(NullOutputStream.getInstance()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@code TestOutputStream} instance writing to the provided {@link PrintStream}.
|
||||
*
|
||||
* @param delegate the stream to write to
|
||||
*/
|
||||
public TestOutputStream(PrintStream delegate) {
|
||||
super(delegate);
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (delegate != System.out) {
|
||||
super.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(int b) {
|
||||
delegate.write(b);
|
||||
if (b == '\r') {
|
||||
// Do nothing
|
||||
} else if (b == '\n') {
|
||||
addLine();
|
||||
} else {
|
||||
line.append((char) b);
|
||||
}
|
||||
}
|
||||
|
||||
public void write(byte b[], int off, int len) {
|
||||
if (b == null) {
|
||||
throw new NullPointerException();
|
||||
} else if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else if (len == 0) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < len; i++) {
|
||||
write(b[off + i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the lines written to the {@link PrintStream} so far.
|
||||
*
|
||||
* @return the written lines
|
||||
*/
|
||||
public List<String> getLines() {
|
||||
synchronized (lines) {
|
||||
if (line.length() > 0) {
|
||||
addLine();
|
||||
}
|
||||
return new ArrayList<>(lines);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Listens for stream output until no output has been received for one second.
|
||||
*
|
||||
* @return the aggregated output (joined by a newline)
|
||||
* @throws InterruptedException if the polling was interrupted
|
||||
*/
|
||||
public String listen() throws InterruptedException {
|
||||
return listen(1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public String listen(long timeout, TimeUnit timeUnit) throws InterruptedException {
|
||||
StringBuilder str = new StringBuilder(128);
|
||||
|
||||
String line;
|
||||
while ((line = poll(timeout, timeUnit)) != null) {
|
||||
str.append(line).append("\n");
|
||||
}
|
||||
|
||||
if (str.length() > 0) {
|
||||
// remove trailing whitespace
|
||||
int i = str.length() - 1;
|
||||
if ('\n' == str.charAt(i)) {
|
||||
str.deleteCharAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
public String poll(long time, TimeUnit timeUnit) throws InterruptedException {
|
||||
return lines.poll(time, timeUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of the lines written to the {@link PrintStream} so far and clears the buffer.
|
||||
*
|
||||
* @return the written lines
|
||||
* @see #getLines()
|
||||
* @see #clear()
|
||||
*/
|
||||
public List<String> reset() {
|
||||
synchronized (lines) {
|
||||
List<String> lines = getLines();
|
||||
clear();
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the buffer holding the lines written to the {@link PrintStream} so far.
|
||||
*/
|
||||
private void clear() {
|
||||
synchronized (lines) {
|
||||
lines.clear();
|
||||
line = new StringBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the current line to the buffer.
|
||||
*/
|
||||
private void addLine() {
|
||||
synchronized (lines) {
|
||||
lines.add(line.toString());
|
||||
line = new StringBuilder();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package dslab.mailbox;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import dslab.ComponentFactory;
|
||||
import dslab.Constants;
|
||||
import dslab.JunitSocketClient;
|
||||
import dslab.Sockets;
|
||||
import dslab.TestBase;
|
||||
import dslab.util.Config;
|
||||
|
||||
public class MailboxServerProtocolTest extends TestBase {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(MailboxServerProtocolTest.class);
|
||||
|
||||
private String componentId = "mailbox-earth-planet";
|
||||
|
||||
private IMailboxServer component;
|
||||
private int dmapServerPort;
|
||||
private int dmtpServerPort;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
component = ComponentFactory.createMailboxServer(componentId, in, out);
|
||||
dmapServerPort = new Config(componentId).getInt("dmap.tcp.port");
|
||||
dmtpServerPort = new Config(componentId).getInt("dmtp.tcp.port");
|
||||
|
||||
new Thread(component).start();
|
||||
|
||||
LOG.info("Waiting for server sockets to appear");
|
||||
Sockets.waitForSocket("localhost", dmapServerPort, Constants.COMPONENT_STARTUP_WAIT);
|
||||
Sockets.waitForSocket("localhost", dmtpServerPort, Constants.COMPONENT_STARTUP_WAIT);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
in.addLine("shutdown"); // send "shutdown" command to command line
|
||||
Thread.sleep(Constants.COMPONENT_TEARDOWN_WAIT);
|
||||
}
|
||||
|
||||
@Test(timeout = 15000)
|
||||
public void loginAndLogout_withValidLogin() throws Exception {
|
||||
try (JunitSocketClient client = new JunitSocketClient(dmapServerPort, err)) {
|
||||
client.verify("ok DMAP");
|
||||
client.sendAndVerify("login trillian 12345", "ok");
|
||||
client.sendAndVerify("logout", "ok");
|
||||
client.sendAndVerify("quit", "ok bye");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 15000)
|
||||
public void login_withInvalidLogin_returnsError() throws Exception {
|
||||
try (JunitSocketClient client = new JunitSocketClient(dmapServerPort, err)) {
|
||||
client.verify("ok DMAP");
|
||||
client.sendAndVerify("login trillian WRONGPW", "error");
|
||||
client.sendAndVerify("quit", "ok bye");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 15000)
|
||||
public void acceptDmtpMessage_listDmapMessage() throws Exception {
|
||||
|
||||
// accept a message via DMTP (to trillian)
|
||||
try (JunitSocketClient client = new JunitSocketClient(dmtpServerPort, err)) {
|
||||
client.verify("ok DMTP");
|
||||
client.sendAndVerify("begin", "ok");
|
||||
client.sendAndVerify("from arthur@earth.planet", "ok");
|
||||
client.sendAndVerify("to trillian@earth.planet", "ok 1");
|
||||
client.sendAndVerify("subject hello", "ok");
|
||||
client.sendAndVerify("data hello from junit", "ok");
|
||||
client.sendAndVerify("send", "ok");
|
||||
client.sendAndVerify("quit", "ok bye");
|
||||
}
|
||||
|
||||
// list the message via DMAP list
|
||||
try (JunitSocketClient client = new JunitSocketClient(dmapServerPort, err)) {
|
||||
client.verify("ok DMAP");
|
||||
client.sendAndVerify("login trillian 12345", "ok");
|
||||
|
||||
client.send("list");
|
||||
String listResult = client.listen();
|
||||
err.checkThat(listResult, containsString("arthur@earth.planet hello"));
|
||||
|
||||
client.sendAndVerify("logout", "ok");
|
||||
client.sendAndVerify("quit", "ok bye");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 15000)
|
||||
public void dmtpMessage_withUnknownRecipient_returnsError() throws Exception {
|
||||
|
||||
// accept a message via DMTP (to trillian)
|
||||
try (JunitSocketClient client = new JunitSocketClient(dmtpServerPort, err)) {
|
||||
client.verify("ok DMTP");
|
||||
client.sendAndVerify("begin", "ok");
|
||||
client.sendAndVerify("from arthur@earth.planet", "ok");
|
||||
client.sendAndVerify("to unknown@earth.planet", "error unknown");
|
||||
client.sendAndVerify("quit", "ok bye");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package dslab.mailbox;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import dslab.ComponentFactory;
|
||||
import dslab.Constants;
|
||||
import dslab.Sockets;
|
||||
import dslab.TestBase;
|
||||
import dslab.monitoring.MonitoringServerTest;
|
||||
import dslab.util.Config;
|
||||
|
||||
/**
|
||||
* MailboxServerTest.
|
||||
*/
|
||||
public class MailboxServerTest extends TestBase {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(MonitoringServerTest.class);
|
||||
|
||||
@Test
|
||||
public void runAndShutdownTransferServer_createsAndStopsTcpSocketCorrectly() throws Exception {
|
||||
String componentId = "mailbox-earth-planet";
|
||||
Config config = new Config(componentId);
|
||||
|
||||
IMailboxServer component = ComponentFactory.createMailboxServer(componentId, in, out);
|
||||
int dmtpPort = config.getInt("dmtp.tcp.port");
|
||||
int dmapPort = config.getInt("dmap.tcp.port");
|
||||
|
||||
assertThat(component, is(notNullValue()));
|
||||
|
||||
Thread componentThread = new Thread(component);
|
||||
LOG.info("Starting thread with component " + component);
|
||||
componentThread.start();
|
||||
|
||||
try {
|
||||
LOG.info("Waiting for DMTP socket to open on port " + dmtpPort);
|
||||
Sockets.waitForSocket("localhost", dmtpPort, Constants.COMPONENT_STARTUP_WAIT);
|
||||
} catch (SocketTimeoutException e) {
|
||||
err.addError(new AssertionError("Expected a TCP server socket on port " + dmtpPort, e));
|
||||
}
|
||||
|
||||
try {
|
||||
LOG.info("Waiting for DMAP socket to open on port " + dmapPort);
|
||||
Sockets.waitForSocket("localhost", dmapPort, Constants.COMPONENT_STARTUP_WAIT);
|
||||
} catch (SocketTimeoutException e) {
|
||||
err.addError(new AssertionError("Expected a TCP server socket on port " + dmapPort, e));
|
||||
}
|
||||
|
||||
LOG.info("Shutting down component " + component);
|
||||
in.addLine("shutdown"); // send "shutdown" command to command line
|
||||
Thread.sleep(Constants.COMPONENT_TEARDOWN_WAIT);
|
||||
|
||||
try {
|
||||
LOG.info("Waiting for thread to stop for component " + component);
|
||||
componentThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
err.addError(new AssertionError("Monitoring server was not terminated correctly"));
|
||||
}
|
||||
|
||||
|
||||
err.checkThat("Expected tcp socket on port " + dmtpPort + " to be closed after shutdown",
|
||||
Sockets.isServerSocketOpen(dmtpPort), is(false));
|
||||
|
||||
err.checkThat("Expected tcp socket on port " + dmapPort + " to be closed after shutdown",
|
||||
Sockets.isServerSocketOpen(dmapPort), is(false));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package dslab.monitoring;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import dslab.ComponentFactory;
|
||||
import dslab.Constants;
|
||||
import dslab.TestBase;
|
||||
import dslab.util.Config;
|
||||
|
||||
/**
|
||||
* Tests whether the UDP-based monitoring protocol is implemented correctly on the server side.
|
||||
*/
|
||||
public class MonitoringServerProtocolTest extends TestBase {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(MonitoringServerProtocolTest.class);
|
||||
|
||||
private String componentId = "monitoring";
|
||||
|
||||
private IMonitoringServer component;
|
||||
private InetSocketAddress addr;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
component = ComponentFactory.createMonitoringServer(componentId, in, out);
|
||||
addr = new InetSocketAddress("127.0.0.1", new Config(componentId).getInt("udp.port"));
|
||||
|
||||
new Thread(component).start();
|
||||
Thread.sleep(Constants.COMPONENT_STARTUP_WAIT);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
in.addLine("shutdown");
|
||||
Thread.sleep(Constants.COMPONENT_TEARDOWN_WAIT);
|
||||
}
|
||||
|
||||
@Test(timeout = 15000)
|
||||
public void addresses_returnsCorrectStatistics() throws Exception {
|
||||
LOG.info("Sending three monitoring packets to monitoring socket");
|
||||
try (DatagramSocket socket = new DatagramSocket()) {
|
||||
String str1 = "127.0.0.1:42 foo@example.com";
|
||||
String str2 = "127.0.0.1:43 foo@example.com";
|
||||
String str3 = "127.0.0.1:42 bar@example.com";
|
||||
|
||||
socket.send(new DatagramPacket(str1.getBytes(), str1.length(), addr));
|
||||
socket.send(new DatagramPacket(str2.getBytes(), str2.length(), addr));
|
||||
socket.send(new DatagramPacket(str3.getBytes(), str3.length(), addr));
|
||||
}
|
||||
|
||||
Thread.sleep(2500);
|
||||
in.addLine("addresses"); // send "addresses" command to command line
|
||||
Thread.sleep(2500);
|
||||
String output = String.join(",", out.getLines());
|
||||
assertThat(output, containsString("foo@example.com 2"));
|
||||
assertThat(output, containsString("bar@example.com 1"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Assumes that run and shutdown works correctly.
|
||||
*/
|
||||
@Test(timeout = 15000)
|
||||
public void servers_returnsCorrectStatistics() throws Exception {
|
||||
LOG.info("Sending three monitoring packets to monitoring socket");
|
||||
try (DatagramSocket socket = new DatagramSocket()) {
|
||||
String str1 = "127.0.0.1:42 foo@example.com";
|
||||
String str2 = "127.0.0.1:43 foo@example.com";
|
||||
String str3 = "127.0.0.1:42 bar@example.com";
|
||||
|
||||
socket.send(new DatagramPacket(str1.getBytes(), str1.length(), addr));
|
||||
socket.send(new DatagramPacket(str2.getBytes(), str2.length(), addr));
|
||||
socket.send(new DatagramPacket(str3.getBytes(), str3.length(), addr));
|
||||
}
|
||||
|
||||
Thread.sleep(2500);
|
||||
in.addLine("servers"); // send "addresses" command to command line
|
||||
Thread.sleep(2500);
|
||||
String output = String.join(",", out.getLines());
|
||||
assertThat(output, containsString("127.0.0.1:42 2"));
|
||||
assertThat(output, containsString("127.0.0.1:43 1"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package dslab.monitoring;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import dslab.ComponentFactory;
|
||||
import dslab.Constants;
|
||||
import dslab.Sockets;
|
||||
import dslab.TestBase;
|
||||
import dslab.util.Config;
|
||||
|
||||
/**
|
||||
* Tests the creation, running, and shutting down of the monitoring server.
|
||||
*/
|
||||
public class MonitoringServerTest extends TestBase {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(MonitoringServerTest.class);
|
||||
|
||||
@Test
|
||||
public void runAndShutdownMonitoringServer_createsAndStopsUdpSocketCorrectly() throws Exception {
|
||||
IMonitoringServer component = ComponentFactory.createMonitoringServer("monitoring", in, out);
|
||||
int port = new Config("monitoring").getInt("udp.port");
|
||||
|
||||
assertThat(component, is(notNullValue()));
|
||||
|
||||
Thread componentThread = new Thread(component);
|
||||
LOG.info("Starting thread with component " + component);
|
||||
componentThread.start();
|
||||
|
||||
Thread.sleep(Constants.COMPONENT_STARTUP_WAIT); // wait a bit for resources to be initialized
|
||||
|
||||
try {
|
||||
LOG.info("Trying to create socket on port " + port);
|
||||
err.checkThat("Expected an open UDP socket on port " + port, Sockets.isDatagramSocketOpen(port), is(true));
|
||||
} catch (Exception e) {
|
||||
// a different unexpected error occurred (unlikely)
|
||||
err.addError(e);
|
||||
}
|
||||
|
||||
LOG.info("Shutting down component " + component);
|
||||
in.addLine("shutdown"); // send "shutdown" command to command line
|
||||
Thread.sleep(Constants.COMPONENT_TEARDOWN_WAIT);
|
||||
|
||||
try {
|
||||
LOG.info("Waiting for thread to stop for component " + component);
|
||||
componentThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
err.addError(new AssertionError("Monitoring server was not terminated correctly"));
|
||||
}
|
||||
|
||||
err.checkThat("Expected datagram socket on port " + port + " to be closed after shutdown",
|
||||
Sockets.isDatagramSocketOpen(port), is(false));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package dslab.transfer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import dslab.ComponentFactory;
|
||||
import dslab.Constants;
|
||||
import dslab.JunitSocketClient;
|
||||
import dslab.Sockets;
|
||||
import dslab.TestBase;
|
||||
import dslab.util.Config;
|
||||
|
||||
/**
|
||||
* TransferServerProtocolTest.
|
||||
*/
|
||||
public class TransferServerProtocolTest extends TestBase {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(TransferServerProtocolTest.class);
|
||||
|
||||
private String componentId = "transfer-1";
|
||||
|
||||
private ITransferServer component;
|
||||
private int serverPort;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
component = ComponentFactory.createTransferServer(componentId, in, out);
|
||||
serverPort = new Config(componentId).getInt("tcp.port");
|
||||
new Thread(component).start();
|
||||
|
||||
LOG.info("Waiting for server socket to appear");
|
||||
Sockets.waitForSocket("localhost", serverPort, Constants.COMPONENT_STARTUP_WAIT);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
in.addLine("shutdown"); // send "shutdown" command to command line
|
||||
Thread.sleep(Constants.COMPONENT_TEARDOWN_WAIT);
|
||||
}
|
||||
|
||||
@Test(timeout = 15000)
|
||||
public void defaultDmtpInteractionTest() throws Exception {
|
||||
try (JunitSocketClient client = new JunitSocketClient(serverPort, err)) {
|
||||
client.verify("ok DMTP");
|
||||
client.sendAndVerify("begin", "ok");
|
||||
client.sendAndVerify("from trillian@earth.planet", "ok");
|
||||
client.sendAndVerify("to arthur@earth.planet", "ok 1");
|
||||
client.sendAndVerify("subject hello", "ok");
|
||||
client.sendAndVerify("data hello from junit", "ok");
|
||||
client.sendAndVerify("send", "ok");
|
||||
client.sendAndVerify("quit", "ok bye");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(timeout = 15000)
|
||||
public void sendWithoutRecipient_returnsErrorOnSend() throws Exception {
|
||||
try (JunitSocketClient client = new JunitSocketClient(serverPort, err)) {
|
||||
client.verify("ok DMTP");
|
||||
client.sendAndVerify("begin", "ok");
|
||||
client.sendAndVerify("from trillian@earth.planet", "ok");
|
||||
client.sendAndVerify("subject hello", "ok");
|
||||
client.sendAndVerify("data hello from junit", "ok");
|
||||
client.sendAndVerify("send", "error");
|
||||
client.sendAndVerify("quit", "ok bye");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package dslab.transfer;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.net.SocketTimeoutException;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import dslab.ComponentFactory;
|
||||
import dslab.Constants;
|
||||
import dslab.Sockets;
|
||||
import dslab.TestBase;
|
||||
import dslab.monitoring.MonitoringServerTest;
|
||||
import dslab.util.Config;
|
||||
|
||||
/**
|
||||
* TransferServerTest.
|
||||
*/
|
||||
public class TransferServerTest extends TestBase {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(MonitoringServerTest.class);
|
||||
|
||||
@Test
|
||||
public void runAndShutdownTransferServer_createsAndStopsTcpSocketCorrectly() throws Exception {
|
||||
ITransferServer component = ComponentFactory.createTransferServer("transfer-1", in, out);
|
||||
int port = new Config("transfer-1").getInt("tcp.port");
|
||||
|
||||
assertThat(component, is(notNullValue()));
|
||||
|
||||
Thread componentThread = new Thread(component);
|
||||
LOG.info("Starting thread with component " + component);
|
||||
componentThread.start();
|
||||
|
||||
try {
|
||||
LOG.info("Waiting for socket to open on port " + port);
|
||||
Sockets.waitForSocket("localhost", port, Constants.COMPONENT_STARTUP_WAIT);
|
||||
} catch (SocketTimeoutException e) {
|
||||
err.addError(new AssertionError("Expected a TCP server socket on port " + port, e));
|
||||
}
|
||||
|
||||
LOG.info("Shutting down component " + component);
|
||||
in.addLine("shutdown"); // send "shutdown" command to command line
|
||||
Thread.sleep(Constants.COMPONENT_TEARDOWN_WAIT);
|
||||
|
||||
try {
|
||||
LOG.info("Waiting for thread to stop for component " + component);
|
||||
componentThread.join();
|
||||
} catch (InterruptedException e) {
|
||||
err.addError(new AssertionError("Monitoring server was not terminated correctly"));
|
||||
}
|
||||
|
||||
err.checkThat("Expected tcp socket on port " + port + " to be closed after shutdown",
|
||||
Sockets.isServerSocketOpen(port), is(false));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user