Skip to content

Commit

Permalink
Updated the functional tests to use the "using" pattern now that conn…
Browse files Browse the repository at this point in the history
…ections are disposable

#1844
  • Loading branch information
NTaylorMullen committed Apr 19, 2013
1 parent 723451f commit 05c48e2
Show file tree
Hide file tree
Showing 9 changed files with 1,489 additions and 1,276 deletions.
Expand Up @@ -22,7 +22,7 @@ public void ConnectionDisposeTriggersStop(HostType hostType, TransportType trans
using (var host = CreateHost(hostType, transportType))
{
host.Initialize();
var connection = CreateConnection(host,"/signalr");
var connection = CreateConnection(host, "/signalr");

using (connection)
{
Expand All @@ -48,33 +48,33 @@ public void RequestHeadersSetCorrectly(HostType hostType, TransportType transpor
host.Initialize();
var connection = CreateConnection(host, "/examine-request");

connection.Received += (arg) =>
using (connection)
{
JObject headers = JsonConvert.DeserializeObject<JObject>(arg);
connection.Received += (arg) =>
{
JObject headers = JsonConvert.DeserializeObject<JObject>(arg);
if (transportType != TransportType.Websockets)
{
Assert.Equal("referer", (string)headers["refererHeader"]);
}
Assert.Equal("test-header", (string)headers["testHeader"]);
tcs.TrySetResult(null);
};

connection.Error += e => tcs.TrySetException(e);

connection.Headers.Add("test-header", "test-header");
if (transportType != TransportType.Websockets)
{
Assert.Equal("referer", (string)headers["refererHeader"]);
connection.Headers.Add(System.Net.HttpRequestHeader.Referer.ToString(), "referer");
}
Assert.Equal("test-header", (string)headers["testHeader"]);
tcs.TrySetResult(null);
};

connection.Error += e => tcs.TrySetException(e);
connection.Start(host.Transport).Wait();
connection.Send("Hello");

connection.Headers.Add("test-header", "test-header");
if (transportType != TransportType.Websockets)
{
connection.Headers.Add(System.Net.HttpRequestHeader.Referer.ToString(), "referer");
// Assert
Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(10)));
}

connection.Start(host.Transport).Wait();
connection.Send("Hello");

// Assert
Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(10)));

// Clean-up
connection.Stop();
}
}

Expand All @@ -90,13 +90,13 @@ public void RequestHeadersCannotBeSetOnceConnected(HostType hostType, TransportT
host.Initialize();
var connection = CreateConnection(host, "/examine-request");

connection.Start(host.Transport).Wait();

var ex = Assert.Throws<InvalidOperationException>(() => connection.Headers.Add("test-header", "test-header"));
Assert.Equal("Request headers cannot be set after the connection has started.", ex.Message);
using (connection)
{
connection.Start(host.Transport).Wait();

// Clean-up
connection.Stop();
var ex = Assert.Throws<InvalidOperationException>(() => connection.Headers.Add("test-header", "test-header"));
Assert.Equal("Request headers cannot be set after the connection has started.", ex.Message);
}
}
}

Expand All @@ -118,23 +118,23 @@ public void ReconnectRequestPathEndsInReconnect(HostType hostType, TransportType

var connection = CreateConnection(host, "/examine-reconnect");

connection.Received += (reconnectEndsPath) =>
using (connection)
{
if (!receivedMessage)
connection.Received += (reconnectEndsPath) =>
{
tcs.TrySetResult(reconnectEndsPath == "True");
receivedMessage = true;
}
};

connection.Start(host.Transport).Wait();
if (!receivedMessage)
{
tcs.TrySetResult(reconnectEndsPath == "True");
receivedMessage = true;
}
};

// Wait for reconnect
Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(10)));
Assert.True(tcs.Task.Result);
connection.Start(host.Transport).Wait();

// Clean-up
connection.Stop();
// Wait for reconnect
Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(10)));
Assert.True(tcs.Task.Result);
}
}
}
}
Expand Down
135 changes: 76 additions & 59 deletions tests/Microsoft.AspNet.SignalR.FunctionalTests/Client/HubProxyFacts.cs
Expand Up @@ -26,22 +26,24 @@ public void EndToEndTest(HostType hostType, TransportType transportType)
host.Initialize();

HubConnection hubConnection = CreateHubConnection(host);
IHubProxy proxy = hubConnection.CreateHubProxy("ChatHub");
var wh = new ManualResetEvent(false);

proxy.On("addMessage", data =>
using (hubConnection)
{
Assert.Equal("hello", data);
wh.Set();
});
IHubProxy proxy = hubConnection.CreateHubProxy("ChatHub");
var wh = new ManualResetEvent(false);

hubConnection.Start(host.Transport).Wait();
proxy.On("addMessage", data =>
{
Assert.Equal("hello", data);
wh.Set();
});

proxy.InvokeWithTimeout("Send", "hello");
hubConnection.Start(host.Transport).Wait();

Assert.True(wh.WaitOne(TimeSpan.FromSeconds(10)));
proxy.InvokeWithTimeout("Send", "hello");

hubConnection.Stop();
Assert.True(wh.WaitOne(TimeSpan.FromSeconds(10)));
}
}
}

Expand All @@ -55,20 +57,24 @@ public void HubNamesAreNotCaseSensitive(HostType hostType, TransportType transpo
host.Initialize();

HubConnection hubConnection = CreateHubConnection(host);
IHubProxy proxy = hubConnection.CreateHubProxy("chatHub");
var wh = new ManualResetEvent(false);

proxy.On("addMessage", data =>
using (hubConnection)
{
Assert.Equal("hello", data);
wh.Set();
});
IHubProxy proxy = hubConnection.CreateHubProxy("chatHub");
var wh = new ManualResetEvent(false);

proxy.On("addMessage", data =>
{
Assert.Equal("hello", data);
wh.Set();
});

hubConnection.Start(host.Transport).Wait();
hubConnection.Start(host.Transport).Wait();

proxy.InvokeWithTimeout("Send", "hello");
proxy.InvokeWithTimeout("Send", "hello");

Assert.True(wh.WaitOne(TimeSpan.FromSeconds(10)));
Assert.True(wh.WaitOne(TimeSpan.FromSeconds(10)));
}
}
}

Expand All @@ -82,10 +88,13 @@ public void UnableToCreateHubThrowsError(HostType hostType, TransportType transp
host.Initialize();

HubConnection hubConnection = CreateHubConnection(host);
IHubProxy proxy = hubConnection.CreateHubProxy("MyHub2");
using (hubConnection)
{
IHubProxy proxy = hubConnection.CreateHubProxy("MyHub2");

hubConnection.Start(host.Transport).Wait();
var ex = Assert.Throws<AggregateException>(() => proxy.InvokeWithTimeout("Send", "hello"));
hubConnection.Start(host.Transport).Wait();
var ex = Assert.Throws<AggregateException>(() => proxy.InvokeWithTimeout("Send", "hello"));
}
}
}

Expand All @@ -104,24 +113,28 @@ public void ConnectionErrorCapturesExceptionsThrownInClientHubMethod(HostType ho
host.Initialize();

var connection = CreateHubConnection(host);
var proxy = connection.CreateHubProxy("ChatHub");

proxy.On("addMessage", () =>
using (connection)
{
throw thrown;
});
var proxy = connection.CreateHubProxy("ChatHub");

connection.Error += e =>
{
caught = e;
wh.Set();
};
proxy.On("addMessage", () =>
{
throw thrown;
});

connection.Error += e =>
{
caught = e;
wh.Set();
};

connection.Start(host.Transport).Wait();
proxy.Invoke("Send", "");
connection.Start(host.Transport).Wait();
proxy.Invoke("Send", "");

Assert.True(wh.Wait(TimeSpan.FromSeconds(5)));
Assert.Equal(thrown, caught);
Assert.True(wh.Wait(TimeSpan.FromSeconds(5)));
Assert.Equal(thrown, caught);
}
}
}

Expand All @@ -138,32 +151,35 @@ public void RequestHeadersSetCorrectly(HostType hostType, TransportType transpor
host.Initialize();

HubConnection hubConnection = CreateHubConnection(host);
IHubProxy proxy = hubConnection.CreateHubProxy("ExamineHeadersHub");
var tcs = new TaskCompletionSource<object>();

proxy.On("sendHeader", (headers) =>
using (hubConnection)
{
Assert.Equal<string>("test-header", (string)headers.testHeader);
IHubProxy proxy = hubConnection.CreateHubProxy("ExamineHeadersHub");
var tcs = new TaskCompletionSource<object>();

proxy.On("sendHeader", (headers) =>
{
Assert.Equal<string>("test-header", (string)headers.testHeader);
if (transportType != TransportType.Websockets)
{
Assert.Equal<string>("referer", (string)headers.refererHeader);
}
tcs.TrySetResult(null);
});

hubConnection.Error += e => tcs.TrySetException(e);

hubConnection.Headers.Add("test-header", "test-header");
if (transportType != TransportType.Websockets)
{
Assert.Equal<string>("referer", (string)headers.refererHeader);
hubConnection.Headers.Add(System.Net.HttpRequestHeader.Referer.ToString(), "referer");
}
tcs.TrySetResult(null);
});

hubConnection.Error += e => tcs.TrySetException(e);
hubConnection.Start(host.Transport).Wait();
proxy.Invoke("Send", "Hello");

hubConnection.Headers.Add("test-header", "test-header");
if (transportType != TransportType.Websockets)
{
hubConnection.Headers.Add(System.Net.HttpRequestHeader.Referer.ToString(), "referer");
Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(10)));
}

hubConnection.Start(host.Transport).Wait();
proxy.Invoke("Send", "Hello");

Assert.True(tcs.Task.Wait(TimeSpan.FromSeconds(10)));
hubConnection.Stop();
}
}

Expand All @@ -180,15 +196,16 @@ public void RequestHeadersCannotBeSetOnceConnected(HostType hostType, TransportT
// Arrange
host.Initialize();
HubConnection hubConnection = CreateHubConnection(host);
IHubProxy proxy = hubConnection.CreateHubProxy("ExamineHeadersHub");

hubConnection.Start(host.Transport).Wait();
using (hubConnection)
{
IHubProxy proxy = hubConnection.CreateHubProxy("ExamineHeadersHub");

var ex = Assert.Throws<InvalidOperationException>(() => hubConnection.Headers.Add("test-header", "test-header"));
Assert.Equal("Request headers cannot be set after the connection has started.", ex.Message);
hubConnection.Start(host.Transport).Wait();

// Clean-up
hubConnection.Stop();
var ex = Assert.Throws<InvalidOperationException>(() => hubConnection.Headers.Add("test-header", "test-header"));
Assert.Equal("Request headers cannot be set after the connection has started.", ex.Message);
}
}
}

Expand Down

0 comments on commit 05c48e2

Please sign in to comment.