Skip to content

Commit

Permalink
Fix non-serializable tests (#1820)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Apr 2, 2018
1 parent 5ce672d commit 3460d44
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ private void WriteStreamInvocationMessage(StreamInvocationMessage message, JsonT

private void WriteCloseMessage(CloseMessage message, JsonTextWriter writer)
{
if (!string.IsNullOrEmpty(message.Error))
if (message.Error != null)
{
writer.WritePropertyName(ErrorPropertyName);
writer.WriteValue(message.Error);
Expand Down Expand Up @@ -615,7 +615,8 @@ private object[] BindArguments(JsonTextReader reader, IReadOnlyList<Type> paramT

private CloseMessage BindCloseMessage(string error)
{
if (string.IsNullOrEmpty(error))
// An empty string is still an error
if (error == null)
{
return CloseMessage.Empty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public WebSocketsTests(ITestOutputHelper output)
{
}

// Using nameof with WebSocketMessageType because it is a GACed type and xunit can't serialize it
[Theory]
[InlineData(WebSocketMessageType.Text)]
[InlineData(WebSocketMessageType.Binary)]
public async Task ReceivedFramesAreWrittenToChannel(WebSocketMessageType webSocketMessageType)
[InlineData(nameof(WebSocketMessageType.Text))]
[InlineData(nameof(WebSocketMessageType.Binary))]
public async Task ReceivedFramesAreWrittenToChannel(string webSocketMessageType)
{
using (StartLog(out var loggerFactory, LogLevel.Debug))
{
Expand All @@ -48,7 +49,7 @@ public async Task ReceivedFramesAreWrittenToChannel(WebSocketMessageType webSock
// Send a frame, then close
await feature.Client.SendAsync(
buffer: new ArraySegment<byte>(Encoding.UTF8.GetBytes("Hello")),
messageType: webSocketMessageType,
messageType: Enum.Parse<WebSocketMessageType>(webSocketMessageType),
endOfMessage: true,
cancellationToken: CancellationToken.None);
await feature.Client.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
Expand All @@ -71,10 +72,11 @@ await feature.Client.SendAsync(
}
}

// Using nameof with WebSocketMessageType because it is a GACed type and xunit can't serialize it
[Theory]
[InlineData(TransferFormat.Text, WebSocketMessageType.Text)]
[InlineData(TransferFormat.Binary, WebSocketMessageType.Binary)]
public async Task WebSocketTransportSetsMessageTypeBasedOnTransferFormatFeature(TransferFormat transferFormat, WebSocketMessageType expectedMessageType)
[InlineData(TransferFormat.Text, nameof(WebSocketMessageType.Text))]
[InlineData(TransferFormat.Binary, nameof(WebSocketMessageType.Binary))]
public async Task WebSocketTransportSetsMessageTypeBasedOnTransferFormatFeature(TransferFormat transferFormat, string expectedMessageType)
{
using (StartLog(out var loggerFactory, LogLevel.Debug))
{
Expand Down Expand Up @@ -104,7 +106,7 @@ public async Task WebSocketTransportSetsMessageTypeBasedOnTransferFormatFeature(

Assert.Equal(1, clientSummary.Received.Count);
Assert.True(clientSummary.Received[0].EndOfMessage);
Assert.Equal(expectedMessageType, clientSummary.Received[0].MessageType);
Assert.Equal(Enum.Parse<WebSocketMessageType>(expectedMessageType), clientSummary.Received[0].MessageType);
Assert.Equal("Hello", Encoding.UTF8.GetString(clientSummary.Received[0].Buffer));
}
}
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public bool Equals(HubMessage x, HubMessage y)
case PingMessage pingMessage:
// If the types are equal (above), then we're done.
return true;
case CloseMessage closeMessage:
return string.Equals(closeMessage.Error, ((CloseMessage) y).Error);
default:
throw new InvalidOperationException($"Unknown message type: {x.GetType().FullName}");
}
Expand Down
16 changes: 10 additions & 6 deletions test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisEndToEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ public RedisEndToEndTests(RedisServerFixture<Startup> serverFixture, ITestOutput
[ConditionalTheory()]
[SkipIfDockerNotPresent]
[MemberData(nameof(TransportTypesAndProtocolTypes))]
public async Task HubConnectionCanSendAndReceiveMessages(TransportType transportType, IHubProtocol protocol)
public async Task HubConnectionCanSendAndReceiveMessages(TransportType transportType, string protocolName)
{
using (StartLog(out var loggerFactory, testName:
$"{nameof(HubConnectionCanSendAndReceiveMessages)}_{transportType.ToString()}_{protocol.Name}"))
$"{nameof(HubConnectionCanSendAndReceiveMessages)}_{transportType.ToString()}_{protocolName}"))
{
var protocol = HubProtocolHelpers.GetHubProtocol(protocolName);

var connection = CreateConnection(_serverFixture.FirstServer.Url + "/echo", transportType, protocol, loggerFactory);

await connection.StartAsync().OrTimeout();
Expand All @@ -59,11 +61,13 @@ public async Task HubConnectionCanSendAndReceiveMessages(TransportType transport
[ConditionalTheory()]
[SkipIfDockerNotPresent]
[MemberData(nameof(TransportTypesAndProtocolTypes))]
public async Task HubConnectionCanSendAndReceiveGroupMessages(TransportType transportType, IHubProtocol protocol)
public async Task HubConnectionCanSendAndReceiveGroupMessages(TransportType transportType, string protocolName)
{
using (StartLog(out var loggerFactory, testName:
$"{nameof(HubConnectionCanSendAndReceiveGroupMessages)}_{transportType.ToString()}_{protocol.Name}"))
$"{nameof(HubConnectionCanSendAndReceiveGroupMessages)}_{transportType.ToString()}_{protocolName}"))
{
var protocol = HubProtocolHelpers.GetHubProtocol(protocolName);

var connection = CreateConnection(_serverFixture.FirstServer.Url + "/echo", transportType, protocol, loggerFactory);
var secondConnection = CreateConnection(_serverFixture.SecondServer.Url + "/echo", transportType, protocol, loggerFactory);

Expand Down Expand Up @@ -111,11 +115,11 @@ public static IEnumerable<object[]> TransportTypesAndProtocolTypes
{
foreach (var transport in TransportTypes())
{
yield return new object[] { transport, new JsonHubProtocol() };
yield return new object[] { transport, JsonHubProtocol.ProtocolName };

if (transport != TransportType.ServerSentEvents)
{
yield return new object[] { transport, new MessagePackHubProtocol() };
yield return new object[] { transport, MessagePackHubProtocol.ProtocolName };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.SignalR.Internal.Protocol;

namespace Microsoft.AspNetCore.SignalR.Tests
{
public static class HubProtocolHelpers
{
private static readonly IHubProtocol JsonHubProtocol = new JsonHubProtocol();

private static readonly IHubProtocol MessagePackHubProtocol = new MessagePackHubProtocol();

public static readonly List<string> AllProtocolNames = new List<string>
{
JsonHubProtocol.Name,
MessagePackHubProtocol.Name
};

public static readonly IList<IHubProtocol> AllProtocols = new List<IHubProtocol>()
{
JsonHubProtocol,
MessagePackHubProtocol
};

public static IHubProtocol GetHubProtocol(string name)
{
var protocol = AllProtocols.SingleOrDefault(p => p.Name == name);
if (protocol == null)
{
throw new InvalidOperationException($"Could not find protocol with name '{name}'.");
}

return protocol;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Common\Microsoft.AspNetCore.SignalR.Common.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Core\Microsoft.AspNetCore.SignalR.Core.csproj" />
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SignalR.Protocols.MsgPack\Microsoft.AspNetCore.SignalR.Protocols.MsgPack.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,10 @@ public async Task DelayedSendTest()

[Theory]
[MemberData(nameof(StreamingMethodAndHubProtocols))]
public async Task HubsCanStreamResponses(string method, IHubProtocol protocol)
public async Task HubsCanStreamResponses(string method, string protocolName)
{
var protocol = HubProtocolHelpers.GetHubProtocol(protocolName);

var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider();
var connectionHandler = serviceProvider.GetService<HubConnectionHandler<StreamingHub>>();
var invocationBinder = new Mock<IInvocationBinder>();
Expand Down Expand Up @@ -1692,9 +1694,9 @@ public static IEnumerable<object[]> StreamingMethodAndHubProtocols
nameof(StreamingHub.CounterObservable), nameof(StreamingHub.CounterObservableAsync), nameof(StreamingHub.CounterObservableValueTaskAsync)
})
{
foreach (var protocol in new IHubProtocol[] { new JsonHubProtocol(), new MessagePackHubProtocol() })
foreach (var protocolName in HubProtocolHelpers.AllProtocolNames)
{
yield return new object[] { method, protocol };
yield return new object[] { method, protocolName };
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.SignalR.Internal;
using Microsoft.AspNetCore.SignalR.Internal.Protocol;
using Microsoft.AspNetCore.SignalR.Tests;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
Expand All @@ -15,42 +17,39 @@ namespace Microsoft.AspNetCore.SignalR.Common.Protocol.Tests
{
public class DefaultHubProtocolResolverTests
{
private static readonly List<string> AllProtocolNames = new List<string> { "json", "messagepack" };

private static readonly IList<IHubProtocol> AllProtocols = new List<IHubProtocol>()
{
new JsonHubProtocol(),
new MessagePackHubProtocol()
};


[Theory]
[MemberData(nameof(HubProtocols))]
public void DefaultHubProtocolResolverTestsCanCreateAllProtocols(IHubProtocol protocol)
[MemberData(nameof(HubProtocolNames))]
public void DefaultHubProtocolResolverTestsCanCreateAllProtocols(string protocolName)
{
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
var protocol = HubProtocolHelpers.GetHubProtocol(protocolName);

var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.IsType(
protocol.GetType(),
resolver.GetProtocol(protocol.Name, AllProtocolNames));
resolver.GetProtocol(protocol.Name, HubProtocolHelpers.AllProtocolNames));
}

[Theory]
[MemberData(nameof(HubProtocols))]
public void DefaultHubProtocolResolverCreatesProtocolswhenSupoortedProtocolsIsNull(IHubProtocol protocol)
[MemberData(nameof(HubProtocolNames))]
public void DefaultHubProtocolResolverCreatesProtocolswhenSupoortedProtocolsIsNull(string protocolName)
{
var protocol = HubProtocolHelpers.GetHubProtocol(protocolName);

List<string> supportedProtocols = null;
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.IsType(
protocol.GetType(),
resolver.GetProtocol(protocol.Name, supportedProtocols));
}

[Theory]
[MemberData(nameof(HubProtocols))]
public void DefaultHubProtocolResolverTestsCanCreateSupportedProtocols(IHubProtocol protocol)
[MemberData(nameof(HubProtocolNames))]
public void DefaultHubProtocolResolverTestsCanCreateSupportedProtocols(string protocolName)
{
var protocol = HubProtocolHelpers.GetHubProtocol(protocolName);

var supportedProtocols = new List<string> { protocol.Name };
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.IsType(
protocol.GetType(),
resolver.GetProtocol(protocol.Name, supportedProtocols));
Expand All @@ -59,18 +58,18 @@ public void DefaultHubProtocolResolverTestsCanCreateSupportedProtocols(IHubProto
[Fact]
public void DefaultHubProtocolResolverThrowsForNullProtocol()
{
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
var exception = Assert.Throws<ArgumentNullException>(
() => resolver.GetProtocol(null, AllProtocolNames));
() => resolver.GetProtocol(null, HubProtocolHelpers.AllProtocolNames));

Assert.Equal("protocolName", exception.ParamName);
}

[Fact]
public void DefaultHubProtocolResolverReturnsNullForNotSupportedProtocol()
{
var resolver = new DefaultHubProtocolResolver(AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.Null(resolver.GetProtocol("notARealProtocol", AllProtocolNames));
var resolver = new DefaultHubProtocolResolver(HubProtocolHelpers.AllProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
Assert.Null(resolver.GetProtocol("notARealProtocol", HubProtocolHelpers.AllProtocolNames));
}

[Fact]
Expand All @@ -84,11 +83,6 @@ public void RegisteringMultipleHubProtocolsFails()
Assert.Equal($"Multiple Hub Protocols with the name '{JsonHubProtocol.ProtocolName}' were registered.", exception.Message);
}

public static IEnumerable<object[]> HubProtocols =>
new[]
{
new object[] { new JsonHubProtocol() },
new object[] { new MessagePackHubProtocol() },
};
public static IEnumerable<object[]> HubProtocolNames => HubProtocolHelpers.AllProtocols.Select(p => new object[] {p.Name});
}
}

0 comments on commit 3460d44

Please sign in to comment.