Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit ca7c7d6

Browse files
committed
Added tests
1 parent 5806fd4 commit ca7c7d6

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

src/Microsoft.AspNetCore.SignalR.Core/DynamicHubClients.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public DynamicHubClients(IHubCallerClients clients)
1818
public dynamic AllExcept(IReadOnlyList<string> excludedIds) => new DynamicClientProxy(_clients.AllExcept(excludedIds));
1919
public dynamic Caller => new DynamicClientProxy(_clients.Caller);
2020
public dynamic Client(string connectionId) => new DynamicClientProxy(_clients.Client(connectionId));
21-
public dynamic Group(string group) => new DynamicClientProxy(_clients.Group(group));
21+
public dynamic Group(string groupName) => new DynamicClientProxy(_clients.Group(groupName));
22+
public dynamic GroupExcept(string groupName, IReadOnlyList<string> excludedIds) => new DynamicClientProxy(_clients.GroupExcept(groupName, excludedIds));
2223
public dynamic Others => new DynamicClientProxy(_clients.Others);
2324
public dynamic User(string userId) => new DynamicClientProxy(_clients.User(userId));
2425
}

test/Microsoft.AspNetCore.SignalR.Redis.Tests/RedisHubLifetimeManagerTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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.Collections.Generic;
56
using System.Threading;
67
using System.Threading.Channels;
78
using System.Threading.Tasks;
@@ -102,6 +103,37 @@ public async Task InvokeGroupAsyncWritesToAllConnectionsInGroupOutput()
102103
}
103104
}
104105

106+
[Fact]
107+
public async Task InvokeGroupExceptAsyncWritesToAllValidConnectionsInGroupOutput()
108+
{
109+
using (var client1 = new TestClient())
110+
using (var client2 = new TestClient())
111+
{
112+
var manager = new RedisHubLifetimeManager<MyHub>(new LoggerFactory().CreateLogger<RedisHubLifetimeManager<MyHub>>(),
113+
Options.Create(new RedisOptions()
114+
{
115+
Factory = t => new TestConnectionMultiplexer()
116+
}));
117+
var connection1 = HubConnectionContextUtils.Create(client1.Connection);
118+
var connection2 = HubConnectionContextUtils.Create(client2.Connection);
119+
120+
await manager.OnConnectedAsync(connection1).OrTimeout();
121+
await manager.OnConnectedAsync(connection2).OrTimeout();
122+
123+
await manager.AddGroupAsync(connection1.ConnectionId, "gunit").OrTimeout();
124+
await manager.AddGroupAsync(connection2.ConnectionId, "gunit").OrTimeout();
125+
126+
var excludedIds = new List<string>{client2.Connection.ConnectionId };
127+
await manager.InvokeGroupExceptAsync("gunit", "Hello", new object[] { "World" }, excludedIds).OrTimeout();
128+
129+
await AssertMessageAsync(client1);
130+
131+
await connection1.DisposeAsync().OrTimeout();
132+
await connection2.DisposeAsync().OrTimeout();
133+
Assert.Null(client2.TryRead());
134+
}
135+
}
136+
105137
[Fact]
106138
public async Task InvokeConnectionAsyncWritesToConnectionOutput()
107139
{

test/Microsoft.AspNetCore.SignalR.Tests/HubEndpointTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,58 @@ public async Task HubsCanAddAndSendToGroup(Type hubType)
992992
}
993993
}
994994

995+
[Theory]
996+
[MemberData(nameof(HubTypes))]
997+
public async Task SendToGroupExcept(Type hubType)
998+
{
999+
var serviceProvider = CreateServiceProvider();
1000+
1001+
dynamic endPoint = serviceProvider.GetService(GetEndPointType(hubType));
1002+
1003+
using (var firstClient = new TestClient())
1004+
using (var secondClient = new TestClient())
1005+
{
1006+
Task firstEndPointTask = endPoint.OnConnectedAsync(firstClient.Connection);
1007+
Task secondEndPointTask = endPoint.OnConnectedAsync(secondClient.Connection);
1008+
1009+
await Task.WhenAll(firstClient.Connected, secondClient.Connected).OrTimeout();
1010+
1011+
var result = (await firstClient.InvokeAsync("GroupExceptSendMethod", "testGroup", "test").OrTimeout()).Result;
1012+
1013+
// check that 'firstConnection' hasn't received the group send
1014+
Assert.Null(firstClient.TryRead());
1015+
1016+
// check that 'secondConnection' hasn't received the group send
1017+
Assert.Null(secondClient.TryRead());
1018+
1019+
await firstClient.InvokeAsync(nameof(MethodHub.GroupAddMethod), "testGroup").OrTimeout();
1020+
await secondClient.InvokeAsync(nameof(MethodHub.GroupAddMethod), "testGroup").OrTimeout();
1021+
1022+
var excludedIds = new List<string> { firstClient.Connection.ConnectionId };
1023+
1024+
await firstClient.SendInvocationAsync("GroupExceptSendMethod", "testGroup", "test", excludedIds).OrTimeout();
1025+
1026+
// check that 'secondConnection' has received the group send
1027+
var hubMessage = await secondClient.ReadAsync().OrTimeout();
1028+
var invocation = Assert.IsType<InvocationMessage>(hubMessage);
1029+
Assert.Equal("Send", invocation.Target);
1030+
Assert.Single(invocation.Arguments);
1031+
Assert.Equal("test", invocation.Arguments[0]);
1032+
1033+
// Check that first client only got the completion message
1034+
hubMessage = await firstClient.ReadAsync().OrTimeout();
1035+
Assert.IsType<CompletionMessage>(hubMessage);
1036+
1037+
Assert.Null(firstClient.TryRead());
1038+
1039+
// kill the connections
1040+
firstClient.Dispose();
1041+
secondClient.Dispose();
1042+
1043+
await Task.WhenAll(firstEndPointTask, secondEndPointTask).OrTimeout();
1044+
}
1045+
}
1046+
9951047
[Fact]
9961048
public async Task RemoveFromGroupWhenNotInGroupDoesNotFail()
9971049
{
@@ -1626,6 +1678,11 @@ public Task GroupSendMethod(string groupName, string message)
16261678
return Clients.Group(groupName).Send(message);
16271679
}
16281680

1681+
public Task GroupExceptSendMethod(string groupName, string message, IReadOnlyList<string> excludedIds)
1682+
{
1683+
return Clients.GroupExcept(groupName, excludedIds).Send(message);
1684+
}
1685+
16291686
public Task BroadcastMethod(string message)
16301687
{
16311688
return Clients.All.Broadcast(message);
@@ -1814,6 +1871,11 @@ public Task GroupSendMethod(string groupName, string message)
18141871
return Clients.Group(groupName).Send(message);
18151872
}
18161873

1874+
public Task GroupExceptSendMethod(string groupName, string message, IReadOnlyList<string> excludedIds)
1875+
{
1876+
return Clients.GroupExcept(groupName, excludedIds).Send(message);
1877+
}
1878+
18171879
public Task BroadcastMethod(string message)
18181880
{
18191881
return Clients.All.Broadcast(message);
@@ -1945,6 +2007,11 @@ public Task GroupSendMethod(string groupName, string message)
19452007
return Clients.Group(groupName).InvokeAsync("Send", message);
19462008
}
19472009

2010+
public Task GroupExceptSendMethod(string groupName, string message, IReadOnlyList<string> excludedIds)
2011+
{
2012+
return Clients.GroupExcept(groupName, excludedIds).InvokeAsync("Send", message);
2013+
}
2014+
19482015
public Task BroadcastMethod(string message)
19492016
{
19502017
return Clients.All.InvokeAsync("Broadcast", message);

0 commit comments

Comments
 (0)