Exemplo n.º 1
0
            private void SendLeaveMessageAfterFirstSyncMessageReceived(AblyRealtime client, string clientId, string channelName)
            {
                var transport = client.GetTestTransport();

                int syncMessageCount = 0;

                transport.AfterDataReceived = message =>
                {
                    if (message.Action == ProtocolMessage.MessageAction.Sync)
                    {
                        syncMessageCount++;
                        if (syncMessageCount == 1)
                        {
                            var leaveMessage = new ProtocolMessage(ProtocolMessage.MessageAction.Presence, channelName)
                            {
                                Presence = new[]
                                {
                                    new PresenceMessage(PresenceAction.Leave, clientId)
                                    {
                                        ConnectionId = $"{client.Connection.Id}",
                                        Id           = $"{client.Connection.Id}-#{clientId}:0",
                                        Timestamp    = TestHelpers.Now(),
                                    }
                                }
                            };
                            transport.FakeReceivedMessage(leaveMessage);
                        }
                    }
                };
            }
Exemplo n.º 2
0
        internal static void BeforeProtocolMessageProcessed(this AblyRealtime client, Action <ProtocolMessage> action)
        {
            var t = client.GetTestTransport();

            if (t != null)
            {
                t.BeforeDataProcessed = action;
            }

            if (client.Options.TransportFactory is TestTransportFactory f)
            {
                f.BeforeDataProcessed = action;
            }
        }
        [Trait("intermittent", "true")] // I think the logic behind resending the detach message has an issue
        public async Task WithChannelInDetachingState_WhenTransportIsDisconnected_ShouldResendDetachMessageOnConnectionResumed(Protocol protocol)
        {
            int          detachMessageCount = 0;
            AblyRealtime client             = null;
            var          channelName        = "test-channel".AddRandomSuffix();

            client = await GetRealtimeClient(protocol, (options, settings) =>
            {
                options.TransportFactory = new TestTransportFactory
                {
                    OnMessageSent = OnMessageSent,
                };
            });

            void OnMessageSent(ProtocolMessage message)
            {
                if (message.Action == ProtocolMessage.MessageAction.Detach)
                {
                    if (detachMessageCount == 0)
                    {
                        detachMessageCount++;
                        client.GetTestTransport().Close(suppressClosedEvent: false);
                    }
                }
            }

            await client.WaitForState(ConnectionState.Connected);

            var channel = client.Channels.Get(channelName);
            await channel.AttachAsync();

            channel.Detach();
            await channel.WaitForState(ChannelState.Detaching);

            await client.WaitForState(ConnectionState.Disconnected);

            channel.State.Should().Be(ChannelState.Detaching);
            await client.WaitForState(ConnectionState.Connected);

            await channel.WaitForState(ChannelState.Detached, TimeSpan.FromSeconds(10));
        }
Exemplo n.º 4
0
 internal static void SimulateLostConnectionAndState(this AblyRealtime client)
 {
     client.State.Connection.Id  = string.Empty;
     client.State.Connection.Key = "xxxxx!xxxxxxx-xxxxxxxx-xxxxxxxx";
     client.GetTestTransport().Close(false);
 }
        public async Task WithChannelInAttachingState_WhenTransportIsDisconnected_ShouldResendAttachMessageOnConnectionResumed(Protocol protocol)
        {
            var channelName  = "test-channel".AddRandomSuffix();
            var sentMessages = new List <ProtocolMessage>();

            int attachMessageCount = 0;

            AblyRealtime client = null;

            client = await GetRealtimeClient(protocol, (options, settings) =>
            {
                options.TransportFactory = new TestTransportFactory
                {
                    OnMessageSent = OnMessageSent,
                };
            });

            void OnMessageSent(ProtocolMessage message)
            {
                sentMessages.Add(message);
                if (message.Action == ProtocolMessage.MessageAction.Attach)
                {
                    if (attachMessageCount == 0)
                    {
                        attachMessageCount++;
                        client.GetTestTransport().Close(suppressClosedEvent: false);
                    }
                }
            }

            bool didDisconnect = false;

            client.Connection.Once(ConnectionEvent.Disconnected, change =>
            {
                didDisconnect = true;
            });

            await client.WaitForState(ConnectionState.Connected);

            var channel = client.Channels.Get(channelName);

            channel.Attach();

            await channel.WaitForState(ChannelState.Attaching);

            await client.WaitForState(ConnectionState.Disconnected);

            await client.WaitForState(ConnectionState.Connecting);

            await client.WaitForState(ConnectionState.Connected);

            client.Connection.State.Should().Be(ConnectionState.Connected);
            didDisconnect.Should().BeTrue();

            await channel.WaitForAttachedState();

            var attachCount = sentMessages.Count(x => x.Channel == channelName && x.Action == ProtocolMessage.MessageAction.Attach);

            attachCount.Should().Be(2);

            client.Close();
        }