Merge branch 'release/2.2'

This commit is contained in:
MikaelMengistu 2018-08-21 14:33:24 -07:00
commit 797ccbedf2
3 changed files with 46 additions and 3 deletions

View File

@ -76,6 +76,6 @@
<XunitPackageVersion>2.3.1</XunitPackageVersion>
<XunitRunnerVisualStudioPackageVersion>2.4.0</XunitRunnerVisualStudioPackageVersion>
</PropertyGroup>
<PropertyGroup Label="Package Versions: Pinned" />
<Import Project="$(DotNetPackageVersionPropsPath)" Condition=" '$(DotNetPackageVersionPropsPath)' != '' " />
<PropertyGroup Label="Package Versions: Pinned" />
</Project>

View File

@ -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");

View File

@ -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;