Initial commit

This commit is contained in:
Naoyuki Kanezawa
2013-04-22 03:37:28 +09:00
commit 53f1f5b544
16 changed files with 1694 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
package com.github.nkzawa.engineio.client;
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 static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class SocketTest {
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(
"node src/test/resources/index.js " + PORT, new String[] {"DEBUG=engine*"});
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(true);
serverError.cancel(true);
serverService.shutdown();
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
}
@Test
public void openAndClose() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
socket = new Socket("ws://localhost:" + PORT) {
@Override
public void onopen() {
System.out.println("onopen:");
events.offer("onopen");
}
@Override
public void onmessage(String data) {}
@Override
public void onclose() {
System.out.println("onclose:");
events.offer("onclose");
}
}.open();
assertThat(events.take(), is("onopen"));
socket.close();
assertThat(events.take(), is("onclose"));
}
@Test
public void messages() throws URISyntaxException, InterruptedException {
final BlockingQueue<String> events = new LinkedBlockingQueue<String>();
socket = new Socket("ws://localhost:" + PORT) {
@Override
public void onopen() {
System.out.println("onopen:");
socket.send("hi");
}
@Override
public void onmessage(String data) {
System.out.println("onmessage: " + data);
events.offer(data);
}
@Override
public void onclose() {}
};
socket.open();
assertThat(events.take(), is("hello client"));
assertThat(events.take(), is("hi"));
socket.close();
}
}