Example #1
0
            public async Task Negotiation(string key, string url)
            {
                const string target            = nameof(NegotiationTests);
                const int    count             = 2;
                var          users             = GenerateRandomUsers(count);
                var          messageToSend     = key + Guid.NewGuid().ToString();
                var          completionSources = new ConcurrentBag <TaskCompletionSource>();
                var          tasks             = users.Select(async user =>
                {
                    var connectionInfo   = await SimpleChatClient.Negotiate(user, url);
                    var connection       = CreateHubConnection(connectionInfo.Url, connectionInfo.AccessToken);
                    var taskCompleSource = new TaskCompletionSource();
                    completionSources.Add(taskCompleSource);
                    connection.On(target, (string message) =>
                    {
                        if (message.Equals(messageToSend))
                        {
                            taskCompleSource.SetResult();
                        }
                    });
                    await connection.StartAsync();
                    return(connection);
                }).ToArray();
                var connections = await Task.WhenAll(tasks);

                await SimpleChatClient.Send(url, new SignalRMessage
                {
                    Target    = target,
                    Arguments = new object[] { messageToSend }
                });

                await Task.WhenAll(completionSources.Select(s => s.Task)).OrTimeout();

                //clean
                await Task.WhenAll(connections.Select(c => c.DisposeAsync().AsTask()));
            }
Example #2
0
            public async Task ConnectionGroupManagement(string key, string url)
            {
                const string target    = nameof(ConnectionGroupManagementTest);
                const int    count     = 2;
                string       groupName = Guid.NewGuid().ToString();

                var users             = GenerateRandomUsers(count).ToArray();
                var messageToSend     = key + Guid.NewGuid().ToString();
                var completionSources = new ConcurrentDictionary <string, TaskCompletionSource>();
                var tasks             = users.Select(async user =>
                {
                    var connectionInfo      = await SimpleChatClient.Negotiate(user, url);
                    var connection          = CreateHubConnection(connectionInfo.Url, connectionInfo.AccessToken);
                    var taskCompleSource    = new TaskCompletionSource();
                    completionSources[user] = taskCompleSource;
                    connection.On(target, (string message) =>
                    {
                        if (message.Equals(messageToSend))
                        {
                            completionSources[user].SetResult();
                        }
                    });
                    await connection.StartAsync();
                    return(connection);
                }).ToArray();
                var connections = await Task.WhenAll(tasks);

                //add connections[0] to group
                await SimpleChatClient.Group(url, new SignalRGroupAction
                {
                    ConnectionId = connections[0].ConnectionId,
                    GroupName    = groupName,
                    Action       = GroupAction.Add
                });

                //send messages
                await SimpleChatClient.Send(url, new SignalRMessage
                {
                    Target    = target,
                    Arguments = new object[] { messageToSend },
                    GroupName = groupName
                });

                await completionSources[users[0]].Task.OrTimeout();

                Assert.False(completionSources[users[1]].Task.IsCompletedSuccessfully);

                //remove connection[0] from group and add connection[1] to group
                await SimpleChatClient.Group(url, new SignalRGroupAction
                {
                    ConnectionId = connections[0].ConnectionId,
                    GroupName    = groupName,
                    Action       = GroupAction.Remove
                });

                await SimpleChatClient.Group(url, new SignalRGroupAction
                {
                    ConnectionId = connections[1].ConnectionId,
                    GroupName    = groupName,
                    Action       = GroupAction.Add
                });

                //reset
                foreach (var user in users)
                {
                    completionSources[user] = new TaskCompletionSource();
                }
                //send messages
                await SimpleChatClient.Send(url, new SignalRMessage
                {
                    Target    = target,
                    Arguments = new object[] { messageToSend },
                    GroupName = groupName
                });

                await completionSources[users[1]].Task.OrTimeout();

                Assert.False(completionSources[users[0]].Task.IsCompletedSuccessfully);
                //clean
                await Task.WhenAll(connections.Select(c => c.DisposeAsync().AsTask()));
            }