Check HubConnection state before running invoke logic (#4400)

This commit is contained in:
Mikael Mengistu 2019-01-15 11:15:21 -08:00 committed by GitHub
parent 437baf6f5e
commit 852d890cd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -31,6 +31,7 @@ Later on, this will be checked using this condition:
Microsoft.AspNetCore.Authentication.Google;
Microsoft.AspNetCore.Http;
Microsoft.AspNetCore.Server.IIS;
java:signalr;
</PackagesInPatch>
</PropertyGroup>

View File

@ -454,7 +454,7 @@ public class HubConnection {
*/
public void send(String method, Object... args) {
if (hubConnectionState != HubConnectionState.CONNECTED) {
throw new RuntimeException("The 'send' method cannot be called if the connection is not active");
throw new RuntimeException("The 'send' method cannot be called if the connection is not active.");
}
InvocationMessage invocationMessage = new InvocationMessage(null, method, args);
@ -472,6 +472,10 @@ public class HubConnection {
*/
@SuppressWarnings("unchecked")
public <T> Single<T> invoke(Class<T> returnType, String method, Object... args) {
if (hubConnectionState != HubConnectionState.CONNECTED) {
throw new RuntimeException("The 'invoke' method cannot be called if the connection is not active.");
}
String id = connectionState.getNextInvocationId();
InvocationMessage invocationMessage = new InvocationMessage(id, method, args);

View File

@ -900,7 +900,16 @@ class HubConnectionTest {
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
Throwable exception = assertThrows(RuntimeException.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());
}
@Test
public void cannotInvokeBeforeStart() {
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
Throwable exception = assertThrows(RuntimeException.class, () -> hubConnection.invoke(String.class, "inc", "arg1"));
assertEquals("The 'invoke' method cannot be called if the connection is not active.", exception.getMessage());
}
@Test
@ -1204,4 +1213,4 @@ class HubConnectionTest {
() -> hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait());
assertEquals("Unexpected status code returned from negotiate: 500 Internal server error.", exception.getMessage());
}
}
}