public void SetupSubscriberToContinueOnFailedHandle()
        {
            // Setup

            var count     = 0;
            var log       = new LogToMemory();
            var messenger = CreateDefaultMockMessenger();
            var _lock     = new ManualResetEvent(false);
            var socket    = new DerivedWebSocketSubscriber(messenger.Object, log)
            {
                HandleCallback = (token) =>
                {
                    if (++count < 2)
                    {
                        throw new InvalidOperationException();
                    }
                    _lock.Set();
                    return(Task.CompletedTask);
                }
            };

            // Execute

            socket.Start();
            _lock.WaitOne();
            socket.Dispose();
            // Check

            messenger.Verify(m => m.ConnectAsync(It.IsAny <CancellationToken>()), Times.Exactly(2));
            messenger.Verify(m => m.GetResponseAsync(It.IsAny <CancellationToken>()), Times.AtLeast(2));
            messenger.Verify(m => m.StopAsync(It.IsAny <CancellationToken>()), Times.Exactly(2));
        }
        public async Task SetupSubscriberToContinueOnFailedConnection()
        {
            // Setup

            var count     = 0;
            var log       = new LogToMemory();
            var messenger = CreateDefaultMockMessenger();
            var socket    = new DerivedWebSocketSubscriber(messenger.Object, log)
            {
                ConnectCallback = (token) =>
                {
                    var failure = count++ == 0;
                    if (failure)
                    {
                        throw new InvalidOperationException();
                    }
                    return(Task.CompletedTask);
                }
            };

            // Execute
            //
            socket.Start();
            await Task.Delay(ReconnectionTime.Add(TimeSpan.FromMilliseconds(100))); // wait for connection retry

            socket.Dispose();

            // Check
            //

            messenger.Verify(m => m.ConnectAsync(It.IsAny <CancellationToken>()), Times.AtLeast(2));
            messenger.Verify(m => m.GetResponseAsync(It.IsAny <CancellationToken>()), Times.AtLeastOnce);
        }
        public void SetupSubscriberToStopOnFailedHandle()
        {
            // Setup

            var heartbeat = Timeout.InfiniteTimeSpan;
            var lk        = new ManualResetEventSlim(false);
            var log       = new LogToMemory();
            var messenger = CreateDefaultMockMessenger();
            var socket    = new DerivedWebSocketSubscriber(messenger.Object, log, heartbeat)
            {
                HandleCallback = (token) =>
                {
                    lk.Set();
                    return(Task.FromException(new AuthenticationException()));
                }
            };

            // Execute

            socket.Start();
            Thread.Sleep(10000);
            socket.Dispose();

            // Check

            // 1 call Connect/Stop. No retries.
            messenger.Verify(m => m.ConnectAsync(It.IsAny <CancellationToken>()), Times.Exactly(1));
            messenger.Verify(m => m.GetResponseAsync(It.IsAny <CancellationToken>()), Times.Exactly(1));
            messenger.Verify(m => m.StopAsync(It.IsAny <CancellationToken>()), Times.Exactly(1));
        }
        public async Task SetupSubscriberToStopOnFailedConnection()
        {
            // Setup

            var heartbeat = TimeSpan.FromMilliseconds(500);
            var waitTime  = TimeSpan.FromMilliseconds(1000);

            var log       = new LogToMemory();
            var messenger = CreateDefaultMockMessenger();
            var socket    = new DerivedWebSocketSubscriber(messenger.Object, log, heartbeat)
            {
                ConnectCallback = (token) => Task.FromException(new AuthenticationException())
            };

            // Execute

            socket.Start();
            await Task.Delay(waitTime); // wait for possible heartbeat recharge

            socket.Dispose();

            // Check

            // 1 call Connect/Stop. No retries.
            messenger.Verify(m => m.ConnectAsync(It.IsAny <CancellationToken>()), Times.Exactly(1));
            messenger.Verify(m => m.GetResponseAsync(It.IsAny <CancellationToken>()), Times.Never);
            messenger.Verify(m => m.StopAsync(It.IsAny <CancellationToken>()), Times.Exactly(1));
        }