public async Task SubscriberHandlesExceptionsOnHandle()
        {
            // Setup

            var count     = 0;
            var log       = new LogToMemory();
            var messenger = CreateDefaultMockMessenger();
            var socket    = new WebSocketSubscriber(messenger.Object, log);

            socket.Subscribe(s =>
            {
                if (count++ == 0)
                {
                    throw new InvalidOperationException();
                }
                return(Task.FromResult(0));
            });

            // Execute

            socket.Start();
            await Task.Delay(100);

            socket.Stop();
            socket.Dispose();

            // Check

            messenger.Verify(m => m.GetResponseAsync(It.IsAny <CancellationToken>()), Times.AtLeast(2));

            Assert.True(count > 1); // Expecting messages to continue
            // Expecting logged exception
            Assert.True(log.ContainsErrors());
        }
        public async Task SubscriberHandlesExceptionsOnResponse()
        {
            // Setup
            //
            var count     = 0;
            var log       = new LogToMemory();
            var messenger = CreateDefaultMockMessenger();

            messenger
            .Setup(m => m.GetResponseAsync(It.IsAny <CancellationToken>()))
            .Callback(() =>
            {
                if (count++ == 0)
                {
                    throw new TimeoutException();
                }
            }).Returns(Task.FromResult(""));
            var socket = new WebSocketSubscriber(messenger.Object, log);

            // Execute

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

            socket.Stop();
            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));

            // Expecting logged exception
            Assert.True(log.ContainsErrors());
        }