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");
|
||||
}};
|
||||
|
||||
public static final String POLLING = "polling";
|
||||
public static final String WEBSOCKET = "websocket";
|
||||
|
||||
public static final String EVENT_OPEN = "open";
|
||||
public static final String EVENT_CLOSE = "close";
|
||||
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.timestampParam = opts.timestampParam != null ? opts.timestampParam : "t";
|
||||
this.timestampRequests = opts.timestampRequests;
|
||||
this.transports = new ArrayList<String>(Arrays.asList(
|
||||
opts.transports != null ? opts.transports : new String[] {POLLING, WEBSOCKET}));
|
||||
this.transports = new ArrayList<String>(Arrays.asList(opts.transports != null ?
|
||||
opts.transports : new String[] {Polling.NAME, WebSocket.NAME}));
|
||||
this.policyPort = opts.policyPort != 0 ? opts.policyPort : 843;
|
||||
|
||||
Socket.sockets.add(this);
|
||||
@@ -151,9 +148,9 @@ public abstract class Socket extends Emitter {
|
||||
opts.timestampParam = this.timestampParam;
|
||||
opts.policyPort = this.policyPort;
|
||||
|
||||
if (WEBSOCKET.equals(name)) {
|
||||
if (WebSocket.NAME.equals(name)) {
|
||||
return new WebSocket(opts);
|
||||
} else if (POLLING.equals(name)) {
|
||||
} else if (Polling.NAME.equals(name)) {
|
||||
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>();
|
||||
for (String upgrade : upgrades) {
|
||||
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";
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
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.Util;
|
||||
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");
|
||||
|
||||
public static final String NAME = "polling";
|
||||
|
||||
public static final String EVENT_POLL = "poll";
|
||||
public static final String EVENT_POLL_COMPLETE = "pollComplete";
|
||||
|
||||
@@ -24,7 +25,7 @@ abstract public class Polling extends Transport {
|
||||
|
||||
public Polling(Options opts) {
|
||||
super(opts);
|
||||
this.name = Socket.POLLING;
|
||||
this.name = NAME;
|
||||
}
|
||||
|
||||
protected void doOpen() {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
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.Util;
|
||||
import com.github.nkzawa.engineio.parser.Packet;
|
||||
@@ -22,6 +21,8 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class WebSocket extends Transport {
|
||||
|
||||
public static final String NAME = "websocket";
|
||||
|
||||
private WebSocketClient socket;
|
||||
private Future bufferedAmountId;
|
||||
|
||||
@@ -30,7 +31,7 @@ public class WebSocket extends Transport {
|
||||
|
||||
public WebSocket(Options opts) {
|
||||
super(opts);
|
||||
this.name = Socket.WEBSOCKET;
|
||||
this.name = NAME;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import com.github.nkzawa.engineio.client.transports.Polling;
|
||||
import com.github.nkzawa.engineio.client.transports.WebSocket;
|
||||
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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
@@ -18,119 +15,24 @@ import static org.junit.Assert.assertThat;
|
||||
@RunWith(JUnit4.class)
|
||||
public class SocketTest {
|
||||
|
||||
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() {
|
||||
@Test
|
||||
public void filterUpgrades() {
|
||||
Socket.Options opts = new Socket.Options();
|
||||
opts.transports = new String[] {Polling.NAME};
|
||||
Socket socket = new Socket(opts) {
|
||||
@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");
|
||||
}
|
||||
|
||||
public void 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();
|
||||
List<String> upgrades = new ArrayList<String>() {{
|
||||
add(Polling.NAME);
|
||||
add(WebSocket.NAME);
|
||||
}};
|
||||
List<String> expected = new ArrayList<String>() {{add(Polling.NAME);}};
|
||||
assertThat(socket.filterUpgrades(upgrades), is(expected));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user