Ejemplo n.º 1
0
        public async Task CountdownTimer_Abort_StopsCountdown()
        {
            // Arrange
            var    timer    = new CountdownTimer("Test timer", Logger);
            var    timeout  = TimeSpan.FromMilliseconds(50);
            int    called   = 0;
            Action callback = () => called++;

            timer.Start(timeout, callback);

            // Act
            timer.Abort();
            for (var i = 0; i < 20; i++)
            {
                if (called == 0)
                {
                    await Task.Delay(50);
                }
                else
                {
                    break;
                }
            }

            // Assert
            called.Should().Be(0);
        }
Ejemplo n.º 2
0
        public void Fail(ErrorInfo error)
        {
            lock (_lock)
            {
                if (_waiting == false)
                {
                    return;
                }

                _timer.Abort();
                _waiting = false;
            }

            InvokeCallbacks(false, error);
        }
Ejemplo n.º 3
0
        private void Complete(bool success, ErrorInfo error = null)
        {
            lock (_lock)
            {
                _timer?.Abort();
                if (Waiting == false)
                {
                    return;
                }

                Waiting = false;
            }

            InvokeCallbacks(success, error);
        }
Ejemplo n.º 4
0
        public async Task CountdownTimer_Abort_StopsCountdown()
        {
            // Arrange
            var    timer    = new CountdownTimer("Test timer");
            var    timeout  = TimeSpan.FromMilliseconds(10);
            int    called   = 0;
            Action callback = () => called++;

            timer.Start(timeout, callback);

            // Act
            timer.Abort();
            await Task.Delay(50);

            // Assert
            called.Should().Be(0);
        }
Ejemplo n.º 5
0
        private void HandleStateChange(ChannelState state, ErrorInfo error, ProtocolMessage protocolMessage)
        {
            State = state;

            switch (state)
            {
            case ChannelState.Attaching:
                if (ConnectionState == ConnectionState.Initialized)
                {
                    Connection.Connect();
                }

                _timer.Abort();
                _timer.Start(ConnectionManager.Options.RealtimeRequestTimeout, OnAttachTimeout);

                //Even thought the connection won't have connected yet the message will be queued and sent as soon as
                //the connection is made
                SendMessage(new ProtocolMessage(ProtocolMessage.MessageAction.Attach, Name));
                break;

            case ChannelState.Attached:

                _timer.Abort();
                if (protocolMessage != null)
                {
                    if (protocolMessage.HasPresenceFlag)
                    {
                        //Start sync
                        if (Logger.IsDebug)
                        {
                            Logger.Debug($"Protocol message has presence flag. Starting Presence SYNC. Flag: {protocolMessage.Flags}");
                        }
                        Presence.AwaitSync();
                    }

                    AttachedSerial = protocolMessage.ChannelSerial;
                }
                SendQueuedMessages();

                break;

            case ChannelState.Detaching:
                //Fail timer if still waiting for attached.
                _attachedAwaiter.Fail(new ErrorInfo("Channel transitioned to detaching", 50000));

                if (ConnectionState == ConnectionState.Closed || ConnectionState == ConnectionState.Connecting ||
                    ConnectionState == ConnectionState.Suspended)
                {
                    SetChannelState(ChannelState.Detached, error);
                }
                else
                {
                    _timer.Start(ConnectionManager.Options.RealtimeRequestTimeout, OnDetachTimeout);
                    SendMessage(new ProtocolMessage(ProtocolMessage.MessageAction.Detach, Name));
                }

                break;

            case ChannelState.Detached:
                _timer.Abort();
                ConnectionManager.FailMessageWaitingForAckAndClearOutgoingQueue(this, error);
                ClearAndFailChannelQueuedMessages(error);
                break;

            case ChannelState.Failed:
                _attachedAwaiter.Fail(error);
                _detachedAwaiter.Fail(error);
                ConnectionManager.FailMessageWaitingForAckAndClearOutgoingQueue(this, error);
                ClearAndFailChannelQueuedMessages(error);
                break;
            }
        }