create a new connection when path is the same

This commit is contained in:
nkzawa
2016-01-31 17:33:28 +09:00
parent d538a9d117
commit f7810c19d3
4 changed files with 38 additions and 4 deletions

View File

@@ -64,13 +64,17 @@ public class IO {
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
String id = Url.extractId(parsed);
String path = parsed.getPath();
boolean sameNamespace = managers.containsKey(id)
&& managers.get(id).nsps.containsKey(path);
boolean newConnection = opts.forceNew || !opts.multiplex || sameNamespace;
Manager io; Manager io;
if (opts.forceNew || !opts.multiplex) { if (newConnection) {
logger.fine(String.format("ignoring socket cache for %s", source)); logger.fine(String.format("ignoring socket cache for %s", source));
io = new Manager(source, opts); io = new Manager(source, opts);
} else { } else {
String id = Url.extractId(parsed);
if (!managers.containsKey(id)) { if (!managers.containsKey(id)) {
logger.fine(String.format("new io instance for %s", source)); logger.fine(String.format("new io instance for %s", source));
managers.putIfAbsent(id, new Manager(source, opts)); managers.putIfAbsent(id, new Manager(source, opts));

View File

@@ -96,7 +96,7 @@ public class Manager extends Emitter {
/** /**
* This HashMap can be accessed from outside of EventThread. * This HashMap can be accessed from outside of EventThread.
*/ */
private ConcurrentHashMap<String, Socket> nsps; /*package*/ ConcurrentHashMap<String, Socket> nsps;
public Manager() { public Manager() {

View File

@@ -81,8 +81,16 @@ public abstract class Connection {
return client(createOptions()); return client(createOptions());
} }
Socket client(String path) throws URISyntaxException {
return IO.socket(path, createOptions());
}
Socket client(IO.Options opts) throws URISyntaxException { Socket client(IO.Options opts) throws URISyntaxException {
return IO.socket(uri() + nsp(), opts); return client(nsp(), opts);
}
Socket client(String path, IO.Options opts) throws URISyntaxException {
return IO.socket(uri() + path, opts);
} }
String uri() { String uri() {

View File

@@ -19,6 +19,8 @@ import java.util.concurrent.LinkedBlockingQueue;
import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
@@ -47,6 +49,26 @@ public class ConnectionTest extends Connection {
socket.close(); socket.close();
} }
@Test(timeout = TIMEOUT)
public void startTwoConnectionsWithSamePath() throws URISyntaxException, InterruptedException {
Socket s1 = client("/");
Socket s2 = client("/");
assertThat(s1.io(), not(equalTo(s2.io())));
s1.close();
s2.close();
}
@Test(timeout = TIMEOUT)
public void startTwoConnectionsWithSamePathAndDifferentQuerystrings() throws URISyntaxException, InterruptedException {
Socket s1 = client("/?woot");
Socket s2 = client("/");
assertThat(s1.io(), not(equalTo(s2.io())));
s1.close();
s2.close();
}
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void workWithAcks() throws URISyntaxException, InterruptedException { public void workWithAcks() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>(); final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();