public async Task SendWebHook_StopsOnLastLastFailureOrFirstSuccessAndFirstGone(TimeSpan[] delays, Func <HttpRequestMessage, int, Task <HttpResponseMessage> > handler, int expectedOffset, SendResult expectedResult)
        {
            // Arrange
            var             actualResult  = SendResult.None;
            var             done          = new ManualResetEvent(initialState: false);
            WebHookWorkItem final         = null;
            var             actualRetries = 0;

            _sender = new TestDataflowWebHookSender(_loggerMock.Object, delays, _options, _httpClient,
                                                    onWebHookRetry: item =>
            {
                actualRetries++;
            },
                                                    onWebHookSuccess: item =>
            {
                final        = item;
                actualResult = SendResult.Success;
                done.Set();
            },
                                                    onWebHookGone: item =>
            {
                final        = item;
                actualResult = SendResult.Gone;
                done.Set();
            },
                                                    onWebHookFailure: item =>
            {
                final        = item;
                actualResult = SendResult.Failure;
                done.Set();
            });
            _handlerMock.Handler = handler;
            var notification = new NotificationDictionary("a1", data: null);
            var webHook      = WebHookManagerTests.CreateWebHook();
            var workItem     = new WebHookWorkItem(webHook, new[] { notification })
            {
                Id = "1234567890",
            };

            // Act
            await _sender.SendWebHookWorkItemsAsync(new[] { workItem });

            done.WaitOne();

            // Assert
            var expectedRetries = expectedResult == SendResult.Failure ? Math.Max(0, expectedOffset - 1) : expectedOffset;

            Assert.Equal(expectedRetries, actualRetries);
            Assert.Equal(expectedResult, actualResult);
            Assert.Equal(expectedOffset, final.Offset);
        }
        private static WebHookWorkItem CreateWorkItem()
        {
            WebHook webHook = WebHookManagerTests.CreateWebHook();
            NotificationDictionary notification1 = new NotificationDictionary("a1", new { d1 = "dv1" });
            NotificationDictionary notification2 = new NotificationDictionary("a1", new Dictionary <string, object> {
                { "d2", new Uri("http://localhost") }
            });
            WebHookWorkItem workItem = new WebHookWorkItem(webHook, new[] { notification1, notification2 })
            {
                Id = "1234567890",
            };

            return(workItem);
        }
        public async Task SendWebHook_StopsOnLastLastFailureOrFirstSuccessAndFirstGone(TimeSpan[] delays, Func <HttpRequestMessage, int, Task <HttpResponseMessage> > handler, int expected)
        {
            // Arrange
            ManualResetEvent done = new ManualResetEvent(initialState: false);
            WebHookWorkItem  success = null, failure = null;

            _sender = new DataflowWebHookSender(_loggerMock.Object, delays, _options, _httpClient, onWebHookSuccess: item =>
            {
                success = item;
                done.Set();
            }, onWebHookFailure: item =>
            {
                failure = item;
                done.Set();
            });
            _handlerMock.Handler = handler;
            NotificationDictionary notification = new NotificationDictionary("a1", data: null);
            WebHook         webHook             = WebHookManagerTests.CreateWebHook();
            WebHookWorkItem workItem            = new WebHookWorkItem(webHook, new[] { notification })
            {
                Id = "1234567890",
            };

            // Act
            await _sender.SendWebHookWorkItemsAsync(new[] { workItem });

            done.WaitOne();

            // Assert
            if (expected >= 0)
            {
                Assert.Equal(expected, success.Offset);
            }
            else
            {
                Assert.Equal(Math.Abs(expected), failure.Offset);
            }
        }