diff --git a/src/main/java/com/github/nkzawa/socketio/client/Manager.java b/src/main/java/com/github/nkzawa/socketio/client/Manager.java index af0b321..e9dffdc 100644 --- a/src/main/java/com/github/nkzawa/socketio/client/Manager.java +++ b/src/main/java/com/github/nkzawa/socketio/client/Manager.java @@ -79,7 +79,7 @@ public class Manager extends Emitter { private List packetBuffer; private Queue subs; private IO.Options opts; - private com.github.nkzawa.engineio.client.Socket engine; + /*package*/ com.github.nkzawa.engineio.client.Socket engine; private Parser.Encoder encoder; private Parser.Decoder decoder; @@ -92,6 +92,14 @@ public class Manager extends Emitter { private ScheduledExecutorService reconnectScheduler = Executors.newSingleThreadScheduledExecutor(); + public Manager() { + this(null, null); + } + + public Manager(URI uri) { + this(uri, null); + } + public Manager(IO.Options opts) { this(null, opts); } diff --git a/src/main/java/com/github/nkzawa/socketio/client/Socket.java b/src/main/java/com/github/nkzawa/socketio/client/Socket.java index 97f41bf..7f98631 100644 --- a/src/main/java/com/github/nkzawa/socketio/client/Socket.java +++ b/src/main/java/com/github/nkzawa/socketio/client/Socket.java @@ -50,7 +50,7 @@ public class Socket extends Emitter { private boolean disconnected = true; private int ids; private String nsp; - private Manager io; + /*package*/ Manager io; private Map acks = new HashMap(); private Queue subs; private final Queue> buffer = new LinkedList>(); diff --git a/src/test/java/com/github/nkzawa/socketio/client/Connection.java b/src/test/java/com/github/nkzawa/socketio/client/Connection.java new file mode 100644 index 0000000..7a9f746 --- /dev/null +++ b/src/test/java/com/github/nkzawa/socketio/client/Connection.java @@ -0,0 +1,90 @@ +package com.github.nkzawa.socketio.client; + +import org.junit.After; +import org.junit.Before; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URISyntaxException; +import java.util.concurrent.*; + +public abstract class Connection { + + final static int TIMEOUT = 3000; + final static int PORT = 3000; + + private Process serverProcess; + private ExecutorService serverService; + private Future serverOutout; + private Future serverError; + + @Before + public void startServer() throws IOException, InterruptedException { + System.out.println("Starting server ..."); + + final CountDownLatch latch = new CountDownLatch(1); + serverProcess = Runtime.getRuntime().exec( + String.format("node src/test/resources/index.js %s %s", PORT, nsp()), + new String[] {"DEBUG=socket.io:*"}); + serverService = Executors.newCachedThreadPool(); + serverOutout = serverService.submit(new Runnable() { + @Override + public void run() { + BufferedReader reader = new BufferedReader( + new InputStreamReader(serverProcess.getInputStream())); + String line; + try { + line = reader.readLine(); + latch.countDown(); + do { + System.out.println("SERVER OUT: " + line); + } while ((line = reader.readLine()) != null); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + serverError = serverService.submit(new Runnable() { + @Override + public void run() { + BufferedReader reader = new BufferedReader( + new InputStreamReader(serverProcess.getErrorStream())); + String line; + try { + while ((line = reader.readLine()) != null) { + System.err.println("SERVER ERR: " + line); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + latch.await(3000, TimeUnit.MILLISECONDS); + } + + @After + public void stopServer() throws InterruptedException { + System.out.println("Stopping server ..."); + serverProcess.destroy(); + serverOutout.cancel(false); + serverError.cancel(false); + serverService.shutdown(); + serverService.awaitTermination(3000, TimeUnit.MILLISECONDS); + } + + protected Socket client() throws URISyntaxException { + IO.Options opts = new IO.Options(); + opts.forceNew = true; + opts.reconnection = false; + return IO.socket(uri() + nsp(), opts); + } + + protected String uri() { + return "http://localhost:" + PORT; + } + + protected String nsp() { + return "/"; + } +} diff --git a/src/test/java/com/github/nkzawa/socketio/client/ConnectionTest.java b/src/test/java/com/github/nkzawa/socketio/client/ConnectionTest.java new file mode 100644 index 0000000..e1f8c5d --- /dev/null +++ b/src/test/java/com/github/nkzawa/socketio/client/ConnectionTest.java @@ -0,0 +1,130 @@ +package com.github.nkzawa.socketio.client; + +import com.github.nkzawa.emitter.Emitter; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.concurrent.CountDownLatch; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(JUnit4.class) +public class ConnectionTest extends Connection { + + private Socket socket; + + @Test(timeout = TIMEOUT) + public void connectToLocalhost() throws URISyntaxException, InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + socket = client(); + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.emit("echo"); + socket.on("echoBack", new Emitter.Listener() { + @Override + public void call(Object... args) { + latch.countDown(); + } + }); + } + }); + socket.connect(); + latch.await(); + } + + @Test(timeout = TIMEOUT) + public void workWithAcks() throws URISyntaxException, InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + socket = client(); + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + JSONObject data = new JSONObject(); + data.put("test", true); + socket.emit("ack", 5, data, new Ack() { + @Override + public void call(Object... args) { + JSONObject data = (JSONObject)args[1]; + if ((Integer)args[0] == 5 && data.getBoolean("test")) { + latch.countDown(); + } + } + }); + } + }); + socket.connect(); + latch.await(); + } + + @Test(timeout = TIMEOUT) + public void workWithFalse() throws URISyntaxException, InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + socket = client(); + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.emit("echo", false); + socket.on("echoBack", new Emitter.Listener() { + @Override + public void call(Object... args) { + assertThat((Boolean)args[0], is(false)); + latch.countDown(); + } + }); + } + }); + socket.connect(); + latch.await(); + } + + @Test(timeout = TIMEOUT) + public void connectToNamespaceAfterConnectionEstablished() throws URISyntaxException, InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + final Manager manager = new Manager(new URI(uri())); + socket = manager.socket("/"); + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + final Socket foo = manager.socket("/foo"); + foo.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + foo.close(); + socket.close(); + latch.countDown(); + } + }); + foo.open(); + } + }); + socket.open(); + latch.await(); + } + + @Test(timeout = TIMEOUT) + public void reconnectByDefault() throws URISyntaxException, InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + socket = IO.socket(uri()); + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.io.engine.close(); + socket.io.on(Manager.EVENT_RECONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.close(); + latch.countDown(); + } + }); + } + }); + socket.open(); + latch.await(); + } +} diff --git a/src/test/java/com/github/nkzawa/socketio/client/ServerConnectionTest.java b/src/test/java/com/github/nkzawa/socketio/client/ServerConnectionTest.java index 91f8e55..a9110c6 100644 --- a/src/test/java/com/github/nkzawa/socketio/client/ServerConnectionTest.java +++ b/src/test/java/com/github/nkzawa/socketio/client/ServerConnectionTest.java @@ -2,88 +2,24 @@ package com.github.nkzawa.socketio.client; import com.github.nkzawa.emitter.Emitter; import org.json.JSONObject; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; import java.net.URISyntaxException; -import java.util.concurrent.*; +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.nullValue; import static org.junit.Assert.assertThat; @RunWith(JUnit4.class) -public class ServerConnectionTest { +public class ServerConnectionTest extends Connection { - final static int TIMEOUT = 3000; - final static int PORT = 3000; - - private Process serverProcess; - private ExecutorService serverService; - private Future serverOutout; - private Future serverError; private Socket socket; - @Before - public void startServer() throws IOException, InterruptedException { - System.out.println("Starting server ..."); - - final CountDownLatch latch = new CountDownLatch(1); - serverProcess = Runtime.getRuntime().exec( - String.format("node src/test/resources/index.js %s %s", PORT, nsp()), - new String[] {"DEBUG=socket.io:*"}); - serverService = Executors.newCachedThreadPool(); - serverOutout = serverService.submit(new Runnable() { - @Override - public void run() { - BufferedReader reader = new BufferedReader( - new InputStreamReader(serverProcess.getInputStream())); - String line; - try { - line = reader.readLine(); - latch.countDown(); - do { - System.out.println("SERVER OUT: " + line); - } while ((line = reader.readLine()) != null); - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - serverError = serverService.submit(new Runnable() { - @Override - public void run() { - BufferedReader reader = new BufferedReader( - new InputStreamReader(serverProcess.getErrorStream())); - String line; - try { - while ((line = reader.readLine()) != null) { - System.err.println("SERVER ERR: " + line); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - }); - latch.await(3000, TimeUnit.MILLISECONDS); - } - - @After - public void stopServer() throws InterruptedException { - System.out.println("Stopping server ..."); - serverProcess.destroy(); - serverOutout.cancel(false); - serverError.cancel(false); - serverService.shutdown(); - serverService.awaitTermination(3000, TimeUnit.MILLISECONDS); - } - @Test(timeout = TIMEOUT) public void openAndClose() throws URISyntaxException, InterruptedException { final BlockingQueue events = new LinkedBlockingQueue(); @@ -216,19 +152,4 @@ public class ServerConnectionTest { assertThat(events.take(), is(new Object[] {})); socket.disconnect(); } - - private Socket client() throws URISyntaxException { - IO.Options opts = new IO.Options(); - opts.forceNew = true; - opts.reconnection = false; - return IO.socket(uri(), opts); - } - - private String uri() { - return "http://localhost:" + PORT + nsp(); - } - - protected String nsp() { - return "/"; - } } diff --git a/src/test/resources/index.js b/src/test/resources/index.js index 12c3f59..949173b 100644 --- a/src/test/resources/index.js +++ b/src/test/resources/index.js @@ -30,7 +30,7 @@ io.of(nsp).on('connection', function(socket) { }); socket.on('error', function() { - console.log('error: ' + arguments); + console.log('error: ', arguments); }); });