private async Task DirectConnectionJoinGroup(string connectionString) { var serviceManager = new ServiceManagerBuilder().WithOptions(option => { option.ConnectionString = connectionString; option.ServiceTransportType = ServiceTransportType.Transient; }).Build(); var hubContext = await serviceManager.CreateHubContextAsync(SignalRConstants.DefaultRestHubName); await SignalRUtils.JoinGroupForConnection( _totalConnection, _groupCount, _connectionIndex, async (i, g) => { var userId = SignalRUtils.GenClientUserIdFromConnectionIndex(_connectionIndex[i]); try { await hubContext.UserGroups.AddToGroupAsync( userId, SignalRUtils.GroupName(_type, g)); _statisticsCollector.IncreaseJoinGroupSuccess(); } catch (Exception e) { _statisticsCollector.IncreaseJoinGroupFail(); Log.Error($"Fail to join group: {e.Message}"); } }); }
private async Task NormalConnectionJoinGroup() { await SignalRUtils.JoinGroupForConnection( _totalConnection, _groupCount, _connectionIndex, async (i, g) => { await SignalRUtils.JoinToGroup( _connections[i], SignalRUtils.GroupName(_type, g), _statisticsCollector); }); }
public async Task TestJoinGroupForConnection1() { int total = 6, group = 15, agents = 2; // we expect every connection joins two or three different groups var indexes = Enumerable.Range(0, total).ToList(); indexes.Shuffle(); var expectedGroup = Enumerable.Range(0, group).ToList(); var assignedGroup = new List <int>(); var connectionGroupDic = new Dictionary <int, List <int> >(); for (var i = 0; i < agents; i++) { (int beg, int end) = Util.GetConnectionRange(total, i, agents); var connectIndex = indexes.GetRange(beg, end - beg); await SignalRUtils.JoinGroupForConnection(total, group, connectIndex, (index, groupIndex) => { //_output.WriteLine($"{connectIndex[index]}: {groupIndex}"); if (!connectionGroupDic.TryGetValue(connectIndex[index], out _)) { connectionGroupDic[connectIndex[index]] = new List <int>(); } connectionGroupDic[connectIndex[index]].Add(groupIndex); return(Task.CompletedTask); }); } Assert.Equal(connectionGroupDic.Keys.Count, total); int groupCount = 0; foreach (var key in connectionGroupDic.Keys) { groupCount += connectionGroupDic[key].Count; assignedGroup.AddRange(connectionGroupDic[key]); } Assert.Equal(groupCount, group); assignedGroup.Sort(); var diff = expectedGroup.Except(assignedGroup).ToList(); Assert.Empty(diff); }
public async Task TestJoinGroupForConnection3() { int total = 6, group = 3, agents = 2; // we expect every 2 connections join one groups var indexes = Enumerable.Range(0, total).ToList(); indexes.Shuffle(); var assignedConnect = new List <int>(); var connectionGroupDic = new Dictionary <int, List <int> >(); for (var i = 0; i < agents; i++) { (int beg, int end) = Util.GetConnectionRange(total, i, agents); var connectIndex = indexes.GetRange(beg, end - beg); await SignalRUtils.JoinGroupForConnection(total, group, connectIndex, (index, groupIndex) => { if (!connectionGroupDic.TryGetValue(groupIndex, out _)) { connectionGroupDic[groupIndex] = new List <int>(); } connectionGroupDic[groupIndex].Add(connectIndex[index]); return(Task.CompletedTask); }); } Assert.Equal(connectionGroupDic.Keys.Count, group); foreach (var key in connectionGroupDic.Keys) { Assert.Equal(total / group, connectionGroupDic[key].Count); assignedConnect.AddRange(connectionGroupDic[key]); } assignedConnect.Sort(); var diff = indexes.Except(assignedConnect).ToList(); Assert.Empty(diff); }