Пример #1
0
        public void Publish_WhenAttached_PublishesMessages()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(m => m.Action == ProtocolMessage.MessageAction.Attach), null))
            .Raises(c => c.MessageReceived += null, new ProtocolMessage(ProtocolMessage.MessageAction.Attached));
            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);
            target.Attach();
            Message[] messages = new Message[]
            {
                new Message("message1", null),
                new Message("message2", "payload"),
            };
            ProtocolMessage sendMessage = null;

            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(cc => cc.Action == ProtocolMessage.MessageAction.Message), null))
            .Callback <ProtocolMessage, Action <bool, ErrorInfo> >((m, e) => sendMessage = m);

            // Act
            target.Publish(messages);

            // Assert
            Assert.Equal(2, sendMessage.Messages.Length);
            Assert.Same(messages[0], sendMessage.Messages[0]);
            Assert.Same(messages[1], sendMessage.Messages[1]);
        }
Пример #2
0
        public void Attach_IgnoresSubsequentAttaches()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            Realtime.Channel    target = new Realtime.Channel("test", "client", manager.Object);
            List <ChannelState> states = new List <ChannelState>();

            target.ChannelStateChanged += (s, e) => states.Add(e.NewState);

            // Act
            target.Attach();
            target.Attach();

            // Assert
            Assert.Single <ChannelState>(states, c => c == ChannelState.Attaching);
        }
Пример #3
0
        public void WhenDisconnected_OppensConnectionOnAttach()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);

            // Act
            target.Attach();

            // Assert
            manager.Verify(c => c.Connect(), Times.Once());
        }
Пример #4
0
        public void WhenConnected_DoesNotOppenConnectionOnAttach()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);

            // Act
            target.Attach();

            // Assert
            manager.Verify(c => c.Connect(), Times.Never());
        }
Пример #5
0
        public void Attach_WhenDetaching_MovesStraightToAttaching()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(m => m.Action == ProtocolMessage.MessageAction.Attach), null))
            .Raises(c => c.MessageReceived += null, new ProtocolMessage(ProtocolMessage.MessageAction.Attached));
            Task detachingTask = null;

            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(m => m.Action == ProtocolMessage.MessageAction.Detach), null))
            .Callback(() => detachingTask = Task.Factory.StartNew(() => Thread.Sleep(50)).ContinueWith(c => manager.Raise(cc => cc.MessageReceived += null, new ProtocolMessage(ProtocolMessage.MessageAction.Detached))));
            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);
            target.Attach();
            target.Detach();

            // Act
            target.Attach();
            detachingTask.Wait();

            // Assert
            Assert.Equal(ChannelState.Attached, target.State);
        }
Пример #6
0
        public void Attach_SendsAttachMessage()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);

            // Act
            target.Attach();

            // Assert
            manager.Verify(c => c.Send(It.Is <ProtocolMessage>(message => message.Action == ProtocolMessage.MessageAction.Attach &&
                                                               message.Channel == target.Name), null), Times.Once());
        }
Пример #7
0
        public void Attach_AttachesSuccessfuly_WhenMessageAttachReceived()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(m => m.Action == ProtocolMessage.MessageAction.Attach), null))
            .Raises(c => c.MessageReceived += null, new ProtocolMessage(ProtocolMessage.MessageAction.Attached));
            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);

            // Act
            target.Attach();

            // Assert
            Assert.Equal(ChannelState.Attached, target.State);
        }
Пример #8
0
        public void Publish_WhenNotAttached_PublishesQueuedMessageOnceAttached_AsSingleMessage()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(m => m.Action == ProtocolMessage.MessageAction.Attach), null))
            .Raises(c => c.MessageReceived += null, new ProtocolMessage(ProtocolMessage.MessageAction.Attached));
            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);

            // Act
            target.Publish("message", null);
            target.Publish("message2", "Test");
            target.Attach();

            // Assert
            manager.Verify(c => c.Send(It.Is <ProtocolMessage>(message => message.Action == ProtocolMessage.MessageAction.Message), null), Times.Once());
        }
Пример #9
0
        public void Publish_WhenAttached_PublishesMessage()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(m => m.Action == ProtocolMessage.MessageAction.Attach), null))
            .Raises(c => c.MessageReceived += null, new ProtocolMessage(ProtocolMessage.MessageAction.Attached));
            Realtime.Channel target = new Realtime.Channel("test", "client", manager.Object);
            target.Attach();

            // Act
            target.Publish("message", null);

            // Assert
            manager.Verify(c => c.Send(It.Is <ProtocolMessage>(message => message.Action == ProtocolMessage.MessageAction.Message &&
                                                               message.Messages.Length == 1 && message.Messages[0].Name == "message"), null));
        }
Пример #10
0
        public void Detach_EmmitsEvent()
        {
            // Arrange
            Mock <IConnectionManager> manager = new Mock <IConnectionManager>();

            manager.SetupGet(c => c.IsActive).Returns(true);
            manager.Setup(c => c.Send(It.Is <ProtocolMessage>(m => m.Action == ProtocolMessage.MessageAction.Attach), null))
            .Raises(c => c.MessageReceived += null, new ProtocolMessage(ProtocolMessage.MessageAction.Attached));
            Realtime.Channel    target = new Realtime.Channel("test", "client", manager.Object);
            List <ChannelState> states = new List <ChannelState>();

            target.Attach();
            target.ChannelStateChanged += (s, e) => states.Add(e.NewState);

            // Act
            target.Detach();

            // Assert
            Assert.Single <ChannelState>(states, c => c == ChannelState.Detaching);
        }