fix tests

This commit is contained in:
Naoyuki Kanezawa
2014-08-17 14:01:14 +09:00
parent 9e24f9d922
commit 1f8edef37f
6 changed files with 99 additions and 105 deletions

View File

@@ -6,9 +6,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
import java.util.concurrent.Semaphore; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -19,7 +19,8 @@ public class BinaryPollingTest extends Connection {
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void receiveBinaryData() throws InterruptedException { public void receiveBinaryData() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5]; final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) { for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i; binaryData[i] = (byte)i;
@@ -38,21 +39,21 @@ public class BinaryPollingTest extends Connection {
public void call(Object... args) { public void call(Object... args) {
if ("hi".equals(args[0])) return; if ("hi".equals(args[0])) return;
assertThat(args[0], instanceOf(byte[].class)); values.offer(args[0]);
assertThat((byte[])args[0], is(binaryData));
socket.close();
semaphore.release();
} }
}); });
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((byte[])values.take(), is(binaryData));
socket.close();
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException { public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5]; final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) { for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i; binaryData[i] = (byte)i;
@@ -73,21 +74,16 @@ public class BinaryPollingTest extends Connection {
public void call(Object... args) { public void call(Object... args) {
if ("hi".equals(args[0])) return; if ("hi".equals(args[0])) return;
if (msg[0] == 0) { values.offer(args[0]);
assertThat(args[0], instanceOf(byte[].class)); msg[0]++;
assertThat((byte[])args[0], is(binaryData));
msg[0]++;
} else {
assertThat(args[0], instanceOf(String.class));
assertThat((String)args[0], is("cash money €€€"));
socket.close();
semaphore.release();
}
} }
}); });
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((byte[])values.take(), is(binaryData));
assertThat((String)values.take(), is("cash money €€€"));
socket.close();
} }
} }

View File

@@ -5,9 +5,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
import java.util.concurrent.Semaphore; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -18,7 +18,8 @@ public class BinaryWSTest extends Connection {
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void receiveBinaryData() throws InterruptedException { public void receiveBinaryData() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5]; final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) { for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i; binaryData[i] = (byte)i;
@@ -37,12 +38,7 @@ public class BinaryWSTest extends Connection {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
if (args[0] instanceof String) return; if (args[0] instanceof String) return;
values.offer(args[0]);
assertThat(args[0], instanceOf(byte[].class));
assertThat((byte[])args[0], is(binaryData));
socket.close();
semaphore.release();
} }
}); });
} }
@@ -50,12 +46,15 @@ public class BinaryWSTest extends Connection {
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((byte[])values.take(), is(binaryData));
socket.close();
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException { public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final byte[] binaryData = new byte[5]; final byte[] binaryData = new byte[5];
for (int i = 0; i < binaryData.length; i++) { for (int i = 0; i < binaryData.length; i++) {
binaryData[i] = (byte)i; binaryData[i] = (byte)i;
@@ -78,15 +77,8 @@ public class BinaryWSTest extends Connection {
public void call(Object... args) { public void call(Object... args) {
if ("hi".equals(args[0])) return; if ("hi".equals(args[0])) return;
if (msg[0] == 0) { values.offer(args[0]);
assertThat(args[0], instanceOf(byte[].class)); msg[0]++;
assertThat((byte[])args[0], is(binaryData));
msg[0]++;
} else {
assertThat((String)args[0], is("cash money €€€"));
socket.close();
semaphore.release();
}
} }
}); });
} }
@@ -94,6 +86,9 @@ public class BinaryWSTest extends Connection {
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((byte[])values.take(), is(binaryData));
assertThat((String)values.take(), is("cash money €€€"));
socket.close();
} }
} }

View File

@@ -7,7 +7,8 @@ import org.junit.runners.JUnit4;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.Semaphore; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -19,7 +20,7 @@ public class ConnectionTest extends Connection {
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void connectToLocalhost() throws InterruptedException { public void connectToLocalhost() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions()); socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@@ -28,20 +29,20 @@ public class ConnectionTest extends Connection {
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
assertThat((String)args[0], is("hi")); values.offer(args[0]);
socket.close(); socket.close();
semaphore.release();
} }
}); });
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((String)values.take(), is("hi"));
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void receiveMultibyteUTF8StringsWithPolling() throws InterruptedException { public void receiveMultibyteUTF8StringsWithPolling() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions()); socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@@ -52,20 +53,20 @@ public class ConnectionTest extends Connection {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
if ("hi".equals(args[0])) return; if ("hi".equals(args[0])) return;
assertThat((String)args[0], is("cash money €€€")); values.offer(args[0]);
socket.close(); socket.close();
semaphore.release();
} }
}); });
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((String)values.take(), is("cash money €€€"));
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void receiveEmoji() throws InterruptedException { public void receiveEmoji() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions()); socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@@ -76,20 +77,20 @@ public class ConnectionTest extends Connection {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
if ("hi".equals(args[0])) return; if ("hi".equals(args[0])) return;
assertThat((String)args[0], is("\uD800-\uDB7F\uDB80-\uDBFF\uDC00-\uDFFF\uE000-\uF8FF")); values.offer(args[0]);
socket.close(); socket.close();
semaphore.release();
} }
}); });
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((String)values.take(), is("\uD800-\uDB7F\uDB80-\uDBFF\uDC00-\uDFFF\uE000-\uF8FF"));
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void notSendPacketsIfSocketCloses() throws InterruptedException { public void notSendPacketsIfSocketCloses() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions()); socket = new Socket(createOptions());
socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { socket.on(Socket.EVENT_OPEN, new Emitter.Listener() {
@@ -107,14 +108,13 @@ public class ConnectionTest extends Connection {
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
assertThat(noPacket[0], is(true)); values.offer(noPacket[0]);
semaphore.release();
} }
}, 1200); }, 1200);
} }
}); });
socket.open(); socket.open();
semaphore.acquire(); assertThat((Boolean)values.take(), is(true));
} }
} }

View File

@@ -14,7 +14,8 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.KeyStore; import java.security.KeyStore;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -69,7 +70,7 @@ public class SSLConnectionTest extends Connection {
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void connect() throws Exception { public void connect() throws Exception {
final CountDownLatch latch = new CountDownLatch(1); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket.Options opts = createOptions(); Socket.Options opts = createOptions();
opts.sslContext = createSSLContext(); opts.sslContext = createSSLContext();
@@ -80,20 +81,20 @@ public class SSLConnectionTest extends Connection {
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
assertThat((String) args[0], is("hi")); values.offer(args[0]);
socket.close();
latch.countDown();
} }
}); });
} }
}); });
socket.open(); socket.open();
latch.await();
assertThat((String)values.take(), is("hi"));
socket.close();
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void upgrade() throws Exception { public void upgrade() throws Exception {
final CountDownLatch latch = new CountDownLatch(1); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket.Options opts = createOptions(); Socket.Options opts = createOptions();
opts.sslContext = createSSLContext(); opts.sslContext = createSSLContext();
@@ -108,9 +109,7 @@ public class SSLConnectionTest extends Connection {
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
assertThat((String) args[0], is("hi")); values.offer(args[0]);
socket.close();
latch.countDown();
} }
}); });
} }
@@ -118,12 +117,14 @@ public class SSLConnectionTest extends Connection {
} }
}); });
socket.open(); socket.open();
latch.await();
assertThat((String)values.take(), is("hi"));
socket.close();
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void defaultSSLContext() throws Exception { public void defaultSSLContext() throws Exception {
final CountDownLatch latch = new CountDownLatch(1); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket.setDefaultSSLContext(createSSLContext()); Socket.setDefaultSSLContext(createSSLContext());
socket = new Socket(createOptions()); socket = new Socket(createOptions());
@@ -133,14 +134,14 @@ public class SSLConnectionTest extends Connection {
socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
assertThat((String) args[0], is("hi")); values.offer(args[0]);
socket.close();
latch.countDown();
} }
}); });
} }
}); });
socket.open(); socket.open();
latch.await();
assertThat((String)values.take(), is("hi"));
socket.close();
} }
} }

View File

@@ -12,7 +12,6 @@ import java.net.URISyntaxException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
@@ -74,28 +73,25 @@ public class ServerConnectionTest extends Connection {
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void handshake() throws URISyntaxException, InterruptedException { public void handshake() throws URISyntaxException, InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = new Socket(createOptions()); socket = new Socket(createOptions());
socket.on(Socket.EVENT_HANDSHAKE, new Emitter.Listener() { socket.on(Socket.EVENT_HANDSHAKE, new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
assertThat(args.length, is(1)); values.offer(args);
assertThat(args[0], is(instanceOf(HandshakeData.class)));
HandshakeData data = (HandshakeData)args[0];
assertThat(data.sid, is(notNullValue()));
assertThat(data.upgrades, is(notNullValue()));
assertThat(data.upgrades, is(not(emptyArray())));
assertThat(data.pingTimeout, is(greaterThan((long)0)));
assertThat(data.pingInterval, is(greaterThan((long) 0)));
socket.close();
semaphore.release();
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
Object[] args = (Object[])values.take();
assertThat(args.length, is(1));
HandshakeData data = (HandshakeData)args[0];
assertThat(data.sid, is(notNullValue()));
assertThat(data.upgrades, is(not(emptyArray())));
assertThat(data.pingTimeout, is(greaterThan((long)0)));
assertThat(data.pingInterval, is(greaterThan((long) 0)));
socket.close();
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
@@ -205,7 +201,7 @@ public class ServerConnectionTest extends Connection {
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void rememberWebsocket() throws InterruptedException { public void rememberWebsocket() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
EventThread.exec(new Runnable() { EventThread.exec(new Runnable() {
@Override @Override
@@ -222,23 +218,25 @@ public class ServerConnectionTest extends Connection {
opts.port = PORT; opts.port = PORT;
opts.rememberUpgrade = true; opts.rememberUpgrade = true;
final Socket socket2 = new Socket(opts); Socket socket2 = new Socket(opts);
socket2.open(); socket2.open();
assertThat(socket2.transport.name, is(WebSocket.NAME)); values.offer(socket2.transport.name);
socket2.close();
} }
semaphore.release();
} }
}); });
socket.open(); socket.open();
assertThat(socket.transport.name, is(Polling.NAME)); values.offer(socket.transport.name);
} }
}); });
semaphore.acquire();
assertThat((String)values.take(), is(Polling.NAME));
assertThat((String)values.take(), is(WebSocket.NAME));
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void notRememberWebsocket() throws InterruptedException { public void notRememberWebsocket() throws InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
EventThread.exec(new Runnable() { EventThread.exec(new Runnable() {
@Override @Override
@@ -257,15 +255,17 @@ public class ServerConnectionTest extends Connection {
final Socket socket2 = new Socket(opts); final Socket socket2 = new Socket(opts);
socket2.open(); socket2.open();
assertThat(socket2.transport.name, is(not(WebSocket.NAME))); values.offer(socket2.transport.name);
socket2.close();
} }
semaphore.release();
} }
}); });
socket.open(); socket.open();
assertThat(socket.transport.name, is(Polling.NAME)); values.offer(socket.transport.name);
} }
}); });
semaphore.acquire();
assertThat((String)values.take(), is(Polling.NAME));
assertThat((String)values.take(), is(not(WebSocket.NAME)));
} }
} }

View File

@@ -12,7 +12,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.concurrent.Semaphore; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@@ -41,7 +42,8 @@ public class SocketTest {
*/ */
@Test @Test
public void socketClosing() throws URISyntaxException, InterruptedException { public void socketClosing() throws URISyntaxException, InterruptedException {
final Semaphore semaphore = new Semaphore(0); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
Socket socket = new Socket("ws://0.0.0.0:8080"); Socket socket = new Socket("ws://0.0.0.0:8080");
final boolean[] closed = {false}; final boolean[] closed = {false};
@@ -52,8 +54,7 @@ public class SocketTest {
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
assertThat(closed[0], is(true)); values.offer(closed[0]);
semaphore.release();
} }
}, 20); }, 20);
} }
@@ -66,7 +67,8 @@ public class SocketTest {
} }
}); });
socket.open(); socket.open();
semaphore.acquire();
assertThat((Boolean)values.take(), is(true));
} }
} }