add tests

This commit is contained in:
Naoyuki Kanezawa
2014-04-10 02:35:14 +09:00
parent 527a3c640c
commit 9338615745
6 changed files with 235 additions and 86 deletions

View File

@@ -79,7 +79,7 @@ public class Manager extends Emitter {
private List<Packet> packetBuffer; private List<Packet> packetBuffer;
private Queue<On.Handle> subs; private Queue<On.Handle> subs;
private IO.Options opts; 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.Encoder encoder;
private Parser.Decoder decoder; private Parser.Decoder decoder;
@@ -92,6 +92,14 @@ public class Manager extends Emitter {
private ScheduledExecutorService reconnectScheduler = Executors.newSingleThreadScheduledExecutor(); private ScheduledExecutorService reconnectScheduler = Executors.newSingleThreadScheduledExecutor();
public Manager() {
this(null, null);
}
public Manager(URI uri) {
this(uri, null);
}
public Manager(IO.Options opts) { public Manager(IO.Options opts) {
this(null, opts); this(null, opts);
} }

View File

@@ -50,7 +50,7 @@ public class Socket extends Emitter {
private boolean disconnected = true; private boolean disconnected = true;
private int ids; private int ids;
private String nsp; private String nsp;
private Manager io; /*package*/ Manager io;
private Map<Integer, Ack> acks = new HashMap<Integer, Ack>(); private Map<Integer, Ack> acks = new HashMap<Integer, Ack>();
private Queue<On.Handle> subs; private Queue<On.Handle> subs;
private final Queue<List<Object>> buffer = new LinkedList<List<Object>>(); private final Queue<List<Object>> buffer = new LinkedList<List<Object>>();

View File

@@ -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 "/";
}
}

View File

@@ -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();
}
}

View File

@@ -2,88 +2,24 @@ package com.github.nkzawa.socketio.client;
import com.github.nkzawa.emitter.Emitter; import com.github.nkzawa.emitter.Emitter;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; 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.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException; 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.is;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class) @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; 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) @Test(timeout = TIMEOUT)
public void openAndClose() throws URISyntaxException, InterruptedException { public void openAndClose() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>(); final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
@@ -216,19 +152,4 @@ public class ServerConnectionTest {
assertThat(events.take(), is(new Object[] {})); assertThat(events.take(), is(new Object[] {}));
socket.disconnect(); 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 "/";
}
} }

View File

@@ -30,7 +30,7 @@ io.of(nsp).on('connection', function(socket) {
}); });
socket.on('error', function() { socket.on('error', function() {
console.log('error: ' + arguments); console.log('error: ', arguments);
}); });
}); });