Java Client HubConnectionBuilder API Review (#2991)
This commit is contained in:
parent
e5470d9239
commit
71ac906159
|
|
@ -0,0 +1,68 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
|
public class HttpConnectionOptions {
|
||||||
|
private String url;
|
||||||
|
private Transport transport;
|
||||||
|
private LogLevel loglevel;
|
||||||
|
|
||||||
|
private Logger logger;
|
||||||
|
private boolean skipNegotiate;
|
||||||
|
|
||||||
|
public HttpConnectionOptions() {}
|
||||||
|
|
||||||
|
public HttpConnectionOptions(String url, Transport transport, LogLevel logLevel, boolean skipNegotiate) {
|
||||||
|
this.url = url;
|
||||||
|
this.transport = transport;
|
||||||
|
this.skipNegotiate = skipNegotiate;
|
||||||
|
this.loglevel = logLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpConnectionOptions(String url, Transport transport, Logger logger, boolean skipNegotiate) {
|
||||||
|
this.url = url;
|
||||||
|
this.transport = transport;
|
||||||
|
this.skipNegotiate = skipNegotiate;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTransport(Transport transport) {
|
||||||
|
this.transport = transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLoglevel(LogLevel loglevel) {
|
||||||
|
this.loglevel = loglevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSkipNegotiate(boolean skipNegotiate) {
|
||||||
|
this.skipNegotiate = skipNegotiate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Transport getTransport() {
|
||||||
|
return transport;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LogLevel getLoglevel() {
|
||||||
|
return loglevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getSkipNegotiate() {
|
||||||
|
return skipNegotiate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Logger getLogger() {
|
||||||
|
return logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogger(Logger logger) {
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -36,6 +36,10 @@ public class HubConnection {
|
||||||
private static int MAX_NEGOTIATE_ATTEMPTS = 100;
|
private static int MAX_NEGOTIATE_ATTEMPTS = 100;
|
||||||
|
|
||||||
public HubConnection(String url, Transport transport, Logger logger, boolean skipNegotiate) {
|
public HubConnection(String url, Transport transport, Logger logger, boolean skipNegotiate) {
|
||||||
|
if (url == null || url.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("A valid url is required.");
|
||||||
|
}
|
||||||
|
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.protocol = new JsonHubProtocol();
|
this.protocol = new JsonHubProtocol();
|
||||||
|
|
||||||
|
|
@ -44,6 +48,11 @@ public class HubConnection {
|
||||||
} else {
|
} else {
|
||||||
this.logger = new NullLogger();
|
this.logger = new NullLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (transport != null) {
|
||||||
|
this.transport = transport;
|
||||||
|
}
|
||||||
|
|
||||||
this.skipNegotiate = skipNegotiate;
|
this.skipNegotiate = skipNegotiate;
|
||||||
this.callback = (payload) -> {
|
this.callback = (payload) -> {
|
||||||
|
|
||||||
|
|
@ -107,10 +116,6 @@ public class HubConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (transport != null) {
|
|
||||||
this.transport = transport;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private NegotiateResponse handleNegotiate() throws IOException {
|
private NegotiateResponse handleNegotiate() throws IOException {
|
||||||
|
|
@ -136,43 +141,6 @@ public class HubConnection {
|
||||||
return negotiateResponse;
|
return negotiateResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HubConnection(String url, Transport transport, Logger logger) {
|
|
||||||
this(url, transport, logger, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HubConnection(String url, Transport transport, boolean skipNegotiate) {
|
|
||||||
this(url, transport, new NullLogger(), skipNegotiate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new instance of the {@link HubConnection} class.
|
|
||||||
*
|
|
||||||
* @param url The url of the SignalR server to connect to.
|
|
||||||
* @param transport The {@link Transport} that the client will use to communicate with the server.
|
|
||||||
*/
|
|
||||||
public HubConnection(String url, Transport transport) {
|
|
||||||
this(url, transport, new NullLogger());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new instance of the {@link HubConnection} class.
|
|
||||||
*
|
|
||||||
* @param url The url of the SignalR server to connect to.
|
|
||||||
*/
|
|
||||||
public HubConnection(String url) {
|
|
||||||
this(url, null, new NullLogger());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes a new instance of the {@link HubConnection} class.
|
|
||||||
*
|
|
||||||
* @param url The url of the SignalR server to connect to.
|
|
||||||
* @param logLevel The minimum level of messages to log.
|
|
||||||
*/
|
|
||||||
public HubConnection(String url, LogLevel logLevel) {
|
|
||||||
this(url, null, new ConsoleLogger(logLevel));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates the state of the {@link HubConnection} to the server.
|
* Indicates the state of the {@link HubConnection} to the server.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,41 @@
|
||||||
package com.microsoft.aspnet.signalr;
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
public class HubConnectionBuilder {
|
public class HubConnectionBuilder {
|
||||||
private boolean built;
|
|
||||||
private String url;
|
private String url;
|
||||||
private Transport transport;
|
private Transport transport;
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
private boolean skipNegotiate;
|
||||||
|
|
||||||
public HubConnectionBuilder withUrl(String url) {
|
public HubConnectionBuilder withUrl(String url) {
|
||||||
|
if (url == null || url.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("A valid url is required.");
|
||||||
|
}
|
||||||
|
|
||||||
this.url = url;
|
this.url = url;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HubConnectionBuilder withUrl(String url, Transport transport) {
|
public HubConnectionBuilder withUrl(String url, Transport transport) {
|
||||||
|
if (url == null || url.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("A valid url is required.");
|
||||||
|
}
|
||||||
this.url = url;
|
this.url = url;
|
||||||
this.transport = transport;
|
this.transport = transport;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HubConnectionBuilder withUrl(String url, HttpConnectionOptions options) {
|
||||||
|
this.url = url;
|
||||||
|
this.transport = options.getTransport();
|
||||||
|
if (options.getLogger() != null) {
|
||||||
|
this.logger = options.getLogger();
|
||||||
|
} else if (options.getLoglevel() != null) {
|
||||||
|
this.logger = new ConsoleLogger(options.getLoglevel());
|
||||||
|
}
|
||||||
|
this.skipNegotiate = options.getSkipNegotiate();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public HubConnectionBuilder configureLogging(LogLevel logLevel) {
|
public HubConnectionBuilder configureLogging(LogLevel logLevel) {
|
||||||
this.logger = new ConsoleLogger(logLevel);
|
this.logger = new ConsoleLogger(logLevel);
|
||||||
return this;
|
return this;
|
||||||
|
|
@ -30,11 +49,10 @@ public class HubConnectionBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HubConnection build() throws Exception {
|
public HubConnection build() {
|
||||||
if (!built) {
|
if (this.url == null) {
|
||||||
built = true;
|
throw new RuntimeException("The 'HubConnectionBuilder.withUrl' method must be called before building the connection.");
|
||||||
return new HubConnection(url, transport, logger);
|
|
||||||
}
|
}
|
||||||
throw new Exception("HubConnectionBuilder allows creation only of a single instance of HubConnection.");
|
return new HubConnection(url, transport, logger, skipNegotiate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -29,10 +29,6 @@ class WebSocketTransport implements Transport {
|
||||||
this.headers = headers;
|
this.headers = headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WebSocketTransport(String url, Logger logger) throws URISyntaxException {
|
|
||||||
this(url, logger, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public URI getUrl() {
|
public URI getUrl() {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class HandshakeProtocolTest {
|
class HandshakeProtocolTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void VerifyCreateHandshakerequestMessage() {
|
public void VerifyCreateHandshakerequestMessage() {
|
||||||
HandshakeRequestMessage handshakeRequest = new HandshakeRequestMessage("json", 1);
|
HandshakeRequestMessage handshakeRequest = new HandshakeRequestMessage("json", 1);
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class HttpConnectionOptionsTest {
|
||||||
|
@Test
|
||||||
|
public void CheckHttpConnectionOptionsFields() {
|
||||||
|
Transport mockTransport = new MockTransport();
|
||||||
|
HttpConnectionOptions options = new HttpConnectionOptions("http://example.com", mockTransport, LogLevel.Information, true);
|
||||||
|
assertEquals("http://example.com",options.getUrl());
|
||||||
|
assertEquals(LogLevel.Information, options.getLoglevel());
|
||||||
|
assertTrue(options.getSkipNegotiate());
|
||||||
|
assertNotNull(options.getTransport());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class HubConnectionBuilderTest {
|
||||||
|
@Test
|
||||||
|
public void callingBuildWithoutCallingWithUrlThrows() {
|
||||||
|
HubConnectionBuilder builder = new HubConnectionBuilder();
|
||||||
|
Throwable exception = assertThrows(RuntimeException.class, () -> builder.build());
|
||||||
|
assertEquals("The \'HubConnectionBuilder.withUrl\' method must be called before building the connection.", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void passingInNullToWithUrlThrows() {
|
||||||
|
HubConnectionBuilder builder = new HubConnectionBuilder();
|
||||||
|
Throwable exception = assertThrows(IllegalArgumentException.class, () -> builder.withUrl(null));
|
||||||
|
assertEquals("A valid url is required.", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void passingInEmptyStringToWihtUrlThrows() {
|
||||||
|
HubConnectionBuilder builder = new HubConnectionBuilder();
|
||||||
|
Throwable exception = assertThrows(IllegalArgumentException.class, () -> builder.withUrl(""));
|
||||||
|
assertEquals("A valid url is required.", exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,6 @@ package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.concurrent.CancellationException;
|
import java.util.concurrent.CancellationException;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
@ -20,7 +19,22 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void checkHubConnectionState() throws Exception {
|
public void checkHubConnectionState() throws Exception {
|
||||||
Transport mockTransport = new MockTransport();
|
Transport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(),true);
|
||||||
|
hubConnection.start();
|
||||||
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
|
||||||
|
hubConnection.stop();
|
||||||
|
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructHubConnectionWithHttpConnectionOptions() throws Exception {
|
||||||
|
Transport mockTransport = new MockTransport();
|
||||||
|
HttpConnectionOptions options = new HttpConnectionOptions();
|
||||||
|
options.setTransport(mockTransport);
|
||||||
|
options.setLoglevel(LogLevel.Information);
|
||||||
|
options.setSkipNegotiate(true);
|
||||||
|
HubConnection hubConnection = new HubConnectionBuilder().withUrl("http://www.example.com", options).build();
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
|
||||||
|
|
@ -31,7 +45,7 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void hubConnectionClosesAfterCloseMessage() throws Exception {
|
public void hubConnectionClosesAfterCloseMessage() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
||||||
|
|
@ -46,7 +60,7 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void hubConnectionReceiveHandshakeResponseWithError() throws Exception {
|
public void hubConnectionReceiveHandshakeResponseWithError() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
Throwable exception = assertThrows(HubException.class, () -> mockTransport.receiveMessage("{\"error\":\"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR));
|
Throwable exception = assertThrows(HubException.class, () -> mockTransport.receiveMessage("{\"error\":\"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR));
|
||||||
|
|
@ -57,7 +71,7 @@ class HubConnectionTest {
|
||||||
public void registeringMultipleHandlersAndBothGetTriggered() throws Exception {
|
public void registeringMultipleHandlersAndBothGetTriggered() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
|
|
||||||
hubConnection.on("inc", action);
|
hubConnection.on("inc", action);
|
||||||
|
|
@ -83,7 +97,7 @@ class HubConnectionTest {
|
||||||
public void removeHandlerByName() throws Exception {
|
public void removeHandlerByName() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
|
|
||||||
hubConnection.on("inc", action);
|
hubConnection.on("inc", action);
|
||||||
|
|
@ -110,7 +124,7 @@ class HubConnectionTest {
|
||||||
public void addAndRemoveHandlerImmediately() throws Exception {
|
public void addAndRemoveHandlerImmediately() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
|
|
||||||
hubConnection.on("inc", action);
|
hubConnection.on("inc", action);
|
||||||
|
|
@ -135,7 +149,7 @@ class HubConnectionTest {
|
||||||
public void removingMultipleHandlersWithOneCallToRemove() throws Exception {
|
public void removingMultipleHandlersWithOneCallToRemove() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
Action secondAction = () -> value.getAndUpdate((val) -> val + 2);
|
Action secondAction = () -> value.getAndUpdate((val) -> val + 2);
|
||||||
|
|
||||||
|
|
@ -167,7 +181,7 @@ class HubConnectionTest {
|
||||||
public void removeHandlerWithUnsubscribe() throws Exception {
|
public void removeHandlerWithUnsubscribe() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
|
|
||||||
Subscription subscription = hubConnection.on("inc", action);
|
Subscription subscription = hubConnection.on("inc", action);
|
||||||
|
|
@ -200,7 +214,7 @@ class HubConnectionTest {
|
||||||
public void unsubscribeTwice() throws Exception {
|
public void unsubscribeTwice() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(),true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
|
|
||||||
Subscription subscription = hubConnection.on("inc", action);
|
Subscription subscription = hubConnection.on("inc", action);
|
||||||
|
|
@ -234,7 +248,7 @@ class HubConnectionTest {
|
||||||
public void removeSingleHandlerWithUnsubscribe() throws Exception {
|
public void removeSingleHandlerWithUnsubscribe() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
Action secondAction = () -> value.getAndUpdate((val) -> val + 2);
|
Action secondAction = () -> value.getAndUpdate((val) -> val + 2);
|
||||||
|
|
||||||
|
|
@ -264,7 +278,7 @@ class HubConnectionTest {
|
||||||
public void addAndRemoveHandlerImmediatelyWithSubscribe() throws Exception {
|
public void addAndRemoveHandlerImmediatelyWithSubscribe() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
Action action = () -> value.getAndUpdate((val) -> val + 1);
|
||||||
|
|
||||||
Subscription sub = hubConnection.on("inc", action);
|
Subscription sub = hubConnection.on("inc", action);
|
||||||
|
|
@ -289,7 +303,7 @@ class HubConnectionTest {
|
||||||
public void registeringMultipleHandlersThatTakeParamsAndBothGetTriggered() throws Exception {
|
public void registeringMultipleHandlersThatTakeParamsAndBothGetTriggered() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
AtomicReference<Double> value = new AtomicReference<>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
Action1<Double> action = (number) -> value.getAndUpdate((val) -> val + number);
|
Action1<Double> action = (number) -> value.getAndUpdate((val) -> val + number);
|
||||||
|
|
||||||
|
|
@ -308,13 +322,13 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void invokeWaitsForCompletionMessage() throws Exception {
|
public void invokeWaitsForCompletionMessage() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
||||||
|
|
||||||
CompletableFuture<Integer> result = hubConnection.invoke(Integer.class, "echo", "message");
|
CompletableFuture<Integer> result = hubConnection.invoke(Integer.class, "echo", "message");
|
||||||
assertEquals("{\"type\":1,\"invocationId\":\"1\",\"target\":\"echo\",\"arguments\":[\"message\"]}" + RECORD_SEPARATOR, mockTransport.sentMessages.get(1));
|
assertEquals("{\"type\":1,\"invocationId\":\"1\",\"target\":\"echo\",\"arguments\":[\"message\"]}" + RECORD_SEPARATOR, mockTransport.getSentMessages()[1]);
|
||||||
assertFalse(result.isDone());
|
assertFalse(result.isDone());
|
||||||
|
|
||||||
mockTransport.receiveMessage("{\"type\":3,\"invocationId\":\"1\",\"result\":42}" + RECORD_SEPARATOR);
|
mockTransport.receiveMessage("{\"type\":3,\"invocationId\":\"1\",\"result\":42}" + RECORD_SEPARATOR);
|
||||||
|
|
@ -325,15 +339,15 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void multipleInvokesWaitForOwnCompletionMessage() throws Exception {
|
public void multipleInvokesWaitForOwnCompletionMessage() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
||||||
|
|
||||||
CompletableFuture<Integer> result = hubConnection.invoke(Integer.class, "echo", "message");
|
CompletableFuture<Integer> result = hubConnection.invoke(Integer.class, "echo", "message");
|
||||||
CompletableFuture<String> result2 = hubConnection.invoke(String.class, "echo", "message");
|
CompletableFuture<String> result2 = hubConnection.invoke(String.class, "echo", "message");
|
||||||
assertEquals("{\"type\":1,\"invocationId\":\"1\",\"target\":\"echo\",\"arguments\":[\"message\"]}" + RECORD_SEPARATOR, mockTransport.sentMessages.get(1));
|
assertEquals("{\"type\":1,\"invocationId\":\"1\",\"target\":\"echo\",\"arguments\":[\"message\"]}" + RECORD_SEPARATOR, mockTransport.getSentMessages()[1]);
|
||||||
assertEquals("{\"type\":1,\"invocationId\":\"2\",\"target\":\"echo\",\"arguments\":[\"message\"]}" + RECORD_SEPARATOR, mockTransport.sentMessages.get(2));
|
assertEquals("{\"type\":1,\"invocationId\":\"2\",\"target\":\"echo\",\"arguments\":[\"message\"]}" + RECORD_SEPARATOR, mockTransport.getSentMessages()[2]);
|
||||||
assertFalse(result.isDone());
|
assertFalse(result.isDone());
|
||||||
assertFalse(result2.isDone());
|
assertFalse(result2.isDone());
|
||||||
|
|
||||||
|
|
@ -348,7 +362,7 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void invokeWorksForPrimitiveTypes() throws Exception {
|
public void invokeWorksForPrimitiveTypes() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
||||||
|
|
@ -366,7 +380,7 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void completionMessageCanHaveError() throws Exception {
|
public void completionMessageCanHaveError() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
||||||
|
|
@ -390,7 +404,7 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void stopCancelsActiveInvokes() throws Exception {
|
public void stopCancelsActiveInvokes() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
|
||||||
|
|
@ -415,7 +429,7 @@ class HubConnectionTest {
|
||||||
public void sendWithNoParamsTriggersOnHandler() throws Exception {
|
public void sendWithNoParamsTriggersOnHandler() throws Exception {
|
||||||
AtomicReference<Integer> value = new AtomicReference<>(0);
|
AtomicReference<Integer> value = new AtomicReference<>(0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", () ->{
|
hubConnection.on("inc", () ->{
|
||||||
assertEquals(Integer.valueOf(0), value.get());
|
assertEquals(Integer.valueOf(0), value.get());
|
||||||
|
|
@ -434,7 +448,7 @@ class HubConnectionTest {
|
||||||
public void sendWithParamTriggersOnHandler() throws Exception {
|
public void sendWithParamTriggersOnHandler() throws Exception {
|
||||||
AtomicReference<String> value = new AtomicReference<>();
|
AtomicReference<String> value = new AtomicReference<>();
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param) ->{
|
hubConnection.on("inc", (param) ->{
|
||||||
assertNull(value.get());
|
assertNull(value.get());
|
||||||
|
|
@ -456,7 +470,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<Double> value2 = new AtomicReference<>();
|
AtomicReference<Double> value2 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1, param2) ->{
|
hubConnection.on("inc", (param1, param2) ->{
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -483,7 +497,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<String> value3 = new AtomicReference<>();
|
AtomicReference<String> value3 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1, param2, param3) ->{
|
hubConnection.on("inc", (param1, param2, param3) ->{
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -514,7 +528,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<String> value4 = new AtomicReference<>();
|
AtomicReference<String> value4 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1, param2, param3, param4) ->{
|
hubConnection.on("inc", (param1, param2, param3, param4) ->{
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -548,7 +562,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<Double> value5 = new AtomicReference<>();
|
AtomicReference<Double> value5 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1, param2, param3, param4, param5) ->{
|
hubConnection.on("inc", (param1, param2, param3, param4, param5) ->{
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -586,7 +600,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<String> value6 = new AtomicReference<>();
|
AtomicReference<String> value6 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1, param2, param3, param4, param5, param6) -> {
|
hubConnection.on("inc", (param1, param2, param3, param4, param5, param6) -> {
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -628,7 +642,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<String> value7 = new AtomicReference<>();
|
AtomicReference<String> value7 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1, param2, param3, param4, param5, param6, param7) -> {
|
hubConnection.on("inc", (param1, param2, param3, param4, param5, param6, param7) -> {
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -674,7 +688,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<String> value8 = new AtomicReference<>();
|
AtomicReference<String> value8 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1, param2, param3, param4, param5, param6, param7, param8) -> {
|
hubConnection.on("inc", (param1, param2, param3, param4, param5, param6, param7, param8) -> {
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -721,7 +735,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<Custom> value1 = new AtomicReference<>();
|
AtomicReference<Custom> value1 = new AtomicReference<>();
|
||||||
|
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", (param1) -> {
|
hubConnection.on("inc", (param1) -> {
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -746,7 +760,7 @@ class HubConnectionTest {
|
||||||
public void receiveHandshakeResponseAndMessage() throws Exception {
|
public void receiveHandshakeResponseAndMessage() throws Exception {
|
||||||
AtomicReference<Double> value = new AtomicReference<Double>(0.0);
|
AtomicReference<Double> value = new AtomicReference<Double>(0.0);
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
|
|
||||||
hubConnection.on("inc", () ->{
|
hubConnection.on("inc", () ->{
|
||||||
assertEquals(Double.valueOf(0), value.get());
|
assertEquals(Double.valueOf(0), value.get());
|
||||||
|
|
@ -768,7 +782,7 @@ class HubConnectionTest {
|
||||||
public void onClosedCallbackRunsWhenStopIsCalled() throws Exception {
|
public void onClosedCallbackRunsWhenStopIsCalled() throws Exception {
|
||||||
AtomicReference<String> value1 = new AtomicReference<>();
|
AtomicReference<String> value1 = new AtomicReference<>();
|
||||||
Transport mockTransport = new MockTransport();
|
Transport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
hubConnection.onClosed((ex) -> {
|
hubConnection.onClosed((ex) -> {
|
||||||
assertNull(value1.get());
|
assertNull(value1.get());
|
||||||
|
|
@ -785,7 +799,7 @@ class HubConnectionTest {
|
||||||
AtomicReference<String> value1 = new AtomicReference<>();
|
AtomicReference<String> value1 = new AtomicReference<>();
|
||||||
AtomicReference<String> value2 = new AtomicReference<>();
|
AtomicReference<String> value2 = new AtomicReference<>();
|
||||||
Transport mockTransport = new MockTransport();
|
Transport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
|
|
||||||
hubConnection.onClosed((ex) -> {
|
hubConnection.onClosed((ex) -> {
|
||||||
|
|
@ -810,7 +824,7 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void hubConnectionClosesAndRunsOnClosedCallbackAfterCloseMessageWithError() throws Exception {
|
public void hubConnectionClosesAndRunsOnClosedCallbackAfterCloseMessageWithError() throws Exception {
|
||||||
MockTransport mockTransport = new MockTransport();
|
MockTransport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
hubConnection.onClosed((ex) -> {
|
hubConnection.onClosed((ex) -> {
|
||||||
assertEquals(ex.getMessage(), "There was an error");
|
assertEquals(ex.getMessage(), "There was an error");
|
||||||
});
|
});
|
||||||
|
|
@ -827,7 +841,7 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void callingStartOnStartedHubConnectionNoOps() throws Exception {
|
public void callingStartOnStartedHubConnectionNoOps() throws Exception {
|
||||||
Transport mockTransport = new MockTransport();
|
Transport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport ,new NullLogger() ,true);
|
||||||
hubConnection.start();
|
hubConnection.start();
|
||||||
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState());
|
||||||
|
|
||||||
|
|
@ -841,49 +855,10 @@ class HubConnectionTest {
|
||||||
@Test
|
@Test
|
||||||
public void cannotSendBeforeStart() throws Exception {
|
public void cannotSendBeforeStart() throws Exception {
|
||||||
Transport mockTransport = new MockTransport();
|
Transport mockTransport = new MockTransport();
|
||||||
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
|
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, new NullLogger(), true);
|
||||||
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
||||||
|
|
||||||
Throwable exception = assertThrows(HubException.class, () -> hubConnection.send("inc"));
|
Throwable exception = assertThrows(HubException.class, () -> hubConnection.send("inc"));
|
||||||
assertEquals("The 'send' method cannot be called if the connection is not active", exception.getMessage());
|
assertEquals("The 'send' method cannot be called if the connection is not active", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MockTransport implements Transport {
|
|
||||||
private OnReceiveCallBack onReceiveCallBack;
|
|
||||||
private ArrayList<String> sentMessages = new ArrayList<>();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletableFuture start() {
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletableFuture send(String message) {
|
|
||||||
sentMessages.add(message);
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setOnReceive(OnReceiveCallBack callback) {
|
|
||||||
this.onReceiveCallBack = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(String message) throws Exception {
|
|
||||||
this.onReceiveCallBack.invoke(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CompletableFuture stop() {
|
|
||||||
return CompletableFuture.completedFuture(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void receiveMessage(String message) throws Exception {
|
|
||||||
this.onReceive(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String[] getSentMessages() {
|
|
||||||
return sentMessages.toArray(new String[sentMessages.size()]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,46 @@
|
||||||
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
class MockTransport implements Transport {
|
||||||
|
private OnReceiveCallBack onReceiveCallBack;
|
||||||
|
private ArrayList<String> sentMessages = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture start() {
|
||||||
|
return CompletableFuture.completedFuture(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture send(String message) {
|
||||||
|
sentMessages.add(message);
|
||||||
|
return CompletableFuture.completedFuture(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setOnReceive(OnReceiveCallBack callback) {
|
||||||
|
this.onReceiveCallBack = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(String message) throws Exception {
|
||||||
|
this.onReceiveCallBack.invoke(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CompletableFuture stop() {
|
||||||
|
return CompletableFuture.completedFuture(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void receiveMessage(String message) throws Exception {
|
||||||
|
this.onReceive(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getSentMessages() {
|
||||||
|
return sentMessages.toArray(new String[sentMessages.size()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ package com.microsoft.aspnet.signalr;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
@ -12,7 +13,7 @@ import org.junit.jupiter.api.Test;
|
||||||
class WebSocketTransportTest {
|
class WebSocketTransportTest {
|
||||||
@Test
|
@Test
|
||||||
public void WebsocketThrowsIfItCantConnect() throws Exception {
|
public void WebsocketThrowsIfItCantConnect() throws Exception {
|
||||||
Transport transport = new WebSocketTransport("www.notarealurl12345.fake", new NullLogger());
|
Transport transport = new WebSocketTransport("www.notarealurl12345.fake", new NullLogger(), new HashMap<>());
|
||||||
Throwable exception = assertThrows(Exception.class, () -> transport.start().get(1,TimeUnit.SECONDS));
|
Throwable exception = assertThrows(Exception.class, () -> transport.start().get(1,TimeUnit.SECONDS));
|
||||||
assertEquals("There was an error starting the Websockets transport.", exception.getCause().getMessage());
|
assertEquals("There was an error starting the Websockets transport.", exception.getCause().getMessage());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ package com.microsoft.aspnet.signalr;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
|
@ -24,7 +25,7 @@ class WebSocketTransportUrlFormatTest {
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource("protocols")
|
@MethodSource("protocols")
|
||||||
public void checkWebsocketUrlProtocol(String url, String expectedUrl) throws URISyntaxException {
|
public void checkWebsocketUrlProtocol(String url, String expectedUrl) throws URISyntaxException {
|
||||||
WebSocketTransport webSocketTransport = new WebSocketTransport(url, new NullLogger());
|
WebSocketTransport webSocketTransport = new WebSocketTransport(url, new NullLogger(), new HashMap<>());
|
||||||
assertEquals(expectedUrl, webSocketTransport.getUrl().toString());
|
assertEquals(expectedUrl, webSocketTransport.getUrl().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -33,7 +33,7 @@ public class Chat {
|
||||||
});
|
});
|
||||||
|
|
||||||
//This is a blocking call
|
//This is a blocking call
|
||||||
hubConnection.start();
|
hubConnection.start().get();
|
||||||
|
|
||||||
String message = "";
|
String message = "";
|
||||||
while (!message.equals("leave")) {
|
while (!message.equals("leave")) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue