public void TestHubUserMessage(string userName, string input, string hub, Type exceptionType = null)
        {
            var hubs = new List <string> {
                "hub1", "hub.hub1", "hub2.hub1.h.hub2", ".", ".."
            };
            var parser = new SignalRMessageParser(hubs, _resolver);

            var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubUserId(hub + "." + userName), input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            if (exceptionType != null)
            {
                Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList());
                return;
            }

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as UserDataMessage;

            Assert.NotNull(msg);
            Assert.Equal(userName, msg.UserId);
            Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload());
        }
예제 #2
0
        protected override IList <string> GetSignals(string userId, string connectionId)
        {
            var signals = _hubs.SelectMany(info =>
            {
                var items = new List <string>
                {
                    PrefixHelper.GetHubName(info.Name),
                    PrefixHelper.GetHubConnectionId(info.CreateQualifiedName(connectionId)),
                };

                if (!String.IsNullOrEmpty(userId))
                {
                    items.Add(PrefixHelper.GetHubUserId(info.CreateQualifiedName(userId)));
                }

                return(items);
            })
                          .Concat(new[]
            {
                PrefixHelper.GetConnectionId(connectionId),
                PrefixHelper.GetAck(connectionId)
            });

            return(signals.ToList());
        }
        public void TestRemoveFromGroupCommandMessage()
        {
            var hubs      = new List <string> {
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();
            var command   = new Command
            {
                CommandType = CommandType.RemoveFromGroup,
                Value       = groupName,
                WaitForAck  = true
            };

            var connectionId = GenerateRandomName();
            var message      = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command);

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as LeaveGroupMessage;

            Assert.NotNull(msg);
            Assert.Equal(connectionId, msg.ConnectionId);
            Assert.Equal(groupName, msg.GroupName);
        }
        public void TestHubGroupMessage(string input, string hub)
        {
            var hubs = new List <string> {
                "hub1", "hub.hub1", "hub.hub1.h.hub2"
            };
            var parser                = new SignalRMessageParser(hubs, _resolver);
            var groupName             = GenerateRandomName();
            var fullName              = PrefixHelper.GetHubGroupName(hub + "." + groupName);
            var message               = SignalRMessageUtility.CreateMessage(fullName, input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as GroupBroadcastDataMessage;

            Assert.NotNull(msg);

            // For group message, it is the full name as the group, e.g. hg-hub.hub1.h.hub2.abcde
            Assert.Equal(fullName, msg.GroupName);
            Assert.Equal <string>(excludedConnectionIds, msg.ExcludedList);
            Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload());
        }
        public void TestHubConnectionMessage(string connectionId, string input, string expectedId, Type exceptionType = null)
        {
            var hubs = new List <string> {
                "hub", "hub1", "hub.hub1", "h", "hub.hub1.h.hub2", "hub.hub1.h"
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();

            var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubConnectionId(connectionId), input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            if (exceptionType != null)
            {
                Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList());
                return;
            }

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as ConnectionDataMessage;

            Assert.NotNull(msg);
            Assert.Equal(expectedId, msg.ConnectionId);
            Assert.Equal(input, msg.Payload.First.GetSingleFramePayload());
        }
        public void TestHubMessage(string connectionId, string input, Type exceptionType = null)
        {
            var hubs = new List <string> {
                "h-", "a", "a.connection1"
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();

            var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubName(connectionId), input);
            var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() };

            message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList());

            if (exceptionType != null)
            {
                Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList());
                return;
            }

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = msgs[0].Message as BroadcastDataMessage;

            Assert.NotNull(msg);
            Assert.Equal <string>(excludedConnectionIds, msg.ExcludedList);
            Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload());
        }
        private IList <string> GetDefaultSignals(string userId, string connectionId)
        {
            // The list of default signals this connection cares about:
            // 1. The default signal (the type name)
            // 2. The connection id (so we can message this particular connection)

            return(new string[] {
                DefaultSignal,
                PrefixHelper.GetConnectionId(connectionId)
            });
        }
예제 #8
0
        private static Task SendCommand(ITransportConnection connection, string connectionId, CommandType commandType)
        {
            var command = new Command
            {
                CommandType = commandType
            };

            var message = new ConnectionMessage(PrefixHelper.GetConnectionId(connectionId),
                                                command);

            return(connection.Send(message));
        }
        /// <summary>
        /// Sends a message to all connections subscribed to the specified signal. An example of signal may be a
        /// specific connection id.
        /// </summary>
        /// <param name="connection">The connection</param>
        /// <param name="connectionIds">The connection ids to send to.</param>
        /// <param name="value">The value to publish.</param>
        /// <returns>A task that represents when the broadcast is complete.</returns>
        public static Task Send(this IConnection connection, IList <string> connectionIds, object value)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            if (connectionIds == null)
            {
                throw new ArgumentNullException("connectionIds");
            }

            var message = new ConnectionMessage(connectionIds.Select(c => PrefixHelper.GetConnectionId(c)).ToList(),
                                                value);

            return(connection.Send(message));
        }
        /// <summary>
        /// Sends a message to all connections subscribed to the specified signal. An example of signal may be a
        /// specific connection id.
        /// </summary>
        /// <param name="connection">The connection</param>
        /// <param name="connectionId">The connectionId to send to.</param>
        /// <param name="value">The value to publish.</param>
        /// <returns>A task that represents when the broadcast is complete.</returns>
        public static Task Send(this IConnection connection, string connectionId, object value)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            if (String.IsNullOrEmpty(connectionId))
            {
                throw new ArgumentException(Resources.Error_ArgumentNullOrEmpty, "connectionId");
            }

            var message = new ConnectionMessage(PrefixHelper.GetConnectionId(connectionId),
                                                value);

            return(connection.Send(message));
        }
        public void TestOtherGroupCommandMessagesAreIgnored(CommandType type)
        {
            var hubs      = new List <string> {
            };
            var parser    = new SignalRMessageParser(hubs, _resolver);
            var groupName = GenerateRandomName();
            var command   = new Command
            {
                CommandType = type,
                Value       = groupName,
                WaitForAck  = true
            };

            var connectionId = GenerateRandomName();
            var message      = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command);

            var msgs = parser.GetMessages(message).ToList();

            Assert.Empty(msgs);
        }
예제 #12
0
        public void TestAddToGroupCommandMessage()
        {
            var hubs      = new List <string> {
            };
            var parser    = new SignalRMessageParser(hubs, _resolver, NullLogger <SignalRMessageParser> .Instance);
            var groupName = GenerateRandomName();
            var command   = new Command
            {
                CommandType = CommandType.AddToGroup,
                Value       = groupName,
                WaitForAck  = true
            };

            var connectionId = GenerateRandomName();
            var message      = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command);

            var msgs = parser.GetMessages(message).ToList();

            Assert.Single(msgs);
            var msg = Assert.IsType <JoinGroupWithAckMessage>(msgs[0].Message);

            Assert.Equal(connectionId, msg.ConnectionId);
            Assert.Equal(groupName, msg.GroupName);
        }
        public void AcksAreSentToAckSubscriber()
        {
            // Arrange
            var waitCommand = new Command {
                WaitForAck = true
            };
            var ackerId     = "acker";
            var waiterId    = "waiter";
            var messageId   = "messageId";
            var maxMessages = 1;

            var ackHandler = new Mock <IAckHandler>();

            ackHandler.Setup(m => m.TriggerAck(waitCommand.Id)).Returns(false);

            var     messageBus  = new Mock <IMessageBus>();
            Message waitMessage = null;
            Message ackMessage  = null;

            messageBus.Setup(m => m.Publish(It.IsAny <Message>())).Returns <Message>(m =>
            {
                if (m.WaitForAck)
                {
                    waitMessage = m;
                }
                else if (m.IsAck)
                {
                    ackMessage = m;
                }

                return(TaskAsyncHelper.Empty);
            });

            var loggerFactory = new Mock <ILoggerFactory>();
            var counters      = new PerformanceCounterManager(loggerFactory.Object);

            var serializer       = JsonUtility.CreateDefaultSerializer();
            var waiterConnection = new Connection(messageBus.Object,
                                                  serializer,
                                                  "signal",
                                                  waiterId,
                                                  new string[] { },
                                                  new string[] { },
                                                  loggerFactory.Object,
                                                  ackHandler.Object,
                                                  counters,
                                                  new Mock <IProtectedData>().Object,
                                                  new MemoryPool());

            // Act
            waiterConnection.Send(ackerId, waitCommand);

            // Assert
            Assert.NotNull(waitMessage);
            Assert.Equal(waiterId, waitMessage.Source);
            Assert.Equal(PrefixHelper.GetConnectionId(ackerId), waitMessage.Key);

            // Arrange some more now that we have a waitMessage
            var messages = new List <ArraySegment <Message> >()
            {
                new ArraySegment <Message>(new[] { waitMessage })
            };
            var messageResult = new MessageResult(messages, 1);

            var ackerConnection = new Connection(messageBus.Object,
                                                 serializer,
                                                 "signal",
                                                 ackerId,
                                                 new string[] { },
                                                 new string[] { },
                                                 loggerFactory.Object,
                                                 ackHandler.Object,
                                                 counters,
                                                 new Mock <IProtectedData>().Object,
                                                 new Mock <IMemoryPool>().Object);

            ackerConnection.WriteCursor = _ => { };

            messageBus.Setup(m => m.Subscribe(ackerConnection,
                                              messageId,
                                              It.IsAny <Func <MessageResult, object, Task <bool> > >(),
                                              maxMessages,
                                              It.IsAny <object>()))
            .Callback <ISubscriber,
                       string,
                       Func <MessageResult, object, Task <bool> >,
                       int,
                       object>((subsciber, cursor, callback, max, state) =>
            {
                callback(messageResult, state);
            });

            // Act
            ackerConnection.Receive(messageId, (_, __) => TaskAsyncHelper.False, maxMessages, null);

            // Assert
            Assert.NotNull(ackMessage);
            Assert.Equal(ackerId, ackMessage.Source);
            Assert.Equal(AckSubscriber.Signal, ackMessage.Key);
        }
예제 #14
0
 protected override IList <string> GetSignals(string connectionId)
 {
     return(_hubs.SelectMany(info => new[] { PrefixHelper.GetHubName(info.Name), PrefixHelper.GetHubConnectionId(info.CreateQualifiedName(connectionId)) })
            .Concat(new[] { PrefixHelper.GetConnectionId(connectionId), PrefixHelper.GetAck(connectionId) })
            .ToList());
 }