public void Does_handle_concurrent_requests()
        {
            var rand   = new Random();
            var client = new JsonHttpClient(Config.AbsoluteBaseUri);

            client.GetHttpClient().Timeout = TimeSpan.FromMinutes(5);
            long      responsesReceived    = 0;
            long      totalSecondsWaited   = 0;
            var       sw = Stopwatch.StartNew();
            const int ConcurrentRequests = 50;

            ConcurrentRequests.Times(i =>
            {
                Interlocked.Increment(ref responsesReceived);

                ThreadPool.QueueUserWorkItem(async _ =>
                {
                    var request = new SleepTest
                    {
                        Name        = $"Request {i+1}",
                        WaitingSecs = rand.Next(30, 60),
                    };
                    Interlocked.Add(ref totalSecondsWaited, request.WaitingSecs);

                    log.Info($"[{DateTime.Now.TimeOfDay}] Sending {request.Name} to sleep for {request.WaitingSecs} seconds...");

                    try
                    {
                        var response = await client.GetAsync(request);

                        log.Info($"[{DateTime.Now.TimeOfDay}] Received {request.Name}: {response.Message}");
                    }
                    catch (Exception ex)
                    {
                        log.Error($"[{DateTime.Now.TimeOfDay}] Error Response: {ex.UnwrapIfSingleException().Message}", ex);
                    }
                    finally
                    {
                        Interlocked.Decrement(ref responsesReceived);
                    }
                });
            });

            while (Interlocked.Read(ref responsesReceived) > 0)
            {
                Thread.Sleep(10);
            }

            log.Info($"Took {sw.Elapsed.TotalSeconds} to execute {ConcurrentRequests} Concurrent Requests waiting a total of {totalSecondsWaited} seconds.");
        }
        public async Task ThrowAsync_ServiceException_only_calls_ServiceExceptionHandlers()
        {
            ServiceEx = UnHandledEx = null;
            var client = new JsonHttpClient(Config.ListeningOn);

            try
            {
                var response = await client.GetAsync(new ThrowAsync());

                Assert.Fail("Should fail");
            }
            catch (WebServiceException ex)
            {
                Assert.That(ServiceEx.Message, Is.EqualTo(nameof(ThrowAsync)));
                Assert.That(UnHandledEx, Is.Null);
                Assert.That(ex.Message, Is.EqualTo(nameof(ThrowAsync)));
            }
        }
Пример #3
0
        public async Task Load_test_GetFactorialSync_HttpClient_async()
        {
            var client = new JsonHttpClient(Config.ListeningOn);

            int i = 0;

            var fetchTasks = NoOfTimes.Times(() =>
                                             client.GetAsync(new GetFactorialSync {
                ForNumber = 3
            })
                                             .ContinueWith(t =>
            {
                if (++i % 100 == 0)
                {
                    "{0}: {1}".Print(i, t.Result.Result);
                }
            }));

            await Task.WhenAll(fetchTasks);
        }