diff --git a/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java b/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java index 6164d97e66..056a6661ba 100644 --- a/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java +++ b/clients/java/signalr/src/main/java/com/microsoft/aspnet/signalr/HubConnection.java @@ -142,6 +142,10 @@ public class HubConnection { * @throws Exception An error occurred while connecting. */ public void start() throws Exception { + if (connectionState != HubConnectionState.DISCONNECTED) { + return; + } + logger.log(LogLevel.Debug, "Starting HubConnection"); transport.setOnReceive(this.callback); transport.start(); @@ -155,7 +159,11 @@ public class HubConnection { * Stops a connection to the server. */ private void stop(String errorMessage) { - if(errorMessage != null){ + if (connectionState == HubConnectionState.DISCONNECTED) { + return; + } + + if(errorMessage != null) { logger.log(LogLevel.Error , "HubConnection disconnected with an error %s.", errorMessage); } else { logger.log(LogLevel.Debug, "Stopping HubConnection."); @@ -187,6 +195,10 @@ public class HubConnection { * @throws Exception If there was an error while sending. */ public void send(String method, Object... args) throws Exception { + if (connectionState != HubConnectionState.CONNECTED) { + throw new HubException("The 'send' method cannot be called if the connection is not active"); + } + InvocationMessage invocationMessage = new InvocationMessage(method, args); String message = protocol.writeMessage(invocationMessage); logger.log(LogLevel.Debug, "Sending message"); diff --git a/clients/java/signalr/src/test/java/HubConnectionTest.java b/clients/java/signalr/src/test/java/HubConnectionTest.java index 1b71543795..867078cc82 100644 --- a/clients/java/signalr/src/test/java/HubConnectionTest.java +++ b/clients/java/signalr/src/test/java/HubConnectionTest.java @@ -4,7 +4,10 @@ import com.microsoft.aspnet.signalr.*; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicReference; + +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.junit.Assert.*; @@ -87,7 +90,6 @@ public class HubConnectionTest { assertEquals(1, value.get(), 0); hubConnection.remove("inc"); - hubConnection.send("inc"); assertEquals(1, value.get(), 0); } @@ -656,6 +658,35 @@ public class HubConnectionTest { assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); } + + @Test + public void CallingStartOnStartedHubConnectionNoOps() throws Exception { + Transport mockTransport = new MockTransport(); + HubConnection hubConnection = new HubConnection("http://example.com", mockTransport); + hubConnection.start(); + assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState()); + + hubConnection.start(); + assertEquals(HubConnectionState.CONNECTED, hubConnection.getConnectionState()); + + hubConnection.stop(); + assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); + } + + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + @Test + public void CannotSendBeforeStart() throws Exception { + exceptionRule.expect(HubException.class); + exceptionRule.expectMessage("The 'send' method cannot be called if the connection is not active"); + + Transport mockTransport = new MockTransport(); + HubConnection hubConnection = new HubConnection("http://example.com", mockTransport); + assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState()); + + hubConnection.send("inc"); + } private class MockTransport implements Transport { private OnReceiveCallBack onReceiveCallBack;