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

            _manager = new WebHookManager(_storeMock.Object, _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);

            // Act
            int actual = await _manager.NotifyAsync(TestUser, new[] { notification });

            done.WaitOne();

            // Assert
            _storeMock.Verify();
            if (expected >= 0)
            {
                Assert.Equal(expected, success.Offset);
            }
            else
            {
                Assert.Equal(Math.Abs(expected), failure.Offset);
            }
        }
Exemplo n.º 2
0
 public WebHookWorkItemTests()
 {
     _notification  = new NotificationDictionary("action", data: null);
     _notifications = new List <NotificationDictionary> {
         _notification
     };
     _webHook  = new WebHook();
     _workItem = new WebHookWorkItem(_webHook, _notifications);
 }
        /// <summary>
        /// Submits a notification to all matching registered WebHooks across all users. To match, the <see cref="WebHook"/> must
        /// have a filter that matches one or more of the actions provided for the notification.
        /// </summary>
        /// <param name="manager">The <see cref="IWebHookManager"/> instance.</param>
        /// <param name="action">The action describing the notification.</param>
        /// <param name="data">Optional additional data to include in the WebHook request.</param>
        /// <param name="predicate">A function to test each <see cref="WebHook"/> to see whether it fulfills the condition. The
        /// predicate is passed the <see cref="WebHook"/> and the user who registered it. If the predicate returns <c>true</c> then
        /// the <see cref="WebHook"/> is included; otherwise it is not.</param>
        /// <returns>The number of <see cref="WebHook"/> instances that were selected and subsequently notified about the actions.</returns>
        public static Task <int> NotifyAllAsync(this IWebHookManager manager, string action, object data, Func <WebHook, string, bool> predicate)
        {
            if (manager == null)
            {
                throw new ArgumentNullException("manager");
            }
            var notifications = new NotificationDictionary[] { new NotificationDictionary(action, data) };

            return(manager.NotifyAllAsync(notifications, predicate));
        }
        public void Data_Initializes_FromAnonymousType()
        {
            NotificationDictionary not = new NotificationDictionary(TestAction, new { data1 = 1234, data2 = "你好世界" });

            // Act
            int    actual1 = (int)not["data1"];
            string actual2 = (string)not["data2"];

            // Assert
            Assert.Equal(1234, actual1);
            Assert.Equal("你好世界", actual2);
        }
        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);
        }
Exemplo n.º 6
0
        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);
        }
Exemplo n.º 7
0
        public async Task SendWebHookWorkItems_ThrowsException_IfInvalidWorkItem()
        {
            // Arrange
            WebHook webHook = new WebHook();
            NotificationDictionary notification = new NotificationDictionary();

            notification.Add("k", new SerializationFailure());
            WebHookWorkItem workItem = new WebHookWorkItem(webHook, new[] { notification });
            IEnumerable <WebHookWorkItem> workItems = new[] { workItem };

            // Act
            InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => _sender.SendWebHookWorkItemsAsync(workItems));

            // Assert
            Assert.Equal("Could not serialize message: Error getting value from 'Fail' on 'Microsoft.AspNet.WebHooks.AzureWebHookSenderTests+SerializationFailure'.", ex.Message);
        }
        public void Data_Initializes_FromDictionary()
        {
            // Arrange
            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { "data1", 1234 },
                { "data2", "你好世界" },
            };
            NotificationDictionary not = new NotificationDictionary(TestAction, data);

            // Act
            int    actual1 = (int)not["data1"];
            string actual2 = (string)not["data2"];

            // Assert
            Assert.Equal(1234, actual1);
            Assert.Equal("你好世界", actual2);
        }
        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);
            }
        }
Exemplo n.º 10
0
        public void GetWorkItems_FilterSingleNotification(IEnumerable <WebHook> webHooks, NotificationDictionary notification)
        {
            // Act
            IEnumerable <WebHookWorkItem> actual = WebHookManager.GetWorkItems(webHooks.ToArray(), new[] { notification });

            // Assert
            Assert.Equal(webHooks.Count(), actual.Count());
            foreach (WebHookWorkItem workItem in actual)
            {
                Assert.Same(workItem.Notifications.Single(), notification);
            }
        }
 public NotificationDictionaryTests()
 {
     _notification = new NotificationDictionary(TestAction, data: null);
 }