add a test for the transport option
This commit is contained in:
@@ -31,9 +31,6 @@ public abstract class Socket extends Emitter {
|
|||||||
put(CLOSED, "closed");
|
put(CLOSED, "closed");
|
||||||
}};
|
}};
|
||||||
|
|
||||||
public static final String POLLING = "polling";
|
|
||||||
public static final String WEBSOCKET = "websocket";
|
|
||||||
|
|
||||||
public static final String EVENT_OPEN = "open";
|
public static final String EVENT_OPEN = "open";
|
||||||
public static final String EVENT_CLOSE = "close";
|
public static final String EVENT_CLOSE = "close";
|
||||||
public static final String EVENT_HANDSHAKE = "handshake";
|
public static final String EVENT_HANDSHAKE = "handshake";
|
||||||
@@ -116,8 +113,8 @@ public abstract class Socket extends Emitter {
|
|||||||
this.path = (opts.path != null ? opts.path : "/engine.io").replaceAll("/$", "") + "/";
|
this.path = (opts.path != null ? opts.path : "/engine.io").replaceAll("/$", "") + "/";
|
||||||
this.timestampParam = opts.timestampParam != null ? opts.timestampParam : "t";
|
this.timestampParam = opts.timestampParam != null ? opts.timestampParam : "t";
|
||||||
this.timestampRequests = opts.timestampRequests;
|
this.timestampRequests = opts.timestampRequests;
|
||||||
this.transports = new ArrayList<String>(Arrays.asList(
|
this.transports = new ArrayList<String>(Arrays.asList(opts.transports != null ?
|
||||||
opts.transports != null ? opts.transports : new String[] {POLLING, WEBSOCKET}));
|
opts.transports : new String[] {Polling.NAME, WebSocket.NAME}));
|
||||||
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
||||||
|
|
||||||
Socket.sockets.add(this);
|
Socket.sockets.add(this);
|
||||||
@@ -151,9 +148,9 @@ public abstract class Socket extends Emitter {
|
|||||||
opts.timestampParam = this.timestampParam;
|
opts.timestampParam = this.timestampParam;
|
||||||
opts.policyPort = this.policyPort;
|
opts.policyPort = this.policyPort;
|
||||||
|
|
||||||
if (WEBSOCKET.equals(name)) {
|
if (WebSocket.NAME.equals(name)) {
|
||||||
return new WebSocket(opts);
|
return new WebSocket(opts);
|
||||||
} else if (POLLING.equals(name)) {
|
} else if (Polling.NAME.equals(name)) {
|
||||||
return new PollingXHR(opts);
|
return new PollingXHR(opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -513,7 +510,7 @@ public abstract class Socket extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String > filterUpgrades(List<String> upgrades) {
|
/*package*/ List<String > filterUpgrades(List<String> upgrades) {
|
||||||
List<String> filteredUpgrades = new ArrayList<String>();
|
List<String> filteredUpgrades = new ArrayList<String>();
|
||||||
for (String upgrade : upgrades) {
|
for (String upgrade : upgrades) {
|
||||||
if (this.transports.contains(upgrade)) {
|
if (this.transports.contains(upgrade)) {
|
||||||
@@ -555,7 +552,7 @@ public abstract class Socket extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Sockets extends ConcurrentLinkedQueue<Socket> {
|
public static class Sockets extends ArrayList<Socket> {
|
||||||
|
|
||||||
public static final String EVENT_ADD = "add";
|
public static final String EVENT_ADD = "add";
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.github.nkzawa.engineio.client.transports;
|
package com.github.nkzawa.engineio.client.transports;
|
||||||
|
|
||||||
|
|
||||||
import com.github.nkzawa.engineio.client.Socket;
|
|
||||||
import com.github.nkzawa.engineio.client.Transport;
|
import com.github.nkzawa.engineio.client.Transport;
|
||||||
import com.github.nkzawa.engineio.client.Util;
|
import com.github.nkzawa.engineio.client.Util;
|
||||||
import com.github.nkzawa.engineio.parser.Packet;
|
import com.github.nkzawa.engineio.parser.Packet;
|
||||||
@@ -16,6 +15,8 @@ abstract public class Polling extends Transport {
|
|||||||
|
|
||||||
private static final Logger logger = Logger.getLogger("engine.io-client:polling");
|
private static final Logger logger = Logger.getLogger("engine.io-client:polling");
|
||||||
|
|
||||||
|
public static final String NAME = "polling";
|
||||||
|
|
||||||
public static final String EVENT_POLL = "poll";
|
public static final String EVENT_POLL = "poll";
|
||||||
public static final String EVENT_POLL_COMPLETE = "pollComplete";
|
public static final String EVENT_POLL_COMPLETE = "pollComplete";
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ abstract public class Polling extends Transport {
|
|||||||
|
|
||||||
public Polling(Options opts) {
|
public Polling(Options opts) {
|
||||||
super(opts);
|
super(opts);
|
||||||
this.name = Socket.POLLING;
|
this.name = NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void doOpen() {
|
protected void doOpen() {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.github.nkzawa.engineio.client.transports;
|
package com.github.nkzawa.engineio.client.transports;
|
||||||
|
|
||||||
|
|
||||||
import com.github.nkzawa.engineio.client.Socket;
|
|
||||||
import com.github.nkzawa.engineio.client.Transport;
|
import com.github.nkzawa.engineio.client.Transport;
|
||||||
import com.github.nkzawa.engineio.client.Util;
|
import com.github.nkzawa.engineio.client.Util;
|
||||||
import com.github.nkzawa.engineio.parser.Packet;
|
import com.github.nkzawa.engineio.parser.Packet;
|
||||||
@@ -22,6 +21,8 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
public class WebSocket extends Transport {
|
public class WebSocket extends Transport {
|
||||||
|
|
||||||
|
public static final String NAME = "websocket";
|
||||||
|
|
||||||
private WebSocketClient socket;
|
private WebSocketClient socket;
|
||||||
private Future bufferedAmountId;
|
private Future bufferedAmountId;
|
||||||
|
|
||||||
@@ -30,7 +31,7 @@ public class WebSocket extends Transport {
|
|||||||
|
|
||||||
public WebSocket(Options opts) {
|
public WebSocket(Options opts) {
|
||||||
super(opts);
|
super(opts);
|
||||||
this.name = Socket.WEBSOCKET;
|
this.name = NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void doOpen() {
|
protected void doOpen() {
|
||||||
|
|||||||
@@ -0,0 +1,136 @@
|
|||||||
|
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 ConnectionTest {
|
||||||
|
|
||||||
|
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(
|
||||||
|
"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(false);
|
||||||
|
serverError.cancel(false);
|
||||||
|
serverService.shutdown();
|
||||||
|
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = TIMEOUT)
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
socket.open();
|
||||||
|
|
||||||
|
assertThat(events.take(), is("onopen"));
|
||||||
|
socket.close();
|
||||||
|
assertThat(events.take(), is("onclose"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = TIMEOUT)
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,13 @@
|
|||||||
package com.github.nkzawa.engineio.client;
|
package com.github.nkzawa.engineio.client;
|
||||||
|
|
||||||
import org.junit.After;
|
import com.github.nkzawa.engineio.client.transports.Polling;
|
||||||
import org.junit.Before;
|
import com.github.nkzawa.engineio.client.transports.WebSocket;
|
||||||
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.util.ArrayList;
|
||||||
import java.io.IOException;
|
import java.util.List;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.concurrent.*;
|
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
@@ -18,119 +15,24 @@ import static org.junit.Assert.assertThat;
|
|||||||
@RunWith(JUnit4.class)
|
@RunWith(JUnit4.class)
|
||||||
public class SocketTest {
|
public class SocketTest {
|
||||||
|
|
||||||
final static int TIMEOUT = 3000;
|
@Test
|
||||||
final static int PORT = 3000;
|
public void filterUpgrades() {
|
||||||
|
Socket.Options opts = new Socket.Options();
|
||||||
private Process serverProcess;
|
opts.transports = new String[] {Polling.NAME};
|
||||||
private ExecutorService serverService;
|
Socket socket = new Socket(opts) {
|
||||||
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
|
@Override
|
||||||
public void run() {
|
public void onopen() {}
|
||||||
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<String> events = new LinkedBlockingQueue<String>();
|
|
||||||
|
|
||||||
socket = new Socket("ws://localhost:" + PORT) {
|
|
||||||
@Override
|
|
||||||
public void onopen() {
|
|
||||||
System.out.println("onopen:");
|
|
||||||
events.offer("onopen");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onmessage(String data) {}
|
public void onmessage(String data) {}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onclose() {
|
|
||||||
System.out.println("onclose:");
|
|
||||||
events.offer("onclose");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
socket.open();
|
|
||||||
|
|
||||||
assertThat(events.take(), is("onopen"));
|
|
||||||
socket.close();
|
|
||||||
assertThat(events.take(), is("onclose"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(timeout = TIMEOUT)
|
|
||||||
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
|
@Override
|
||||||
public void onclose() {}
|
public void onclose() {}
|
||||||
};
|
};
|
||||||
socket.open();
|
List<String> upgrades = new ArrayList<String>() {{
|
||||||
|
add(Polling.NAME);
|
||||||
assertThat(events.take(), is("hello client"));
|
add(WebSocket.NAME);
|
||||||
assertThat(events.take(), is("hi"));
|
}};
|
||||||
socket.close();
|
List<String> expected = new ArrayList<String>() {{add(Polling.NAME);}};
|
||||||
|
assertThat(socket.filterUpgrades(upgrades), is(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user