public void UpdatingPresence_WhenChannelIsAttached_SendsQueuedMessages_AsASingleMessage_CallsCallbacks(ChannelState state) { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(state); manager.Setup(c => c.Send(It.IsAny <ProtocolMessage>(), It.IsAny <Action <bool, ErrorInfo> >())) .Callback <ProtocolMessage, Action <bool, ErrorInfo> >((pm, act) => act(true, null)); var target = new Presence(manager.Object, channel.Object, "testClient"); List <Tuple <bool, ErrorInfo> > callbacks = new List <Tuple <bool, ErrorInfo> >(); target.Enter(null, (s, e) => { callbacks.Add(Tuple.Create(s, e)); }); target.Update(null, (s, e) => { callbacks.Add(Tuple.Create(s, e)); }); // Act channel.Raise(c => c.ChannelStateChanged += null, new ChannelStateChangedEventArgs(ChannelState.Attached)); // Assert Assert.Equal <int>(2, callbacks.Count); Assert.True(callbacks[0].Item1); Assert.Null(callbacks[0].Item2); Assert.True(callbacks[1].Item1); Assert.Null(callbacks[1].Item2); }
public void UpdatingPresence_WhenConnection_InvalidState_ThrowsError(ChannelState state) { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(state); var target = new Presence(manager.Object, channel.Object, "testClient"); // Act Assert.Throws <AblyException>(() => target.Enter(null, null)); }
public void UpdatingPresence_WhenConnectionIsConnecting_QueuesMessages(ChannelState state) { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(state); var target = new Presence(manager.Object, channel.Object, "testClient"); // Act target.Enter(null, null); // Assert manager.Verify(c => c.Send(It.IsAny <ProtocolMessage>(), null), Times.Never()); }
public void UpdatingPresence_WhenConnection_Initialized_AttachesChannel() { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(ChannelState.Initialised); var target = new Presence(manager.Object, channel.Object, "testClient"); // Act target.Enter(null, null); // Assert channel.Verify(c => c.Attach(), Times.Once()); }
public void UpdatingPresence_WhenChannelIsFailed_FailsQueuedMessages(ChannelState state) { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(state); var target = new Presence(manager.Object, channel.Object, "testClient"); target.Enter(null, null); // Act channel.Raise(c => c.ChannelStateChanged += null, new ChannelStateChangedEventArgs(ChannelState.Failed)); // Assert manager.Verify(c => c.Send(It.IsAny <ProtocolMessage>(), null), Times.Never()); }
public void UpdatingPresence_WhenChannelIsAttached_SendsQueuedMessages_AsASingleMessage(ChannelState state) { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(state); var target = new Presence(manager.Object, channel.Object, "testClient"); target.Enter(null, null); target.Update(null, null); // Act channel.Raise(c => c.ChannelStateChanged += null, new ChannelStateChangedEventArgs(ChannelState.Attached)); // Assert manager.Verify(c => c.Send(It.IsAny <ProtocolMessage>(), It.IsAny <Action <bool, ErrorInfo> >()), Times.Once()); }
public void UpdatingPresence_WhenChannelIsFailed_FailsQueuedMessages_CallsCallbacks(ChannelState state) { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(state); var target = new Presence(manager.Object, channel.Object, "testClient"); ErrorInfo targetError = new ErrorInfo("rrr", 12); List <Tuple <bool, ErrorInfo> > callbacks = new List <Tuple <bool, ErrorInfo> >(); target.Enter(null, (s, e) => { callbacks.Add(Tuple.Create(s, e)); }); // Act channel.Raise(c => c.ChannelStateChanged += null, new ChannelStateChangedEventArgs(ChannelState.Failed, targetError)); // Assert Assert.Equal <int>(1, callbacks.Count); Assert.False(callbacks[0].Item1); Assert.Same(targetError, callbacks[0].Item2); }
public void Enter_SendsCorrectMessage() { // Arrange Mock <IConnectionManager> manager = new Mock <IConnectionManager>(); Mock <IRealtimeChannel> channel = new Mock <IRealtimeChannel>(); channel.SetupGet(c => c.State).Returns(ChannelState.Attached); Stack <ProtocolMessage> messages = new Stack <ProtocolMessage>(); manager.Setup(c => c.Send(It.IsAny <ProtocolMessage>(), null)).Callback <ProtocolMessage, Action <bool, ErrorInfo> >((m, c) => messages.Push(m)); var target = new Presence(manager.Object, channel.Object, "testClient"); // Act target.Enter(null, null); // Assert Assert.Equal <int>(1, messages.Count); ProtocolMessage msg = messages.Pop(); Assert.Equal <ProtocolMessage.MessageAction>(ProtocolMessage.MessageAction.Presence, msg.Action); Assert.Equal <int>(1, msg.Presence.Length); Assert.Equal <PresenceMessage.ActionType>(PresenceMessage.ActionType.Enter, msg.Presence[0].Action); Assert.Equal <string>("testClient", msg.Presence[0].ClientId); }