Merge pull request #3180 from dotnet-maestro-bot/merge/release/2.2-to-master

[automated] Merge branch 'release/2.2' => 'master'
This commit is contained in:
Mikael Mengistu 2018-10-23 10:34:05 -07:00 committed by GitHub
commit 4990c315e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 36 deletions

View File

@ -3,7 +3,6 @@
package com.microsoft.signalr;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@ -18,7 +17,7 @@ public class HttpHubConnectionBuilder {
private HttpClient httpClient;
private boolean skipNegotiate;
private Single<String> accessTokenProvider;
private Duration handshakeResponseTimeout;
private long handshakeResponseTimeout = 0;
private Map<String, String> headers;
HttpHubConnectionBuilder(String url) {
@ -73,11 +72,11 @@ public class HttpHubConnectionBuilder {
/**
* Sets the duration the {@link HubConnection} should wait for a Handshake Response from the server.
*
* @param timeout The duration that the {@link HubConnection} should wait for a Handshake Response from the server.
* @param timeoutInMilliseconds The duration (specified in milliseconds) that the {@link HubConnection} should wait for a Handshake Response from the server.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withHandshakeResponseTimeout(Duration timeout) {
this.handshakeResponseTimeout = timeout;
public HttpHubConnectionBuilder withHandshakeResponseTimeout(long timeoutInMilliseconds) {
this.handshakeResponseTimeout = timeoutInMilliseconds;
return this;
}
@ -115,4 +114,4 @@ public class HttpHubConnectionBuilder {
public HubConnection build() {
return new HubConnection(url, transport, skipNegotiate, httpClient, accessTokenProvider, handshakeResponseTimeout, headers);
}
}
}

View File

@ -3,7 +3,6 @@
package com.microsoft.signalr;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
@ -47,57 +46,57 @@ public class HubConnection {
private Timer pingTimer = null;
private final AtomicLong nextServerTimeout = new AtomicLong();
private final AtomicLong nextPingActivation = new AtomicLong();
private Duration keepAliveInterval = Duration.ofSeconds(15);
private Duration serverTimeout = Duration.ofSeconds(30);
private Duration tickRate = Duration.ofSeconds(1);
private long keepAliveInterval = 15*1000;
private long serverTimeout = 30*1000;
private long tickRate = 1000;
private CompletableFuture<Void> handshakeResponseFuture;
private Duration handshakeResponseTimeout = Duration.ofSeconds(15);
private long handshakeResponseTimeout = 15*1000;
private final Logger logger = LoggerFactory.getLogger(HubConnection.class);
/**
* Sets the server timeout interval for the connection.
*
* @param serverTimeout The server timeout duration.
* @param serverTimeoutInMilliseconds The server timeout duration (specified in milliseconds).
*/
public void setServerTimeout(Duration serverTimeout) {
this.serverTimeout = serverTimeout;
public void setServerTimeout(long serverTimeoutInMilliseconds) {
this.serverTimeout = serverTimeoutInMilliseconds;
}
/**
* Gets the server timeout duration.
*
* @return The server timeout duration.
* @return The server timeout duration (specified in milliseconds).
*/
public Duration getServerTimeout() {
public long getServerTimeout() {
return this.serverTimeout;
}
/**
* Sets the keep alive interval duration.
*
* @param keepAliveInterval The interval at which the connection should send keep alive messages.
* @param keepAliveIntervalInMilliseconds The interval (specified in milliseconds) at which the connection should send keep alive messages.
*/
public void setKeepAliveInterval(Duration keepAliveInterval) {
this.keepAliveInterval = keepAliveInterval;
public void setKeepAliveInterval(long keepAliveIntervalInMilliseconds) {
this.keepAliveInterval = keepAliveIntervalInMilliseconds;
}
/**
* Gets the keep alive interval.
*
* @return The interval between keep alive messages.
* @return The interval (specified in milliseconds) between keep alive messages.
*/
public Duration getKeepAliveInterval() {
public long getKeepAliveInterval() {
return this.keepAliveInterval;
}
// For testing purposes
void setTickRate(Duration tickRate) {
this.tickRate = tickRate;
void setTickRate(long tickRateInMilliseconds) {
this.tickRate = tickRateInMilliseconds;
}
HubConnection(String url, Transport transport, boolean skipNegotiate, HttpClient httpClient,
Single<String> accessTokenProvider, Duration handshakeResponseTimeout, Map<String, String> headers) {
Single<String> accessTokenProvider, long handshakeResponseTimeout, Map<String, String> headers) {
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("A valid url is required.");
}
@ -121,7 +120,7 @@ public class HubConnection {
this.transport = transport;
}
if (handshakeResponseTimeout != null) {
if (handshakeResponseTimeout > 0) {
this.handshakeResponseTimeout = handshakeResponseTimeout;
}
@ -249,6 +248,7 @@ public class HubConnection {
/**
* Starts a connection to the server.
*
* @return A Completable that completes when the connection has been established.
*/
public Completable start() {
@ -287,7 +287,7 @@ public class HubConnection {
String handshake = HandshakeProtocol.createHandshakeRequestMessage(
new HandshakeRequestMessage(protocol.getName(), protocol.getVersion()));
return transport.send(handshake).thenCompose((innerFuture) -> {
timeoutHandshakeResponse(handshakeResponseTimeout.toMillis(), TimeUnit.MILLISECONDS);
timeoutHandshakeResponse(handshakeResponseTimeout, TimeUnit.MILLISECONDS);
return handshakeResponseFuture.thenRun(() -> {
hubConnectionStateLock.lock();
try {
@ -316,7 +316,7 @@ public class HubConnection {
pingTimer.cancel();
}
}
}, new Date(0), tickRate.toMillis());
}, new Date(0), tickRate);
} finally {
hubConnectionStateLock.unlock();
}
@ -492,11 +492,11 @@ public class HubConnection {
}
private void resetServerTimeout() {
this.nextServerTimeout.set(System.currentTimeMillis() + serverTimeout.toMillis());
this.nextServerTimeout.set(System.currentTimeMillis() + serverTimeout);
}
private void resetKeepAlive() {
this.nextPingActivation.set(System.currentTimeMillis() + keepAliveInterval.toMillis());
this.nextPingActivation.set(System.currentTimeMillis() + keepAliveInterval);
}
/**
@ -824,4 +824,4 @@ public class HubConnection {
return handlers.get(0).getClasses();
}
}
}
}

View File

@ -5,7 +5,6 @@ package com.microsoft.signalr;
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
@ -68,7 +67,7 @@ class HubConnectionTest {
.withTransport(mockTransport)
.withHttpClient(new TestHttpClient())
.shouldSkipNegotiate(true)
.withHandshakeResponseTimeout(Duration.ofMillis(100))
.withHandshakeResponseTimeout(100)
.build();
Throwable exception = assertThrows(RuntimeException.class, () -> hubConnection.start().blockingAwait(1000, TimeUnit.MILLISECONDS));
assertEquals(ExecutionException.class, exception.getCause().getClass());
@ -1066,8 +1065,8 @@ class HubConnectionTest {
@Test
public void connectionTimesOutIfServerDoesNotSendMessage() throws InterruptedException, ExecutionException, TimeoutException {
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
hubConnection.setServerTimeout(Duration.ofMillis(1));
hubConnection.setTickRate(Duration.ofMillis(1));
hubConnection.setServerTimeout(1);
hubConnection.setTickRate(1);
CompletableFuture<Exception> closedFuture = new CompletableFuture<>();
hubConnection.onClosed((e) -> {
closedFuture.complete(e);
@ -1082,8 +1081,8 @@ class HubConnectionTest {
public void connectionSendsPingsRegularly() throws InterruptedException {
MockTransport mockTransport = new MockTransport(true, false);
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com", mockTransport);
hubConnection.setKeepAliveInterval(Duration.ofMillis(1));
hubConnection.setTickRate(Duration.ofMillis(1));
hubConnection.setKeepAliveInterval(1);
hubConnection.setTickRate(1);
hubConnection.start().blockingAwait(1000, TimeUnit.MILLISECONDS);