Skip to content

Commit

Permalink
Added tests to verify JS and C# clients time out connections
Browse files Browse the repository at this point in the history
  • Loading branch information
NTaylorMullen committed May 28, 2013
1 parent a108281 commit b75282a
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 8 deletions.
Expand Up @@ -67,8 +67,7 @@
})(),
createHubConnection: function (end, assert, testName, url, wrapStart) {
var connection,
qs = (testName ? "test=" + window.encodeURIComponent(testName) : ""),
urlSet = !!url;
qs = (testName ? "test=" + window.encodeURIComponent(testName) : "");

wrapStart = typeof wrapStart === "undefined" ? true : false;

Expand Down
Expand Up @@ -2,6 +2,33 @@

testUtilities.runWithAllTransports(function (transport) {

QUnit.asyncTimeoutTest(transport + " transport can timeout when it does not receive initialize message.", testUtilities.defaultTestTimeout, function (end, assert, testName) {
var connection = testUtilities.createHubConnection(end, assert, testName, undefined, false),
savedProcessMessages = $.signalR.transports._logic.processMessages;

$.signalR.transports._logic.processMessages = function (_, minData) {
// Look for initialize message, if we get it, ignore it, transports should time out
if (!minData.S) {
savedProcessMessages.apply(this, arguments);
}
}

connection.start({ transport: transport }).done(function () {
assert.ok(false, "Connection started");
end();
}).fail(function () {
// transport timed out
assert.comment("Connection failed to start!");
end();
});

// Cleanup
return function () {
$.signalR.transports._logic.processMessages = savedProcessMessages;
connection.stop();
};
});

QUnit.asyncTimeoutTest(transport + " transport can send and receive messages on connect.", testUtilities.defaultTestTimeout, function (end, assert, testName) {
var connection = testUtilities.createConnection("multisend", end, assert, testName),
values = [];
Expand Down
Expand Up @@ -14,12 +14,42 @@ QUnit.asyncTimeoutTest("Default transports fall back and connect.", testUtilitie
};
});

// 1 test timeout per transport
QUnit.asyncTimeoutTest("Connection times out when initialize not received.", testUtilities.defaultTestTimeout*4, function (end, assert, testName) {
var connection = testUtilities.createHubConnection(end, assert, testName, undefined, false),
savedProcessMessages = $.signalR.transports._logic.processMessages;

$.signalR.transports._logic.processMessages = function (_, minData) {
// Look for initialize message, if we get it, ignore it, transports should time out
if (!minData.S) {
savedProcessMessages.apply(this, arguments);
}
}

connection.start().done(function () {
assert.ok(false, "Connection started");
end();
}).fail(function () {
// All transports fell back because they did not get init message, success!
assert.comment("Connection failed to start!");
end();
});

// Cleanup
return function () {
$.signalR.transports._logic.processMessages = savedProcessMessages;
connection.stop();
};
});

QUnit.module("Fallback Facts", testUtilities.transports.webSockets.enabled);

QUnit.asyncTimeoutTest("WebSockets fall back to next transport.", testUtilities.defaultTestTimeout, function (end, assert, testName) {
var connection = testUtilities.createHubConnection(end, assert, testName);
var saveWebSocket = window.WebSocket;
window.WebSocket = function () { };
window.WebSocket = function () {
this.close = $.noop;
};
connection.start().done(function () {
assert.notEqual(connection.transport.name, "webSockets", "Connected using " + connection.transport.name);
end();
Expand Down
Expand Up @@ -40,7 +40,7 @@ QUnit.test("connection.json is custom object after set", function () {
QUnit.equal(con.json, customJson, "Verifies connection.json is settable to a custom object.");
});

QUnit.test("connection.json is unique on different objcets object when custom", function () {
QUnit.test("connection.json is unique on different objects when custom", function () {
var con1 = $.connection(),
con2 = $.connection(),
customJson1 = {},
Expand Down
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client;
Expand All @@ -14,6 +15,65 @@ namespace Microsoft.AspNet.SignalR.Tests
{
public class HubProxyFacts : HostedTest
{
[Theory]
[InlineData(HostType.Memory, TransportType.LongPolling)]
[InlineData(HostType.Memory, TransportType.ServerSentEvents)]
[InlineData(HostType.IISExpress, TransportType.Websockets)]
[InlineData(HostType.IISExpress, TransportType.ServerSentEvents)]
[InlineData(HostType.IISExpress, TransportType.LongPolling)]
public void TransportTimesOutIfNoInitMessage(HostType hostType, TransportType transportType)
{
var mre = new ManualResetEventSlim();

using (var host = CreateHost(hostType, transportType))
{
// All defaults except for last parameter (transportConnectTimeout)
host.Initialize(-1,110,30,1);

HubConnection hubConnection = CreateHubConnection(host);
IHubProxy proxy = hubConnection.CreateHubProxy("DelayedOnConnectedHub");

using (hubConnection)
{
hubConnection.Start(host.Transport).Catch(ex =>
{
mre.Set();
});

Assert.True(mre.Wait(TimeSpan.FromSeconds(10)));
}
}
}

[Theory]
[InlineData(HostType.Memory, TransportType.Auto)]
[InlineData(HostType.IISExpress, TransportType.Auto)]
public void ConnectionFailsStartOnMultipleTransportTimeouts(HostType hostType, TransportType transportType)
{
var mre = new ManualResetEventSlim();

using (var host = CreateHost(hostType, transportType))
{
// All defaults except for last parameter (transportConnectTimeout)
host.Initialize(-1, 110, 30, 1);

HubConnection hubConnection = CreateHubConnection(host);
IHubProxy proxy = hubConnection.CreateHubProxy("DelayedOnConnectedHub");

using (hubConnection)
{
hubConnection.Start(host.Transport).Catch(ex =>
{
mre.Set();
});

// Should take 1-2s per transport timeout
Assert.True(mre.Wait(TimeSpan.FromSeconds(15)));
Assert.True(String.IsNullOrEmpty(hubConnection.Transport.Name));
}
}
}

[Theory]
[InlineData(HostType.Memory, TransportType.ServerSentEvents)]
[InlineData(HostType.Memory, TransportType.LongPolling)]
Expand Down Expand Up @@ -45,9 +105,9 @@ public void InitMessageReceivedPriorToStartCompletion(HostType hostType, Transpo
[Theory]
[InlineData(HostType.Memory, TransportType.ServerSentEvents)]
[InlineData(HostType.Memory, TransportType.LongPolling)]
[InlineData(HostType.IISExpress, TransportType.Websockets)]
[InlineData(HostType.IISExpress, TransportType.ServerSentEvents)]
[InlineData(HostType.IISExpress, TransportType.LongPolling)]
[InlineData(HostType.IISExpress, TransportType.Websockets)]
public void TransportsBufferMessagesCorrectly(HostType hostType, TransportType transportType)
{
using (var host = CreateHost(hostType, transportType))
Expand Down
Expand Up @@ -105,14 +105,15 @@ public void ThrownWebExceptionShouldBeUnwrapped(HostType hostType, TransportType
Assert.NotNull(ser.Exception);
}
}
}
}

[Fact]
public void FallbackToLongPollingIIS()
{
using (ITestHost host = CreateHost(HostType.IISExpress))
{
host.Initialize();
// Reduce transportConnectionTimeout to 3 seconds
host.Initialize(-1,110,30,3);

var connection = CreateConnection(host, "/fall-back");

Expand All @@ -130,7 +131,7 @@ public void FallbackToLongPollingIIS()

var client = new DefaultHttpClient();
var transports = new IClientTransport[] {
new ServerSentEventsTransport(client) { ConnectionTimeout = TimeSpan.Zero },
new ServerSentEventsTransport(client),
new LongPollingTransport(client)
};

Expand Down
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.AspNet.SignalR.Tests.Common.Hubs
{
public class DelayedOnConnectedHub : Hub
{
public async override Task OnConnected()
{
await Task.Delay(5000);
await base.OnConnected();
}
}
}
Expand Up @@ -140,6 +140,7 @@
<Compile Include="Connections\SyncErrorConnection.cs" />
<Compile Include="Connections\UnusableProtectedConnection.cs" />
<Compile Include="Hubs\ChatWithGroups.cs" />
<Compile Include="Hubs\DelayedOnConnectedHub.cs" />
<Compile Include="Hubs\EchoHub.cs" />
<Compile Include="Hubs\ExamineHeadersHub.cs" />
<Compile Include="Hubs\GroupJoiningHub.cs" />
Expand Down

0 comments on commit b75282a

Please sign in to comment.