101 lines
3.0 KiB
Java
101 lines
3.0 KiB
Java
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 serverOutput;
|
|
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/server.js %s", nsp()), createEnv());
|
|
serverService = Executors.newCachedThreadPool();
|
|
serverOutput = 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();
|
|
serverOutput.cancel(false);
|
|
serverError.cancel(false);
|
|
serverService.shutdown();
|
|
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
|
|
}
|
|
|
|
Socket client() throws URISyntaxException {
|
|
return client(createOptions());
|
|
}
|
|
|
|
Socket client(IO.Options opts) throws URISyntaxException {
|
|
return IO.socket(uri() + nsp(), opts);
|
|
}
|
|
|
|
String uri() {
|
|
return "http://localhost:" + PORT;
|
|
}
|
|
|
|
String nsp() {
|
|
return "/";
|
|
}
|
|
|
|
IO.Options createOptions() {
|
|
IO.Options opts = new IO.Options();
|
|
opts.forceNew = true;
|
|
return opts;
|
|
}
|
|
|
|
String[] createEnv() {
|
|
return new String[] {"DEBUG=socket.io:*", "PORT=" + PORT};
|
|
}
|
|
}
|