Skip to content

Commit 57b2bcd

Browse files
committed
start testing
1 parent dc00a2a commit 57b2bcd

File tree

4 files changed

+190
-2
lines changed

4 files changed

+190
-2
lines changed

src/SignalR/server/Core/src/Internal/DefaultHubMessageSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public DefaultHubMessageSerializer(IHubProtocolResolver hubProtocolResolver, IOp
2525
}
2626
}
2727

28+
// REVIEW: Do we care about removing dupes, since that's a very unlikely scenario?
2829
var additionalProtocols = hubOptions.Value.AdditionalHubProtocols ?? globalHubOptions.Value.AdditionalHubProtocols ?? Array.Empty<IHubProtocol>();
2930
foreach (var protocol in additionalProtocols)
3031
{

src/SignalR/server/SignalR/test/AddSignalRTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void ServicesAddedBeforeAddSignalRAreUsed()
2929
serviceCollection.AddSingleton(typeof(IHubContext<,>), typeof(CustomHubContext<,>));
3030
var hubOptions = new HubOptionsSetup(new List<IHubProtocol>());
3131
serviceCollection.AddSingleton<IConfigureOptions<HubOptions>>(hubOptions);
32+
serviceCollection.AddSingleton(typeof(IHubMessageSerializer<>), typeof(CustomHubMessageSerializer<>));
3233
serviceCollection.AddSignalR();
3334

3435
var serviceProvider = serviceCollection.BuildServiceProvider();
@@ -39,6 +40,7 @@ public void ServicesAddedBeforeAddSignalRAreUsed()
3940
Assert.IsType<CustomHubContext<CustomHub>>(serviceProvider.GetRequiredService<IHubContext<CustomHub>>());
4041
Assert.IsType<CustomHubContext<CustomTHub, string>>(serviceProvider.GetRequiredService<IHubContext<CustomTHub, string>>());
4142
Assert.IsType<CustomHubContext<CustomDynamicHub>>(serviceProvider.GetRequiredService<IHubContext<CustomDynamicHub>>());
43+
Assert.IsType<CustomHubMessageSerializer<CustomDynamicHub>>(serviceProvider.GetRequiredService<IHubMessageSerializer<CustomDynamicHub>>());
4244
Assert.Equal(hubOptions, serviceProvider.GetRequiredService<IConfigureOptions<HubOptions>>());
4345
Assert.Equal(markerService, serviceProvider.GetRequiredService<SignalRCoreMarkerService>());
4446
}
@@ -55,6 +57,7 @@ public void ServicesAddedAfterAddSignalRAreUsed()
5557
serviceCollection.AddScoped(typeof(IHubActivator<>), typeof(CustomHubActivator<>));
5658
serviceCollection.AddSingleton(typeof(IHubContext<>), typeof(CustomHubContext<>));
5759
serviceCollection.AddSingleton(typeof(IHubContext<,>), typeof(CustomHubContext<,>));
60+
serviceCollection.AddSingleton(typeof(IHubMessageSerializer<>), typeof(CustomHubMessageSerializer<>));
5861

5962
var serviceProvider = serviceCollection.BuildServiceProvider();
6063
Assert.IsType<CustomIdProvider>(serviceProvider.GetRequiredService<IUserIdProvider>());
@@ -64,6 +67,7 @@ public void ServicesAddedAfterAddSignalRAreUsed()
6467
Assert.IsType<CustomHubContext<CustomHub>>(serviceProvider.GetRequiredService<IHubContext<CustomHub>>());
6568
Assert.IsType<CustomHubContext<CustomTHub, string>>(serviceProvider.GetRequiredService<IHubContext<CustomTHub, string>>());
6669
Assert.IsType<CustomHubContext<CustomDynamicHub>>(serviceProvider.GetRequiredService<IHubContext<CustomDynamicHub>>());
70+
Assert.IsType<CustomHubMessageSerializer<CustomDynamicHub>>(serviceProvider.GetRequiredService<IHubMessageSerializer<CustomDynamicHub>>());
6771
}
6872

6973
[Fact]
@@ -74,11 +78,15 @@ public void HubSpecificOptionsDoNotAffectGlobalHubOptions()
7478
serviceCollection.AddSignalR().AddHubOptions<CustomHub>(options =>
7579
{
7680
options.SupportedProtocols.Clear();
81+
options.AdditionalHubProtocols.Add(new JsonHubProtocol());
7782
});
7883

7984
var serviceProvider = serviceCollection.BuildServiceProvider();
8085
Assert.Equal(1, serviceProvider.GetRequiredService<IOptions<HubOptions>>().Value.SupportedProtocols.Count);
8186
Assert.Equal(0, serviceProvider.GetRequiredService<IOptions<HubOptions<CustomHub>>>().Value.SupportedProtocols.Count);
87+
88+
Assert.Equal(0, serviceProvider.GetRequiredService<IOptions<HubOptions>>().Value.AdditionalHubProtocols.Count);
89+
Assert.Equal(1, serviceProvider.GetRequiredService<IOptions<HubOptions<CustomHub>>>().Value.AdditionalHubProtocols.Count);
8290
}
8391

8492
[Fact]
@@ -222,4 +230,12 @@ public override Task SendUsersAsync(IReadOnlyList<string> userIds, string method
222230
throw new System.NotImplementedException();
223231
}
224232
}
233+
234+
public class CustomHubMessageSerializer<THub> : IHubMessageSerializer<THub> where THub : Hub
235+
{
236+
public SerializedHubMessage SerializeMessage(HubMessage message)
237+
{
238+
throw new System.NotImplementedException();
239+
}
240+
}
225241
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using Microsoft.AspNetCore.SignalR.Internal;
8+
using Microsoft.AspNetCore.SignalR.Protocol;
9+
using Microsoft.Extensions.Logging.Abstractions;
10+
using Microsoft.Extensions.Options;
11+
using Xunit;
12+
13+
namespace Microsoft.AspNetCore.SignalR.Tests.Internal
14+
{
15+
public class DefaultHubMessageSerializerTests
16+
{
17+
[Theory]
18+
[MemberData(nameof(InvocationTestData))]
19+
public void SerializeMessages(string testName)
20+
{
21+
var testData = _invocationTestData[testName];
22+
23+
var resolver = CreateHubProtocolResolver(new List<IHubProtocol> { new MessagePackHubProtocol(), new JsonHubProtocol() });
24+
var protocolNames = new List<string>();
25+
foreach (var protocol in testData.SupportedHubProtocols)
26+
{
27+
protocolNames.Add(protocol.Name);
28+
}
29+
var serializer = new DefaultHubMessageSerializer<Hub>(resolver, Options.Create(new HubOptions() { SupportedProtocols = protocolNames, AdditionalHubProtocols = testData.AdditionalHubProtocols }), Options.Create(new HubOptions<Hub>()));
30+
var serializedHubMessage = serializer.SerializeMessage(_testMessage);
31+
32+
var serializedMessages = serializedHubMessage.GetAllSerializations().ToList();
33+
34+
var allBytes = new List<byte>();
35+
Assert.Equal(testData.SupportedHubProtocols.Count + testData.AdditionalHubProtocols.Count, serializedMessages.Count);
36+
foreach (var message in serializedMessages)
37+
{
38+
allBytes.AddRange(message.Serialized.ToArray());
39+
}
40+
41+
Assert.Equal(testData.Encoded, allBytes);
42+
}
43+
44+
private IHubProtocolResolver CreateHubProtocolResolver(List<IHubProtocol> hubProtocols)
45+
{
46+
return new DefaultHubProtocolResolver(hubProtocols, NullLogger<DefaultHubProtocolResolver>.Instance);
47+
}
48+
49+
// We use a func so we are guaranteed to get a new SerializedHubMessage for each test
50+
private static Dictionary<string, ProtocolTestData> _invocationTestData = new[]
51+
{
52+
new ProtocolTestData(
53+
"Single supported protocol",
54+
new List<IHubProtocol>() { new MessagePackHubProtocol() },
55+
new List<IHubProtocol>(),
56+
0x0D,
57+
0x96,
58+
0x01,
59+
0x80,
60+
0xC0,
61+
0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t',
62+
0x90,
63+
0x90),
64+
new ProtocolTestData(
65+
"Supported protocol with same additional protocol",
66+
new List<IHubProtocol>() { new MessagePackHubProtocol() },
67+
new List<IHubProtocol>() { new MessagePackHubProtocol() },
68+
0x0D,
69+
0x96,
70+
0x01,
71+
0x80,
72+
0xC0,
73+
0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t',
74+
0x90,
75+
0x90,
76+
0x0D,
77+
0x96,
78+
0x01,
79+
0x80,
80+
0xC0,
81+
0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t',
82+
0x90,
83+
0x90),
84+
new ProtocolTestData(
85+
"Supported protocol with different additional protocol",
86+
new List<IHubProtocol>() { new MessagePackHubProtocol() },
87+
new List<IHubProtocol>() { new JsonHubProtocol() },
88+
0x0D,
89+
0x96,
90+
0x01,
91+
0x80,
92+
0xC0,
93+
0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t',
94+
0x90,
95+
0x90,
96+
(byte)'{', (byte)'"', (byte)'t', (byte)'y', (byte)'p', (byte)'e', (byte)'"', (byte)':', (byte)'1',
97+
(byte)',',(byte)'"', (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', (byte)'"', (byte)':',
98+
(byte)'"', (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t', (byte)'"',
99+
(byte)',', (byte)'"', (byte)'a', (byte)'r', (byte)'g', (byte)'u', (byte)'m', (byte)'e', (byte)'n', (byte)'t', (byte)'s', (byte)'"',
100+
(byte)':', (byte)'[', (byte)']', (byte)'}', 0x1e),
101+
new ProtocolTestData(
102+
"No supported protocol with additional protocol",
103+
new List<IHubProtocol>(),
104+
new List<IHubProtocol>() { new MessagePackHubProtocol() },
105+
0x0D,
106+
0x96,
107+
0x01,
108+
0x80,
109+
0xC0,
110+
0xA6, (byte)'t', (byte)'a', (byte)'r', (byte)'g', (byte)'e', (byte)'t',
111+
0x90,
112+
0x90),
113+
new ProtocolTestData(
114+
"No protocols",
115+
new List<IHubProtocol>(),
116+
new List<IHubProtocol>()),
117+
}.ToDictionary(t => t.Name);
118+
119+
public static IEnumerable<object[]> InvocationTestData = _invocationTestData.Keys.Select(k => new object[] { k });
120+
121+
public class ProtocolTestData
122+
{
123+
public string Name { get; }
124+
public byte[] Encoded { get; }
125+
public List<IHubProtocol> AdditionalHubProtocols { get; }
126+
public List<IHubProtocol> SupportedHubProtocols { get; }
127+
128+
public ProtocolTestData(string name, List<IHubProtocol> supportedHubProtocols, List<IHubProtocol> additionalHubProtocols, params byte[] encoded)
129+
{
130+
Name = name;
131+
Encoded = encoded;
132+
AdditionalHubProtocols = additionalHubProtocols;
133+
SupportedHubProtocols = supportedHubProtocols;
134+
}
135+
}
136+
137+
// The actual invocation message doesn't matter
138+
private static InvocationMessage _testMessage = new InvocationMessage("target", Array.Empty<object>());
139+
}
140+
}

src/SignalR/server/StackExchangeRedis/test/RedisProtocolTests.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Buffers;
65
using System.Collections.Generic;
76
using System.Linq;
7+
using Microsoft.AspNetCore.SignalR.Internal;
88
using Microsoft.AspNetCore.SignalR.Protocol;
99
using Microsoft.AspNetCore.SignalR.StackExchangeRedis.Internal;
1010
using Microsoft.AspNetCore.SignalR.Tests;
11+
using Microsoft.Extensions.Logging.Abstractions;
12+
using Microsoft.Extensions.Options;
1113
using Xunit;
1214

1315
namespace Microsoft.AspNetCore.SignalR.StackExchangeRedis.Tests
@@ -169,7 +171,23 @@ public void ParseInvocation(string testName)
169171
public void WriteInvocation(string testName)
170172
{
171173
var testData = _invocationTestData[testName];
172-
var protocol = new RedisProtocol<EchoHub>(new[] { new DummyHubProtocol("p1"), new DummyHubProtocol("p2") });
174+
var protocol = new RedisProtocol<EchoHub>(new [] { new DummyHubProtocol("p1"), new DummyHubProtocol("p2") });
175+
176+
// Actual invocation doesn't matter because we're using a dummy hub protocol.
177+
// But the dummy protocol will check that we gave it the test message to make sure everything flows through properly.
178+
var expected = testData.Decoded();
179+
var encoded = protocol.WriteInvocation(_testMessage.Target, _testMessage.Arguments, expected.ExcludedConnectionIds);
180+
181+
Assert.Equal(testData.Encoded, encoded);
182+
}
183+
184+
[Theory]
185+
[MemberData(nameof(InvocationTestData))]
186+
public void WriteInvocationWithHubMessageSerializer(string testName)
187+
{
188+
var testData = _invocationTestData[testName];
189+
var hubMessageSerializer = CreateHubMessageSerializer(new List<IHubProtocol>() { new DummyHubProtocol("p1"), new DummyHubProtocol("p2") });
190+
var protocol = new RedisProtocol<Hub>(hubMessageSerializer);
173191

174192
// Actual invocation doesn't matter because we're using a dummy hub protocol.
175193
// But the dummy protocol will check that we gave it the test message to make sure everything flows through properly.
@@ -196,5 +214,18 @@ public ProtocolTestData(string name, T decoded, byte[] encoded)
196214
Encoded = encoded;
197215
}
198216
}
217+
218+
private IHubMessageSerializer<Hub> CreateHubMessageSerializer(List<IHubProtocol> protocols)
219+
{
220+
var hubTypeOptions = Options.Create(new HubOptions<Hub>()
221+
{
222+
AdditionalHubProtocols = protocols
223+
});
224+
var globalHubOptions = Options.Create(new HubOptions());
225+
226+
var protocolResolver = new DefaultHubProtocolResolver(new List<IHubProtocol>(), NullLogger<DefaultHubProtocolResolver>.Instance);
227+
228+
return new DefaultHubMessageSerializer<Hub>(protocolResolver, globalHubOptions, hubTypeOptions);
229+
}
199230
}
200231
}

0 commit comments

Comments
 (0)