コード例 #1
0
        public async Task CallIntoExceptionStrategyWhenHttpRequestFails()
        {
            Mock <IDispatchExceptionStrategy> strategyMock = new Mock <IDispatchExceptionStrategy>();
            HttpRequestException exception = new HttpRequestException();

            Mock <HttpMessageHandler> httpReqHandlerMock = new Mock <HttpMessageHandler>();

            httpReqHandlerMock.Protected()
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            .Throws(exception)
            .Verifiable();

            using (var sender =
                       new AnalyticsSenderBuilder(AnalyticsEnvironment.Testing, KeyVal, SourceVal)
                       .WithMaxQueueSize(1)
                       .WithCommandLineArgs($"--{AnalyticsCommandLineArgs.EndpointName}", "https://example.com/")
                       .With(strategyMock.Object)
                       .With(new HttpClient(httpReqHandlerMock.Object))
                       .Build())
            {
                await sender.SendAsync(ClassVal, TypeVal, new Dictionary <string, string>());

                strategyMock.Verify(s => s.ProcessException(exception), Times.Once());
            }
        }
コード例 #2
0
        public async Task SendAnalyticEventsToHttpsEndpoint()
        {
            var client = new HttpClient(_messageHandlerMock.Object);

            using (var sender =
                       new AnalyticsSenderBuilder(AnalyticsEnvironment.Testing, KeyVal, SourceVal)
                       .WithCommandLineArgs($"--{AnalyticsCommandLineArgs.EndpointName}", "https://example.com/")
                       .WithMaxQueueTime(TimeSpan.FromMilliseconds(5))
                       .With(client)
                       .Build())
            {
                await sender.SendAsync(ClassVal, TypeVal, new Dictionary <string, object>
                {
                    { "dogs", "excellent" },
                    { "animals", new Dictionary <string, List <string> >
                      {
                          { "mammals", new List <string> {
                                "dolphins", "cats"
                            } },
                          { "lizards", new List <string> {
                                "iguanas", "chameleons"
                            } }
                      } }
                }, "12345678");

                await Task.Delay(TimeSpan.FromMilliseconds(20));

                _messageHandlerMock.Protected().Verify("SendAsync", Times.Exactly(1),
                                                       ItExpr.Is <HttpRequestMessage>(req => SendAnalyticEventsToHttpsEndpointExpectedMessage(req)),
                                                       ItExpr.IsAny <CancellationToken>());
            }
        }
コード例 #3
0
        public async Task NotDispatchAnalyticsEventsWithoutTime()
        {
            var client = new HttpClient(_messageHandlerMock.Object);

            using (var sender =
                       new AnalyticsSenderBuilder(AnalyticsEnvironment.Testing, KeyVal, SourceVal)
                       .WithMaxQueueTime(TimeSpan.FromSeconds(5))
                       .WithCommandLineArgs($"--{AnalyticsCommandLineArgs.EndpointName}", "https://example.com/")
                       .With(client)
                       .Build())
            {
                await sender.SendAsync(ClassVal, TypeVal, new Dictionary <string, string>());

                await sender.SendAsync("class-val-2", "type-val-2", new Dictionary <string, string>());

                _messageHandlerMock.Protected().Verify("SendAsync", Times.Exactly(0),
                                                       ItExpr.IsAny <HttpRequestMessage>(),
                                                       ItExpr.IsAny <CancellationToken>());
            }
        }
コード例 #4
0
        public async Task DispatchAnalyticsEventsAfterQueueFills()
        {
            var client = new HttpClient(_messageHandlerMock.Object);

            using (var sender =
                       new AnalyticsSenderBuilder(AnalyticsEnvironment.Testing, KeyVal, SourceVal)
                       .WithMaxQueueSize(3)
                       // This test will hopefully not take a year to run
                       .WithMaxQueueTime(TimeSpan.FromDays(365.25))
                       .WithCommandLineArgs($"--{AnalyticsCommandLineArgs.EndpointName}", "https://example.com/")
                       .With(client)
                       .Build())
            {
                await sender.SendAsync(ClassVal, TypeVal, new Dictionary <string, string>());

                await sender.SendAsync("class-val-2", "type-val-2", new Dictionary <string, string>());

                await sender.SendAsync("class-val-3", "type-val-3", new Dictionary <string, string>());

                _messageHandlerMock.Protected().Verify("SendAsync", Times.Exactly(1),
                                                       ItExpr.IsAny <HttpRequestMessage>(),
                                                       ItExpr.IsAny <CancellationToken>());
            }
        }