/// <summary>Subtest that back-off handler works as expected when exception is or isn't thrown.</summary> private void SubtestSendAsync_BackOffExceptionHandler(bool throwException, BackOffHandler.Initializer initializer, Exception exceptionToThrow = null) { var handler = new ExceptionMessageHandler { ThrowException = throwException }; if (exceptionToThrow != null) { handler.Exception = exceptionToThrow; } var configurableHanlder = new ConfigurableMessageHandler(handler); var boHandler = new MockBackOffHandler(initializer); configurableHanlder.AddExceptionHandler(boHandler); int boHandleCount = 0; // if an exception should be thrown and the handler can handle it then calculate the handle count by the // lg(MaxTimeSpan) if (throwException && initializer.HandleExceptionFunc(exceptionToThrow)) { boHandleCount = Math.Min((int)Math.Floor(Math.Log(boHandler.MaxTimeSpan.TotalSeconds, 2)) + 1, configurableHanlder.NumTries - 1); boHandleCount = boHandleCount >= 0 ? boHandleCount : 0; } using (var client = new HttpClient(configurableHanlder)) { var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler"); try { HttpResponseMessage response = client.SendAsync(request).Result; Assert.False(throwException); } catch (AggregateException ae) { Assert.True(throwException); // Don't compare exception messages because for TaskCanceledException the message // on Mono is different if there is a task associated with the exception. Assert.AreEqual(handler.Exception.GetType(), ae.InnerException.GetType()); } Assert.That(boHandler.Waits.Count, Is.EqualTo(boHandleCount)); // check the exponential behavior - wait 1, 2, 4, 8, ... seconds. if (throwException) { for (int i = 0; i < boHandler.Waits.Count; ++i) { Assert.That(boHandler.Waits[i].TotalSeconds, Is.EqualTo((int)Math.Pow(2, i))); } } Assert.That(handler.Calls, Is.EqualTo(boHandleCount + 1)); } }
/// <summary> /// Subtest that back-off handler works as expected when a successful or abnormal response is returned. /// For testing the back-off handler in case of a canceled request, set the <code>cancelRequestNum</code> /// parameter to the index of the request you want to cancel. /// </summary> private void SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode statusCode, BackOffHandler.Initializer initializer, int cancelRequestNum = 0, int numTries = 10) { var handler = new UnsuccessfulResponseMessageHandler { ResponseStatusCode = statusCode }; CancellationToken cancellationToken = CancellationToken.None; bool cancel = cancelRequestNum > 0; if (cancel) { CancellationTokenSource tcs = new CancellationTokenSource(); handler.CancellationTokenSource = tcs; handler.CancelRequestNum = cancelRequestNum; cancellationToken = tcs.Token; } var configurableHanlder = new ConfigurableMessageHandler(handler) { NumTries = numTries }; var boHandler = new MockBackOffHandler(initializer); configurableHanlder.AddUnsuccessfulResponseHandler(boHandler); int boHandleCount = 0; if (initializer.HandleUnsuccessfulResponseFunc != null && initializer.HandleUnsuccessfulResponseFunc(new HttpResponseMessage { StatusCode = statusCode })) { boHandleCount = Math.Min((int)Math.Floor(Math.Log(boHandler.MaxTimeSpan.TotalSeconds, 2)) + 1, configurableHanlder.NumTries - 1); boHandleCount = boHandleCount >= 0 ? boHandleCount : 0; if (cancel) { boHandleCount = Math.Min(boHandleCount, cancelRequestNum); } } using (var client = new HttpClient(configurableHanlder)) { var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler"); try { HttpResponseMessage response = client.SendAsync(request, cancellationToken).Result; Assert.False(cancel); } catch (AggregateException ae) { // a canceled request should throw an exception Assert.IsInstanceOf<TaskCanceledException>(ae.InnerException); Assert.True(cancel); } Assert.That(boHandler.Waits.Count, Is.EqualTo(boHandleCount)); // check the exponential behavior - wait 1, 2, 4, 8, ... seconds. for (int i = 0; i < boHandler.Waits.Count; ++i) { Assert.That(boHandler.Waits[i].TotalSeconds, Is.EqualTo((int)Math.Pow(2, i))); } // if the request was canceled the number of calls to the message handler is equal to the number of // calls to back-off handler Assert.That(handler.Calls, Is.EqualTo(boHandleCount + (cancel ? 0 : 1))); } }
/// <summary> /// Subtest that back-off handler works as expected when a successful or abnormal response is returned. /// For testing the back-off handler in case of a canceled request, set the <code>cancelRequestNum</code> /// parameter to the index of the request you want to cancel. /// </summary> private void SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode statusCode, BackOffHandler.Initializer initializer, int cancelRequestNum = 0) { var handler = new UnsuccessfulResponseMessageHandler { ResponseStatusCode = statusCode }; CancellationToken cancellationToken = CancellationToken.None; bool cancel = cancelRequestNum > 0; if (cancel) { CancellationTokenSource tcs = new CancellationTokenSource(); handler.CancellationTokenSource = tcs; handler.CancelRequestNum = cancelRequestNum; cancellationToken = tcs.Token; } var configurableHanlder = new ConfigurableMessageHandler(handler); var boHandler = new MockBackOffHandler(initializer); configurableHanlder.UnsuccessfulResponseHandlers.Add(boHandler); int boHandleCount = 0; if (initializer.HandleUnsuccessfulResponseFunc != null && initializer.HandleUnsuccessfulResponseFunc(new HttpResponseMessage { StatusCode = statusCode })) { boHandleCount = Math.Min((int)Math.Floor(Math.Log(boHandler.MaxTimeSpan.TotalSeconds, 2)) + 1, configurableHanlder.NumTries - 1); boHandleCount = boHandleCount >= 0 ? boHandleCount : 0; if (cancel) { boHandleCount = Math.Min(boHandleCount, cancelRequestNum); } } using (var client = new HttpClient(configurableHanlder)) { var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler"); try { HttpResponseMessage response = client.SendAsync(request, cancellationToken).Result; Assert.False(cancel); } catch (AggregateException ae) { // a canceled request should throw an exception Assert.IsInstanceOf <TaskCanceledException>(ae.InnerException); Assert.True(cancel); } Assert.That(boHandler.Waits.Count, Is.EqualTo(boHandleCount)); // check the exponential behavior - wait 1, 2, 4, 8, ... seconds. for (int i = 0; i < boHandler.Waits.Count; ++i) { Assert.That(boHandler.Waits[i].TotalSeconds, Is.EqualTo((int)Math.Pow(2, i))); } // if the request was canceled the number of calls to the message handler is equal to the number of // calls to back-off handler Assert.That(handler.Calls, Is.EqualTo(boHandleCount + (cancel ? 0 : 1))); } }