Java Client Expose ConnectionId on HubConnection (#8689)

This commit is contained in:
Mikael Mengistu 2019-03-28 17:02:00 -07:00 committed by GitHub
parent e881ee58c0
commit f10635f6c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 0 deletions

View File

@ -55,6 +55,7 @@ public class HubConnection {
private long handshakeResponseTimeout = 15*1000;
private Map<String, Observable> streamMap = new ConcurrentHashMap<>();
private TransportEnum transportEnum = TransportEnum.ALL;
private String connectionId;
private final Logger logger = LoggerFactory.getLogger(HubConnection.class);
/**
@ -93,6 +94,15 @@ public class HubConnection {
return this.keepAliveInterval;
}
/**
* Gets the connections connectionId. This value will be cleared when the connection is stopped and
* will have a new value every time the connection is successfully started.
* @return A string representing the the client's connectionId.
*/
public String getConnectionId() {
return this.connectionId;
}
// For testing purposes
void setTickRate(long tickRateInMilliseconds) {
this.tickRate = tickRateInMilliseconds;
@ -338,6 +348,7 @@ public class HubConnection {
hubConnectionStateLock.lock();
try {
hubConnectionState = HubConnectionState.CONNECTED;
this.connectionId = negotiateResponse.getConnectionId();
logger.info("HubConnection started.");
resetServerTimeout();
//Don't send pings if we're using long polling.
@ -480,6 +491,7 @@ public class HubConnection {
hubConnectionState = HubConnectionState.DISCONNECTED;
handshakeResponseSubject.onComplete();
redirectAccessTokenProvider = null;
connectionId = null;
transportEnum = TransportEnum.ALL;
this.headers.remove("Authorization");
} finally {

View File

@ -1453,6 +1453,49 @@ class HubConnectionTest {
assertEquals("Negotiate redirection limit exceeded.", exception.getMessage());
}
@Test
public void noConnectionIdWhenSkippingNegotiate() {
MockTransport transport = new MockTransport();
HubConnection hubConnection = HubConnectionBuilder
.create("http://example.com")
.withTransportImplementation(transport)
.shouldSkipNegotiate(true)
.build();
assertNull(hubConnection.getConnectionId());
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
assertNull(hubConnection.getConnectionId());
hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait();
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
assertNull(hubConnection.getConnectionId());
}
@Test
public void connectionIdIsAvailableAfterStart() {
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate",
(req) -> Single.just(new HttpResponse(200, "",
"{\"connectionId\":\"bVOiRPG8-6YiJ6d7ZcTOVQ\",\""
+ "availableTransports\":[{\"transport\":\"WebSockets\",\"transferFormats\":[\"Text\",\"Binary\"]}]}")));
MockTransport transport = new MockTransport(true);
HubConnection hubConnection = HubConnectionBuilder
.create("http://example.com")
.withTransportImplementation(transport)
.withHttpClient(client)
.build();
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
assertNull(hubConnection.getConnectionId());
hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait();
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
assertEquals("bVOiRPG8-6YiJ6d7ZcTOVQ", hubConnection.getConnectionId());
hubConnection.stop().timeout(1, TimeUnit.SECONDS).blockingAwait();
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
assertNull(hubConnection.getConnectionId());
}
@Test
public void afterSuccessfulNegotiateConnectsWithWebsocketsTransport() {
TestHttpClient client = new TestHttpClient().on("POST", "http://example.com/negotiate",