示例#1
0
        public void ShouldSuspend_WhenFirstAttemptEqualOrGreaterThanConnectionStateTtl_ShouldReturnTrue()
        {
            var now   = new Now();
            var state = new RealtimeState(null, now.ValueFn);

            state.AttemptsInfo.Attempts.Add(new ConnectionAttempt(now.Value));

            // Move now to default ConnectionStateTtl - 1 second
            now.Reset(DateTimeOffset.UtcNow.Add(Defaults.ConnectionStateTtl));
            state.ShouldSuspend(now.ValueFn).Should().BeTrue("When time is equal");        // =
            now.Reset(DateTimeOffset.UtcNow.Add(Defaults.ConnectionStateTtl).AddSeconds(60));
            state.ShouldSuspend(now.ValueFn).Should().BeTrue("When time is greater than"); // >
        }
        public async Task WhenItMovesFromDisconnectedToSuspended_ShouldTryDefaultHostAgain()
        {
            var now = new Now();

            var client = await GetConnectedClient(opts =>
            {
                opts.DisconnectedRetryTimeout = TimeSpan.FromMilliseconds(10);
                opts.SuspendedRetryTimeout    = TimeSpan.FromMilliseconds(10);
                opts.NowFunc = now.ValueFn;
            });

            var realtimeHosts = new List <string>();

            FakeTransportFactory.InitialiseFakeTransport = p => realtimeHosts.Add(p.Parameters.Host);

            client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Disconnected)
            {
                Error = new ErrorInfo {
                    StatusCode = HttpStatusCode.GatewayTimeout
                }
            });
            // The connection manager will move from Disconnected to Connecting on a fallback host
            await client.WaitForState(ConnectionState.Connecting);

            // Add 1 more second than the ConnectionStateTtl
            now.Reset(now.Value.Add(client.State.Connection.ConnectionStateTtl).AddSeconds(1));

            // Return an error which will trip the Suspended state check
            client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Error)
            {
                Error = new ErrorInfo {
                    StatusCode = HttpStatusCode.GatewayTimeout
                }
            });

            await client.WaitForState(ConnectionState.Suspended);

            // Shortly after the suspended timer will trigger and retry the connection
            await client.WaitForState(ConnectionState.Connecting);

            await client.ProcessCommands();

            realtimeHosts.Should().HaveCount(2);
            realtimeHosts.First().Should().Match(x => client.State.Connection.FallbackHosts.Contains(x));
            realtimeHosts.Last().Should().Be("realtime.ably.io");
        }