Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
wanlwanl committed Oct 12, 2019
2 parents 34fb9ec + 62468e0 commit 46c40fd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
31 changes: 18 additions & 13 deletions src/SignalRServiceExtension/Bindings/SignalRAsyncCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,29 @@ internal SignalRAsyncCollector(IAzureSignalRSender client)

if (!string.IsNullOrEmpty(groupAction.ConnectionId))
{
if (groupAction.Action == GroupAction.Add)
switch(groupAction.Action)
{
await client.AddConnectionToGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false);
}
else
{
await client.RemoveConnectionFromGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false);
case GroupAction.Add:
await client.AddConnectionToGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false);
break;
case GroupAction.Remove:
await client.RemoveConnectionFromGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false);
break;
}
}
else if (!string.IsNullOrEmpty(groupAction.UserId))
{
if (groupAction.Action == GroupAction.Add)
{
await client.AddUserToGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false);
}
else
switch (groupAction.Action)
{
await client.RemoveUserFromGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false);
case GroupAction.Add:
await client.AddUserToGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false);
break;
case GroupAction.Remove:
await client.RemoveUserFromGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false);
break;
case GroupAction.RemoveAll:
await client.RemoveUserFromAllGroups(groupAction.UserId).ConfigureAwait(false);
break;
}
}
else
Expand All @@ -95,4 +100,4 @@ internal SignalRAsyncCollector(IAzureSignalRSender client)
return Task.CompletedTask;
}
}
}
}
4 changes: 3 additions & 1 deletion src/SignalRServiceExtension/Bindings/SignalRGroupAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public enum GroupAction
[EnumMember(Value = "add")]
Add,
[EnumMember(Value = "remove")]
Remove
Remove,
[EnumMember(Value = "removeAll")]
RemoveAll
}
}
12 changes: 11 additions & 1 deletion src/SignalRServiceExtension/Client/AzureSignalRClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ public async Task RemoveUserFromGroup(string userId, string groupName)
await serviceHubContext.UserGroups.RemoveFromGroupAsync(userId, groupName);
}

public async Task RemoveUserFromAllGroups(string userId)
{
if (string.IsNullOrEmpty(userId))
{
throw new ArgumentException($"{nameof(userId)} cannot be null or empty");
}
var serviceHubContext = await serviceManagerStore.GetOrAddByConnectionString(connectionString).GetAsync(hubName);
await serviceHubContext.UserGroups.RemoveFromAllGroupsAsync(userId);
}

public async Task AddConnectionToGroup(string connectionId, string groupName)
{
if (string.IsNullOrEmpty(connectionId))
Expand Down Expand Up @@ -165,4 +175,4 @@ private static IEnumerable<Claim> BuildJwtClaims(IEnumerable<Claim> customerClai
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/SignalRServiceExtension/Client/IAzureSignalRSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ internal interface IAzureSignalRSender
Task SendToGroup(string group, SignalRData data);
Task AddUserToGroup(string userId, string groupName);
Task RemoveUserFromGroup(string userId, string groupName);
Task RemoveUserFromAllGroups(string userId);
Task AddConnectionToGroup(string connectionId, string groupName);
Task RemoveConnectionFromGroup(string connectionId, string groupName);
}
}
}
22 changes: 21 additions & 1 deletion test/SignalRServiceExtension.Tests/SignalRAsyncCollectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ await collector.AddAsync(new SignalRGroupAction
Assert.Equal("group1", actualData.Arguments[1]);
}

[Fact]
public async Task AddAsync_WithUserId_CallsRemoveUserFromAllGroups()
{
var signalRSenderMock = new Mock<IAzureSignalRSender>();
var collector = new SignalRAsyncCollector<SignalRGroupAction>(signalRSenderMock.Object);

await collector.AddAsync(new SignalRGroupAction
{
UserId = "userId1",
Action = GroupAction.RemoveAll
});

signalRSenderMock.Verify(
c => c.RemoveUserFromAllGroups("userId1"),
Times.Once);
signalRSenderMock.VerifyNoOtherCalls();
var actualData = signalRSenderMock.Invocations[0];
Assert.Equal("userId1", actualData.Arguments[0]);
}

[Fact]
public async Task AddAsync_InvalidTypeThrowException()
{
Expand Down Expand Up @@ -240,4 +260,4 @@ public async Task AddAsync_GroupOperation_WithoutParametersThrowException()
await Assert.ThrowsAsync<ArgumentException>(() => collector.AddAsync(item));
}
}
}
}

0 comments on commit 46c40fd

Please sign in to comment.