Skip to content
Browse files

Added more unit test coverage.

  • Loading branch information...
1 parent 4b5ff23 commit a0807ff4aea1847de173f3a2ca124ae5d09313a2 @davidfowl davidfowl committed Oct 30, 2012
View
31 src/Microsoft.AspNet.SignalR.Core/GroupManager.cs
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
+using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Infrastructure;
@@ -20,6 +21,11 @@ public class GroupManager : IConnectionGroupManager
/// <param name="groupPrefix">The prefix for this group. Either a <see cref="IHub"/> name or <see cref="PersistentConnection"/> type name.</param>
public GroupManager(IConnection connection, string groupPrefix)
{
+ if (connection == null)
+ {
+ throw new ArgumentNullException("connection");
+ }
+
_connection = connection;
_groupPrefix = groupPrefix;
}
@@ -32,6 +38,11 @@ public GroupManager(IConnection connection, string groupPrefix)
/// <returns>A task that represents when send is complete.</returns>
public Task Send(string groupName, object value, params string[] exclude)
{
+ if (groupName == null)
+ {
+ throw new ArgumentNullException("groupName");
+ }
+
var qualifiedName = CreateQualifiedName(groupName);
var message = new ConnectionMessage(qualifiedName, value)
{
@@ -49,6 +60,16 @@ public Task Send(string groupName, object value, params string[] exclude)
/// <returns>A task that represents the connection id being added to the group.</returns>
public Task Add(string connectionId, string groupName)
{
+ if (connectionId == null)
+ {
+ throw new ArgumentNullException("connectionId");
+ }
+
+ if (groupName == null)
+ {
+ throw new ArgumentNullException("groupName");
+ }
+
var command = new Command
{
Type = CommandType.AddToGroup,
@@ -67,6 +88,16 @@ public Task Add(string connectionId, string groupName)
/// <returns>A task that represents the connection id being removed from the group.</returns>
public Task Remove(string connectionId, string groupName)
{
+ if (connectionId == null)
+ {
+ throw new ArgumentNullException("connectionId");
+ }
+
+ if (groupName == null)
+ {
+ throw new ArgumentNullException("groupName");
+ }
+
var command = new Command
{
Type = CommandType.RemoveFromGroup,
View
153 tests/Microsoft.AspNet.SignalR.Tests/GroupManagerFacts.cs
@@ -0,0 +1,153 @@
+using System;
+using System.Linq;
+using Moq;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests
+{
+ public class GroupManagerFacts
+ {
+ public class Add
+ {
+ [Fact]
+ public void ThrowsIfConnectionIsNull()
+ {
+ Assert.Throws<ArgumentNullException>(() => new GroupManager(null, "Prefix"));
+ }
+
+ [Fact]
+ public void ThrowsIfConnectionIdIsNull()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()));
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Assert
+ Assert.Throws<ArgumentNullException>(() => groupManager.Add(null, "SomeGroup"));
+ }
+
+ [Fact]
+ public void ThrowsIfGroupIsNull()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()));
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Assert
+ Assert.Throws<ArgumentNullException>(() => groupManager.Add("1", null));
+ }
+
+ [Fact]
+ public void CreatesAddToGroupCommandOnAdd()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()))
+ .Callback<ConnectionMessage>(m =>
+ {
+ Assert.Equal("1", m.Signal);
+ Assert.NotNull(m.Value);
+ var command = m.Value as Command;
+ Assert.NotNull(command);
+ Assert.NotNull(command.Id);
+ Assert.Equal(CommandType.AddToGroup, command.Type);
+ Assert.Equal("Prefix.MyGroup", command.Value);
+ Assert.True(command.WaitForAck);
+ });
+
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Act
+ groupManager.Add("1", "MyGroup");
+ }
+ }
+
+ public class Remove
+ {
+ [Fact]
+ public void ThrowsIfConnectionIdIsNull()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()));
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Assert
+ Assert.Throws<ArgumentNullException>(() => groupManager.Remove(null, "SomeGroup"));
+ }
+
+ [Fact]
+ public void ThrowsIfGroupIsNull()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()));
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Assert
+ Assert.Throws<ArgumentNullException>(() => groupManager.Remove("1", null));
+ }
+
+ [Fact]
+ public void CreatesRemoveFromGroupCommandOnAdd()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()))
+ .Callback<ConnectionMessage>(m =>
+ {
+ Assert.Equal("1", m.Signal);
+ Assert.NotNull(m.Value);
+ var command = m.Value as Command;
+ Assert.NotNull(command);
+ Assert.NotNull(command.Id);
+ Assert.Equal(CommandType.RemoveFromGroup, command.Type);
+ Assert.Equal("Prefix.MyGroup", command.Value);
+ Assert.True(command.WaitForAck);
+ });
+
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Act
+ groupManager.Remove("1", "MyGroup");
+ }
+ }
+
+ public class Send
+ {
+ [Fact]
+ public void ThrowsIfGroupIsNull()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()));
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Assert
+ Assert.Throws<ArgumentNullException>(() => groupManager.Send(null, "Way"));
+ }
+
+ [Fact]
+ public void SendsMessageToGroup()
+ {
+ // Arrange
+ var connection = new Mock<IConnection>();
+ connection.Setup(m => m.Send(It.IsAny<ConnectionMessage>()))
+ .Callback<ConnectionMessage>(m =>
+ {
+ Assert.Equal("Prefix.MyGroup", m.Signal);
+ Assert.Equal("some value", m.Value);
+ Assert.True(m.ExcludedSignals.Contains("x"));
+ Assert.True(m.ExcludedSignals.Contains("y"));
+ });
+
+ var groupManager = new GroupManager(connection.Object, "Prefix");
+
+ // Act
+ groupManager.Send("MyGroup", "some value", "x", "y");
+ }
+ }
+ }
+}
View
47 tests/Microsoft.AspNet.SignalR.Tests/Json/JsonFacts.cs
@@ -0,0 +1,47 @@
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests.Json
+{
+ public class JsonFacts
+ {
+ [Fact]
+ public void CamelCaseConversPascalCaseToCamelCase()
+ {
+ // Act
+ string name = Microsoft.AspNet.SignalR.Json.CamelCase("SomeMethod");
+
+ // Assert
+ Assert.Equal("someMethod", name);
+ }
+
+ [Fact]
+ public void MimeTypeReturnsJsonMimeType()
+ {
+ // Act
+ string mimeType = Microsoft.AspNet.SignalR.Json.MimeType;
+
+ // Assert
+ Assert.Equal("application/json", mimeType);
+ }
+
+ [Fact]
+ public void JsonPMimeTypeReturnsJsonPMimeType()
+ {
+ // Act
+ string mimeType = Microsoft.AspNet.SignalR.Json.JsonpMimeType;
+
+ // Assert
+ Assert.Equal("text/javascript", mimeType);
+ }
+
+ [Fact]
+ public void CreateJsonPCallbackWrapsContentInMethod()
+ {
+ // Act
+ string callback = Microsoft.AspNet.SignalR.Json.CreateJsonpCallback("foo", "1");
+
+ // Assert
+ Assert.Equal("foo(1);", callback);
+ }
+ }
+}
View
26 tests/Microsoft.AspNet.SignalR.Tests/Json/JsonNetSerializerFacts.cs
@@ -0,0 +1,26 @@
+using System.IO;
+using Moq;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests.Json
+{
+ public class JsonNetSerializerFacts
+ {
+ [Fact]
+ public void SerializeInterceptsIJsonWritable()
+ {
+ // Arrange
+ var serializer = new JsonNetSerializer();
+ var sw = new StringWriter();
+ var value = new Mock<IJsonWritable>();
+ value.Setup(m => m.WriteJson(It.IsAny<TextWriter>()))
+ .Callback<TextWriter>(tw => tw.Write("Custom"));
+
+ // Act
+ serializer.Serialize(value.Object, sw);
+
+ // Assert
+ Assert.Equal("Custom", sw.ToString());
+ }
+ }
+}
View
5 tests/Microsoft.AspNet.SignalR.Tests/Microsoft.AspNet.SignalR.Tests.csproj
@@ -57,10 +57,15 @@
<Compile Include="ChunkBufferFacts.cs" />
<Compile Include="Client\ConnectionFacts.cs" />
<Compile Include="ConfigurationManagerFacts.cs" />
+ <Compile Include="GroupManagerFacts.cs" />
<Compile Include="Hosting\CookieFacts.cs" />
<Compile Include="Hosting\HostContextExtensionsFacts.cs" />
<Compile Include="Hosting\RequestExtensionsFacts.cs" />
<Compile Include="Hosting\ResponseExtensionsFacts.cs" />
+ <Compile Include="Json\JsonFacts.cs" />
+ <Compile Include="Json\JsonNetSerializerFacts.cs" />
+ <Compile Include="PersistentConnectionFactoryFacts.cs" />
+ <Compile Include="PersistentResponseFacts.cs" />
<Compile Include="Server\AckHandlerFacts.cs" />
<Compile Include="Server\Hubs\HubDispatcherFacts.cs" />
<Compile Include="Server\MessageResultExtensions.cs" />
View
76 tests/Microsoft.AspNet.SignalR.Tests/PersistentConnectionFactoryFacts.cs
@@ -0,0 +1,76 @@
+using System;
+using Moq;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests
+{
+ public class PersistentConnectionFactoryFacts
+ {
+ public class CreateInstance
+ {
+ [Fact]
+ public void ThrowsIfTypeIsNull()
+ {
+ // Arrange
+ var resolver = new Mock<IDependencyResolver>();
+ var factory = new PersistentConnectionFactory(resolver.Object);
+
+ // Act & Assert
+ Assert.Throws<ArgumentNullException>(() => factory.CreateInstance(null));
+ }
+
+ [Fact]
+ public void ThrowsIfTypeIsNotPersistentConnection()
+ {
+ // Arrange
+ var resolver = new Mock<IDependencyResolver>();
+ var factory = new PersistentConnectionFactory(resolver.Object);
+
+ // Act & Assert
+ Assert.Throws<InvalidOperationException>(() => factory.CreateInstance(typeof(PersistentConnectionFactoryFacts)));
+ }
+
+ [Fact]
+ public void CreatesInstanceIfTypeIsPersistentConnection()
+ {
+ // Arrange
+ var resolver = new Mock<IDependencyResolver>();
+ var factory = new PersistentConnectionFactory(resolver.Object);
+
+ // Act
+ PersistentConnection connection = factory.CreateInstance(typeof(MyConnection));
+
+ // Assert
+ Assert.NotNull(connection);
+ }
+
+ [Fact]
+ public void UsesDependencyResolver()
+ {
+ // Arrange
+ var resolver = new Mock<IDependencyResolver>();
+ var factory = new PersistentConnectionFactory(resolver.Object);
+ var otherConnection = new MyOtherConnection();
+ resolver.Setup(m => m.GetService(typeof(MyConnection)))
+ .Returns(otherConnection);
+
+ // Act
+ PersistentConnection connection = factory.CreateInstance(typeof(MyConnection));
+
+ // Assert
+ Assert.NotNull(connection);
+ Assert.Same(otherConnection, connection);
+ }
+
+ public class MyOtherConnection : MyConnection
+ {
+
+ }
+
+ public class MyConnection : PersistentConnection
+ {
+
+ }
+ }
+ }
+}
View
93 tests/Microsoft.AspNet.SignalR.Tests/PersistentResponseFacts.cs
@@ -0,0 +1,93 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using Xunit;
+
+namespace Microsoft.AspNet.SignalR.Tests
+{
+ public class PersistentResponseFacts
+ {
+ [Fact]
+ public void WriteJsonWritesMessagesThatAreNotExcluded()
+ {
+ // Arrange
+ var writer = new StringWriter();
+ var response = new PersistentResponse(m => m.Key == "key2");
+ response.Messages = new List<ArraySegment<Message>>();
+ response.TransportData = new Dictionary<string, object>();
+ response.TransportData["Groups"] = new List<string>
+ {
+ "g1"
+ };
+ response.MessageId = "Baz";
+ response.Messages.Add(new ArraySegment<Message>(new[] { new Message("1", "key", "value1"),
+ new Message("1", "key2", "value2") }, 0, 2));
+
+ // Act
+ ((IJsonWritable)response).WriteJson(writer);
+
+ // Assert
+ Assert.Equal(@"{""MessageId"":""Baz"",""Disconnect"":false,""TimedOut"":false,""TransportData"":{""Groups"":[""g1""]},""Messages"":[value1]}", writer.ToString());
+ }
+
+ [Fact]
+ public void WriteJsonWritesSkipsCommands()
+ {
+ // Arrange
+ var writer = new StringWriter();
+ var response = new PersistentResponse(m => false);
+ response.Messages = new List<ArraySegment<Message>>();
+ response.TransportData = new Dictionary<string, object>();
+ response.TransportData["Groups"] = new List<string>
+ {
+ "g1"
+ };
+ response.MessageId = "Baz";
+ response.Messages.Add(new ArraySegment<Message>(new[] { new Message("1", "key", "value1") { CommandId = "something"},
+ new Message("1", "key2", "value2") }, 0, 2));
+
+ // Act
+ ((IJsonWritable)response).WriteJson(writer);
+
+ // Assert
+ Assert.Equal(@"{""MessageId"":""Baz"",""Disconnect"":false,""TimedOut"":false,""TransportData"":{""Groups"":[""g1""]},""Messages"":[value2]}", writer.ToString());
+ }
+
+ [Fact]
+ public void WriteJsonSkipsNullGroups()
+ {
+ // Arrange
+ var writer = new StringWriter();
+ var response = new PersistentResponse(m => m.Key == "key2");
+ response.TransportData = new Dictionary<string, object>();
+ response.Messages = new List<ArraySegment<Message>>();
+ response.MessageId = "Baz";
+ response.Messages.Add(new ArraySegment<Message>(new[] { new Message("1", "key", "value1"),
+ new Message("1", "key2", "value2") }, 0, 2));
+
+ // Act
+ ((IJsonWritable)response).WriteJson(writer);
+
+ // Assert
+ Assert.Equal(@"{""MessageId"":""Baz"",""Disconnect"":false,""TimedOut"":false,""TransportData"":{},""Messages"":[value1]}", writer.ToString());
+ }
+
+ [Fact]
+ public void WriteJsonSkipsNullTransportDaa()
+ {
+ // Arrange
+ var writer = new StringWriter();
+ var response = new PersistentResponse(m => m.Key == "key2");
+ response.Messages = new List<ArraySegment<Message>>();
+ response.MessageId = "Baz";
+ response.Messages.Add(new ArraySegment<Message>(new[] { new Message("1", "key", "value1"),
+ new Message("1", "key2", "value2") }, 0, 2));
+
+ // Act
+ ((IJsonWritable)response).WriteJson(writer);
+
+ // Assert
+ Assert.Equal(@"{""MessageId"":""Baz"",""Disconnect"":false,""TimedOut"":false,""Messages"":[value1]}", writer.ToString());
+ }
+ }
+}

0 comments on commit a0807ff

Please sign in to comment.
Something went wrong with that request. Please try again.