Skip to content

Commit dbc6d53

Browse files
committed
Check HubConnection state before running invoke logic
1 parent 437baf6 commit dbc6d53

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/SignalR/clients/java/signalr/src/main/java/com/microsoft/signalr/HubConnection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ public void send(String method, Object... args) {
472472
*/
473473
@SuppressWarnings("unchecked")
474474
public <T> Single<T> invoke(Class<T> returnType, String method, Object... args) {
475+
if (hubConnectionState != HubConnectionState.CONNECTED) {
476+
throw new RuntimeException("The 'invoke' method cannot be called if the connection is not active");
477+
}
478+
475479
String id = connectionState.getNextInvocationId();
476480
InvocationMessage invocationMessage = new InvocationMessage(id, method, args);
477481

src/SignalR/clients/java/signalr/src/test/java/com/microsoft/signalr/HubConnectionTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,15 @@ public void cannotSendBeforeStart() {
903903
assertEquals("The 'send' method cannot be called if the connection is not active", exception.getMessage());
904904
}
905905

906+
@Test
907+
public void cannotInvokeBeforeStart() {
908+
HubConnection hubConnection = TestUtils.createHubConnection("http://example.com");
909+
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
910+
911+
Throwable exception = assertThrows(RuntimeException.class, () -> hubConnection.invoke(String.class, "inc", "arg1"));
912+
assertEquals("The 'invoke' method cannot be called if the connection is not active", exception.getMessage());
913+
}
914+
906915
@Test
907916
public void doesNotErrorWhenReceivingInvokeWithIncorrectArgumentLength() {
908917
MockTransport mockTransport = new MockTransport();
@@ -1204,4 +1213,4 @@ public void non200FromNegotiateThrowsError() {
12041213
() -> hubConnection.start().timeout(1, TimeUnit.SECONDS).blockingAwait());
12051214
assertEquals("Unexpected status code returned from negotiate: 500 Internal server error.", exception.getMessage());
12061215
}
1207-
}
1216+
}

0 commit comments

Comments
 (0)