fix termination #85
This commit is contained in:
@@ -7,6 +7,8 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public abstract class Connection {
|
||||
@@ -95,6 +97,16 @@ public abstract class Connection {
|
||||
}
|
||||
|
||||
String[] createEnv() {
|
||||
return new String[] {"DEBUG=socket.io:*", "PORT=" + PORT};
|
||||
Map<String, String> env = new HashMap<String, String>(System.getenv());
|
||||
env.put("DEBUG", "socket.io:*");
|
||||
env.put("PORT", String.valueOf(PORT));
|
||||
String[] _env = new String[env.size()];
|
||||
int i = 0;
|
||||
for (String key : env.keySet()) {
|
||||
_env[i] = key + "=" + env.get(key);
|
||||
i++;
|
||||
}
|
||||
return _env;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.github.nkzawa.socketio.client;
|
||||
|
||||
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 static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class ExecutionTest extends Connection {
|
||||
|
||||
final static int TIMEOUT = 30 * 1000;
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void execConnection() throws InterruptedException, IOException {
|
||||
exec("com.github.nkzawa.socketio.client.executions.Connection");
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void execConnectionFailure() throws InterruptedException, IOException {
|
||||
exec("com.github.nkzawa.socketio.client.executions.ConnectionFailure");
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void execImmediateClose() throws InterruptedException, IOException {
|
||||
exec("com.github.nkzawa.socketio.client.executions.ImmediateClose");
|
||||
}
|
||||
|
||||
private void exec(String mainClass) throws InterruptedException, IOException {
|
||||
Process process = Runtime.getRuntime().exec(String.format("mvn --quiet exec:java" +
|
||||
" -Dexec.mainClass=%s -Dexec.classpathScope=test", mainClass), createEnv());
|
||||
BufferedReader input = new BufferedReader(
|
||||
new InputStreamReader(process.getInputStream()));
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
System.out.println("EXEC OUT: " + line);
|
||||
}
|
||||
process.waitFor();
|
||||
assertThat(process.exitValue(), is(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.github.nkzawa.socketio.client.executions;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import com.github.nkzawa.socketio.client.IO;
|
||||
import com.github.nkzawa.socketio.client.Socket;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class Connection {
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
IO.Options options = new IO.Options();
|
||||
options.forceNew = true;
|
||||
final Socket socket = IO.socket("http://localhost:" + System.getenv("PORT"), options);
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("connect");
|
||||
socket.close();
|
||||
}
|
||||
});
|
||||
socket.open();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.github.nkzawa.socketio.client.executions;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import com.github.nkzawa.socketio.client.IO;
|
||||
import com.github.nkzawa.socketio.client.Socket;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class ConnectionFailure {
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
int port = Integer.parseInt(System.getenv("PORT"));
|
||||
port++;
|
||||
IO.Options options = new IO.Options();
|
||||
options.forceNew = true;
|
||||
options.reconnection = false;
|
||||
final Socket socket = IO.socket("http://localhost:" + port, options);
|
||||
socket.on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("connect timeout");
|
||||
}
|
||||
}).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("connect error");
|
||||
}
|
||||
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("disconnect");
|
||||
}
|
||||
});
|
||||
socket.open();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.github.nkzawa.socketio.client.executions;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import com.github.nkzawa.socketio.client.IO;
|
||||
import com.github.nkzawa.socketio.client.Socket;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class ImmediateClose {
|
||||
|
||||
public static void main(String[] args) throws URISyntaxException {
|
||||
IO.Options options = new IO.Options();
|
||||
options.forceNew = true;
|
||||
final Socket socket = IO.socket("http://localhost:" + System.getenv("PORT"), options);
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("connect");
|
||||
}
|
||||
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("disconnect");
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
socket.disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user