public void RetryAndThrowWhenFail() { // given var traceLogger = new TraceLogger(); var cancelSource = new CancellationTokenSource(); var retryEventHandler = new RetryEventHandler("ThrowEvent", null, cancelSource.Token, traceLogger); // when try { this.retryer.TryActionAsync <int>( () => throw new WebException("Unauthorized", null, WebExceptionStatus.ConnectionClosed, new MockWebResponse(HttpStatusCode.Unauthorized)), retryEventHandler, true).ConfigureAwait(false); } catch (Exception ex) { Assert.AreEqual(ex.Message, "RequestTimeout"); } // then var events = traceLogger.Events; Assert.AreEqual(events.Count, RetryCount); }
public void WaitAndRetry() { // given var traceLogger = new TraceLogger(); var cancelSource = new CancellationTokenSource(); var eventProperties = new Dictionary <string, string>() { { "VstsPlanUrl", "testUrl" }, { "ProjectId", "testProjectId" }, { "PlanId", "testPlanId" } }; var retryEventHandler = new RetryEventHandler("TestEvent", eventProperties, cancelSource.Token, traceLogger); // when retryer.TryActionAsync <int>( () => { Exception ex = new Exception("Unauthorized"); throw new WebException("Unauthorized", ex, WebExceptionStatus.ConnectionClosed, new MockWebResponse(HttpStatusCode.Unauthorized)); }, retryEventHandler).ConfigureAwait(false); // then var events = traceLogger.Events; Assert.AreEqual(events.Count, RetryCount); Assert.AreEqual(events[0], "TestEvent_Retry"); }
public async Task RateLimitTest_ShouldApplyRateLimiter_WithConcurrentRequests() { await Task.Delay(TimeSpan.FromMinutes(2)); // in case a previous test maxed out the limit RiotClient.RateLimiter = new RateLimiter(); RetryEventHandler onRateLimitExceeded = (o, e) => { if (e.Response != null) { Assert.Fail("Rate limit was exceeded! Proactive rate limiting failed."); } }; IRiotClient client = new RiotClient(); client.Settings.RetryOnRateLimitExceeded = true; client.RateLimitExceeded += onRateLimitExceeded; // Send one request in advance so the client can get the rate limits. await client.GetMasterLeagueAsync(RankedQueue.RANKED_SOLO_5x5); var tasks = new List <Task <LeagueList> >(); for (var i = 0; i < 59; ++i) { tasks.Add(Task.Run(() => client.GetMasterLeagueAsync(RankedQueue.RANKED_SOLO_5x5))); } var allTask = Task.WhenAll(tasks); var finishedTask = await Task.WhenAny(allTask, Task.Delay(60000)); if (finishedTask == allTask) { var failedTask = tasks.FirstOrDefault(t => t.IsFaulted); if (failedTask != null) { Assert.Fail(failedTask.Exception?.ToString()); } var leagues = allTask.Result; for (var i = 0; i < tasks.Count; ++i) { Assert.That(leagues[i], Is.Not.Null, "Failed to get league: " + i); } } else { var completedCount = tasks.Count(t => t.IsCompleted); Assert.Fail($"Timed out waiting for tasks ({completedCount}/{tasks.Count} tasks completed)"); } }
public void CheckShouldRetry() { // given var traceBrokerInstrumentation = new TraceBrokerInstrumentation(); var cancelSource = new CancellationTokenSource(); var retryEventHandler = new RetryEventHandler("TestEvent", null, cancelSource.Token, traceBrokerInstrumentation); retryEventHandler.ShouldRetry = (e, count) => (e is InvalidFilterCriteriaException && count < 1); // when retryer.TryActionAsync <int>( () => { throw new InvalidFilterCriteriaException("custom exception"); }, retryEventHandler).ConfigureAwait(false); // then var events = traceBrokerInstrumentation.Events; Assert.AreEqual(events.Count, 1); }
public void ReplaceDefaultTransientErrors() { // given var traceLogger = new TraceLogger(); var cancelSource = new CancellationTokenSource(); var trasientHttpCodes = new HashSet <HttpStatusCode>() { HttpStatusCode.ExpectationFailed }; var retryEventHandler = new RetryEventHandler("ReplaceDefaultEvent", null, cancelSource.Token, traceLogger, trasientHttpCodes); // when retryer.TryActionAsync <int>( () => throw new WebException("ExpectationFailed", null, WebExceptionStatus.ConnectionClosed, new MockWebResponse(HttpStatusCode.ExpectationFailed)), retryEventHandler).ConfigureAwait(false); // then var events = traceLogger.Events; Assert.AreEqual(events.Count, RetryCount); }