diff --git a/src/test/java/com/github/nkzawa/engineio/client/BinaryPollingTest.java b/src/test/java/com/github/nkzawa/engineio/client/BinaryPollingTest.java index e9735c1..96e8f6c 100644 --- a/src/test/java/com/github/nkzawa/engineio/client/BinaryPollingTest.java +++ b/src/test/java/com/github/nkzawa/engineio/client/BinaryPollingTest.java @@ -6,9 +6,9 @@ import org.junit.Test; import org.junit.runner.RunWith; 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.junit.Assert.assertThat; @@ -19,7 +19,8 @@ public class BinaryPollingTest extends Connection { @Test(timeout = TIMEOUT) public void receiveBinaryData() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); + final byte[] binaryData = new byte[5]; for (int i = 0; i < binaryData.length; i++) { binaryData[i] = (byte)i; @@ -38,21 +39,21 @@ public class BinaryPollingTest extends Connection { public void call(Object... args) { if ("hi".equals(args[0])) return; - assertThat(args[0], instanceOf(byte[].class)); - assertThat((byte[])args[0], is(binaryData)); - socket.close(); - semaphore.release(); + values.offer(args[0]); } }); } }); socket.open(); - semaphore.acquire(); + + assertThat((byte[])values.take(), is(binaryData)); + socket.close(); } @Test(timeout = TIMEOUT) public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); + final byte[] binaryData = new byte[5]; for (int i = 0; i < binaryData.length; i++) { binaryData[i] = (byte)i; @@ -73,21 +74,16 @@ public class BinaryPollingTest extends Connection { public void call(Object... args) { if ("hi".equals(args[0])) return; - if (msg[0] == 0) { - assertThat(args[0], instanceOf(byte[].class)); - 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(); - } + values.offer(args[0]); + msg[0]++; } }); } }); socket.open(); - semaphore.acquire(); + + assertThat((byte[])values.take(), is(binaryData)); + assertThat((String)values.take(), is("cash money €€€")); + socket.close(); } } diff --git a/src/test/java/com/github/nkzawa/engineio/client/BinaryWSTest.java b/src/test/java/com/github/nkzawa/engineio/client/BinaryWSTest.java index 22431a5..ca36d33 100644 --- a/src/test/java/com/github/nkzawa/engineio/client/BinaryWSTest.java +++ b/src/test/java/com/github/nkzawa/engineio/client/BinaryWSTest.java @@ -5,9 +5,9 @@ import org.junit.Test; import org.junit.runner.RunWith; 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.junit.Assert.assertThat; @@ -18,7 +18,8 @@ public class BinaryWSTest extends Connection { @Test(timeout = TIMEOUT) public void receiveBinaryData() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); + final byte[] binaryData = new byte[5]; for (int i = 0; i < binaryData.length; i++) { binaryData[i] = (byte)i; @@ -37,12 +38,7 @@ public class BinaryWSTest extends Connection { @Override public void call(Object... args) { if (args[0] instanceof String) return; - - assertThat(args[0], instanceOf(byte[].class)); - assertThat((byte[])args[0], is(binaryData)); - - socket.close(); - semaphore.release(); + values.offer(args[0]); } }); } @@ -50,12 +46,15 @@ public class BinaryWSTest extends Connection { } }); socket.open(); - semaphore.acquire(); + + assertThat((byte[])values.take(), is(binaryData)); + socket.close(); } @Test(timeout = TIMEOUT) public void receiveBinaryDataAndMultibyteUTF8String() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); + final byte[] binaryData = new byte[5]; for (int i = 0; i < binaryData.length; i++) { binaryData[i] = (byte)i; @@ -78,15 +77,8 @@ public class BinaryWSTest extends Connection { public void call(Object... args) { if ("hi".equals(args[0])) return; - if (msg[0] == 0) { - assertThat(args[0], instanceOf(byte[].class)); - assertThat((byte[])args[0], is(binaryData)); - msg[0]++; - } else { - assertThat((String)args[0], is("cash money €€€")); - socket.close(); - semaphore.release(); - } + values.offer(args[0]); + msg[0]++; } }); } @@ -94,6 +86,9 @@ public class BinaryWSTest extends Connection { } }); socket.open(); - semaphore.acquire(); + + assertThat((byte[])values.take(), is(binaryData)); + assertThat((String)values.take(), is("cash money €€€")); + socket.close(); } } diff --git a/src/test/java/com/github/nkzawa/engineio/client/ConnectionTest.java b/src/test/java/com/github/nkzawa/engineio/client/ConnectionTest.java index 1524849..391fff1 100644 --- a/src/test/java/com/github/nkzawa/engineio/client/ConnectionTest.java +++ b/src/test/java/com/github/nkzawa/engineio/client/ConnectionTest.java @@ -7,7 +7,8 @@ import org.junit.runners.JUnit4; import java.util.Timer; 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.junit.Assert.assertThat; @@ -19,7 +20,7 @@ public class ConnectionTest extends Connection { @Test(timeout = TIMEOUT) public void connectToLocalhost() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); socket = new Socket(createOptions()); 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() { @Override public void call(Object... args) { - assertThat((String)args[0], is("hi")); + values.offer(args[0]); socket.close(); - semaphore.release(); } }); } }); socket.open(); - semaphore.acquire(); + + assertThat((String)values.take(), is("hi")); } @Test(timeout = TIMEOUT) public void receiveMultibyteUTF8StringsWithPolling() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); socket = new Socket(createOptions()); socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { @@ -52,20 +53,20 @@ public class ConnectionTest extends Connection { @Override public void call(Object... args) { if ("hi".equals(args[0])) return; - assertThat((String)args[0], is("cash money €€€")); + values.offer(args[0]); socket.close(); - semaphore.release(); } }); } }); socket.open(); - semaphore.acquire(); + + assertThat((String)values.take(), is("cash money €€€")); } @Test(timeout = TIMEOUT) public void receiveEmoji() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); socket = new Socket(createOptions()); socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { @@ -76,20 +77,20 @@ public class ConnectionTest extends Connection { @Override public void call(Object... args) { 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(); - semaphore.release(); } }); } }); socket.open(); - semaphore.acquire(); + + assertThat((String)values.take(), is("\uD800-\uDB7F\uDB80-\uDBFF\uDC00-\uDFFF\uE000-\uF8FF")); } @Test(timeout = TIMEOUT) public void notSendPacketsIfSocketCloses() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); socket = new Socket(createOptions()); socket.on(Socket.EVENT_OPEN, new Emitter.Listener() { @@ -107,14 +108,13 @@ public class ConnectionTest extends Connection { timer.schedule(new TimerTask() { @Override public void run() { - assertThat(noPacket[0], is(true)); - semaphore.release(); + values.offer(noPacket[0]); } }, 1200); } }); socket.open(); - semaphore.acquire(); + assertThat((Boolean)values.take(), is(true)); } } diff --git a/src/test/java/com/github/nkzawa/engineio/client/SSLConnectionTest.java b/src/test/java/com/github/nkzawa/engineio/client/SSLConnectionTest.java index c07255e..c4c0e5c 100644 --- a/src/test/java/com/github/nkzawa/engineio/client/SSLConnectionTest.java +++ b/src/test/java/com/github/nkzawa/engineio/client/SSLConnectionTest.java @@ -14,7 +14,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.security.GeneralSecurityException; 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.junit.Assert.assertThat; @@ -69,7 +70,7 @@ public class SSLConnectionTest extends Connection { @Test(timeout = TIMEOUT) public void connect() throws Exception { - final CountDownLatch latch = new CountDownLatch(1); + final BlockingQueue values = new LinkedBlockingQueue(); Socket.Options opts = createOptions(); opts.sslContext = createSSLContext(); @@ -80,20 +81,20 @@ public class SSLConnectionTest extends Connection { socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { @Override public void call(Object... args) { - assertThat((String) args[0], is("hi")); - socket.close(); - latch.countDown(); + values.offer(args[0]); } }); } }); socket.open(); - latch.await(); + + assertThat((String)values.take(), is("hi")); + socket.close(); } @Test(timeout = TIMEOUT) public void upgrade() throws Exception { - final CountDownLatch latch = new CountDownLatch(1); + final BlockingQueue values = new LinkedBlockingQueue(); Socket.Options opts = createOptions(); opts.sslContext = createSSLContext(); @@ -108,9 +109,7 @@ public class SSLConnectionTest extends Connection { socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { @Override public void call(Object... args) { - assertThat((String) args[0], is("hi")); - socket.close(); - latch.countDown(); + values.offer(args[0]); } }); } @@ -118,12 +117,14 @@ public class SSLConnectionTest extends Connection { } }); socket.open(); - latch.await(); + + assertThat((String)values.take(), is("hi")); + socket.close(); } @Test(timeout = TIMEOUT) public void defaultSSLContext() throws Exception { - final CountDownLatch latch = new CountDownLatch(1); + final BlockingQueue values = new LinkedBlockingQueue(); Socket.setDefaultSSLContext(createSSLContext()); socket = new Socket(createOptions()); @@ -133,14 +134,14 @@ public class SSLConnectionTest extends Connection { socket.on(Socket.EVENT_MESSAGE, new Emitter.Listener() { @Override public void call(Object... args) { - assertThat((String) args[0], is("hi")); - socket.close(); - latch.countDown(); + values.offer(args[0]); } }); } }); socket.open(); - latch.await(); + + assertThat((String)values.take(), is("hi")); + socket.close(); } } diff --git a/src/test/java/com/github/nkzawa/engineio/client/ServerConnectionTest.java b/src/test/java/com/github/nkzawa/engineio/client/ServerConnectionTest.java index f2b1be7..da7b6cc 100644 --- a/src/test/java/com/github/nkzawa/engineio/client/ServerConnectionTest.java +++ b/src/test/java/com/github/nkzawa/engineio/client/ServerConnectionTest.java @@ -12,7 +12,6 @@ import java.net.URISyntaxException; import java.util.Map; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.Semaphore; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; @@ -74,28 +73,25 @@ public class ServerConnectionTest extends Connection { @Test(timeout = TIMEOUT) public void handshake() throws URISyntaxException, InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); socket = new Socket(createOptions()); socket.on(Socket.EVENT_HANDSHAKE, new Emitter.Listener() { @Override public void call(Object... args) { - assertThat(args.length, is(1)); - 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(); + values.offer(args); } }); 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) @@ -205,7 +201,7 @@ public class ServerConnectionTest extends Connection { @Test(timeout = TIMEOUT) public void rememberWebsocket() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); EventThread.exec(new Runnable() { @Override @@ -222,23 +218,25 @@ public class ServerConnectionTest extends Connection { opts.port = PORT; opts.rememberUpgrade = true; - final Socket socket2 = new Socket(opts); + Socket socket2 = new Socket(opts); socket2.open(); - assertThat(socket2.transport.name, is(WebSocket.NAME)); + values.offer(socket2.transport.name); + socket2.close(); } - semaphore.release(); } }); 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) public void notRememberWebsocket() throws InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); EventThread.exec(new Runnable() { @Override @@ -257,15 +255,17 @@ public class ServerConnectionTest extends Connection { final Socket socket2 = new Socket(opts); socket2.open(); - assertThat(socket2.transport.name, is(not(WebSocket.NAME))); + values.offer(socket2.transport.name); + socket2.close(); } - semaphore.release(); } }); 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))); } } diff --git a/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java b/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java index 99c8760..f231dae 100644 --- a/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java +++ b/src/test/java/com/github/nkzawa/engineio/client/SocketTest.java @@ -12,7 +12,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Timer; 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.junit.Assert.assertThat; @@ -41,7 +42,8 @@ public class SocketTest { */ @Test public void socketClosing() throws URISyntaxException, InterruptedException { - final Semaphore semaphore = new Semaphore(0); + final BlockingQueue values = new LinkedBlockingQueue(); + Socket socket = new Socket("ws://0.0.0.0:8080"); final boolean[] closed = {false}; @@ -52,8 +54,7 @@ public class SocketTest { timer.schedule(new TimerTask() { @Override public void run() { - assertThat(closed[0], is(true)); - semaphore.release(); + values.offer(closed[0]); } }, 20); } @@ -66,7 +67,8 @@ public class SocketTest { } }); socket.open(); - semaphore.acquire(); + + assertThat((Boolean)values.take(), is(true)); } }