Skip to content

Commit 852d890

Browse files
authored
Check HubConnection state before running invoke logic (#4400)
1 parent 437baf6 commit 852d890

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

eng/PatchConfig.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Later on, this will be checked using this condition:
3131
Microsoft.AspNetCore.Authentication.Google;
3232
Microsoft.AspNetCore.Http;
3333
Microsoft.AspNetCore.Server.IIS;
34+
java:signalr;
3435
</PackagesInPatch>
3536
</PropertyGroup>
3637

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ private void stopConnection(String errorMessage) {
454454
*/
455455
public void send(String method, Object... args) {
456456
if (hubConnectionState != HubConnectionState.CONNECTED) {
457-
throw new RuntimeException("The 'send' method cannot be called if the connection is not active");
457+
throw new RuntimeException("The 'send' method cannot be called if the connection is not active.");
458458
}
459459

460460
InvocationMessage invocationMessage = new InvocationMessage(null, method, args);
@@ -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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,16 @@ public void cannotSendBeforeStart() {
900900
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
901901

902902
Throwable exception = assertThrows(RuntimeException.class, () -> hubConnection.send("inc"));
903-
assertEquals("The 'send' method cannot be called if the connection is not active", exception.getMessage());
903+
assertEquals("The 'send' method cannot be called if the connection is not active.", exception.getMessage());
904+
}
905+
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());
904913
}
905914

906915
@Test
@@ -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)