Check connection state before start stop and send (#2824)
This commit is contained in:
parent
61eea0d0d4
commit
60d617c5ac
|
|
@ -142,6 +142,10 @@ public class HubConnection {
|
||||||
* @throws Exception An error occurred while connecting.
|
* @throws Exception An error occurred while connecting.
|
||||||
*/
|
*/
|
||||||
public void start() throws Exception {
|
public void start() throws Exception {
|
||||||
|
if (connectionState != HubConnectionState.DISCONNECTED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logger.log(LogLevel.Debug, "Starting HubConnection");
|
logger.log(LogLevel.Debug, "Starting HubConnection");
|
||||||
transport.setOnReceive(this.callback);
|
transport.setOnReceive(this.callback);
|
||||||
transport.start();
|
transport.start();
|
||||||
|
|
@ -155,7 +159,11 @@ public class HubConnection {
|
||||||
* Stops a connection to the server.
|
* Stops a connection to the server.
|
||||||
*/
|
*/
|
||||||
private void stop(String errorMessage) {
|
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);
|
logger.log(LogLevel.Error , "HubConnection disconnected with an error %s.", errorMessage);
|
||||||
} else {
|
} else {
|
||||||
logger.log(LogLevel.Debug, "Stopping HubConnection.");
|
logger.log(LogLevel.Debug, "Stopping HubConnection.");
|
||||||
|
|
@ -187,6 +195,10 @@ public class HubConnection {
|
||||||
* @throws Exception If there was an error while sending.
|
* @throws Exception If there was an error while sending.
|
||||||
*/
|
*/
|
||||||
public void send(String method, Object... args) throws Exception {
|
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);
|
InvocationMessage invocationMessage = new InvocationMessage(method, args);
|
||||||
String message = protocol.writeMessage(invocationMessage);
|
String message = protocol.writeMessage(invocationMessage);
|
||||||
logger.log(LogLevel.Debug, "Sending message");
|
logger.log(LogLevel.Debug, "Sending message");
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@
|
||||||
import com.microsoft.aspnet.signalr.*;
|
import com.microsoft.aspnet.signalr.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
@ -87,7 +90,6 @@ public class HubConnectionTest {
|
||||||
assertEquals(1, value.get(), 0);
|
assertEquals(1, value.get(), 0);
|
||||||
|
|
||||||
hubConnection.remove("inc");
|
hubConnection.remove("inc");
|
||||||
hubConnection.send("inc");
|
|
||||||
assertEquals(1, value.get(), 0);
|
assertEquals(1, value.get(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -656,6 +658,35 @@ public class HubConnectionTest {
|
||||||
|
|
||||||
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
|
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 class MockTransport implements Transport {
|
||||||
private OnReceiveCallBack onReceiveCallBack;
|
private OnReceiveCallBack onReceiveCallBack;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue