private async Task <Tuple <decimal, string> > RunTestCaseWithRetryAsync(RetryAttribute retryAttribute, ExceptionAggregator aggregator) { var totalTimeTaken = 0m; List <string> messages = new(); var numAttempts = Math.Max(1, retryAttribute.MaxRetries); for (var attempt = 1; attempt <= numAttempts; attempt++) { var result = await base.InvokeTestAsync(aggregator).ConfigureAwait(false); totalTimeTaken += result.Item1; messages.Add(result.Item2); if (!aggregator.HasExceptions) { break; } else if (attempt < numAttempts) { // We can't use the ITestOutputHelper here because there's no active test messages.Add($"[{TestCase.DisplayName}] Attempt {attempt} of {retryAttribute.MaxRetries} failed due to {aggregator.ToException()}"); await Task.Delay(5000).ConfigureAwait(false); aggregator.Clear(); } } return(new(totalTimeTaken, string.Join(Environment.NewLine, messages))); }
protected void ResolveAttributes(Type type, Type modelType) { if (!SendAck) { AutoAckAttribute ackAttribute = type.GetCustomAttribute <AutoAckAttribute>(); SendAck = ackAttribute != null; } if (!SendNack) { AutoNackAttribute nackAttribute = type.GetCustomAttribute <AutoNackAttribute>(); SendNack = nackAttribute != null; NackReason = nackAttribute != null ? nackAttribute.Reason : NackReason.None; } RetryAttribute retryAttr = type.GetCustomAttribute <RetryAttribute>(); if (retryAttr != null) { Retry = retryAttr; } if (PushExceptions == null) { PushExceptions = new List <TransportExceptionDescriptor>(); } IEnumerable <PushExceptionsAttribute> pushAttributes = type.GetCustomAttributes <PushExceptionsAttribute>(true); foreach (PushExceptionsAttribute attribute in pushAttributes) { if (attribute.ExceptionType == null) { DefaultPushException = new TransportExceptionDescriptor(attribute.ModelType); } else { PushExceptions.Add(new TransportExceptionDescriptor(attribute.ModelType, attribute.ExceptionType)); } } if (PublishExceptions == null) { PublishExceptions = new List <TransportExceptionDescriptor>(); } IEnumerable <PublishExceptionsAttribute> publishAttributes = type.GetCustomAttributes <PublishExceptionsAttribute>(true); foreach (PublishExceptionsAttribute attribute in publishAttributes) { if (attribute.ExceptionType == null) { DefaultPublishException = new TransportExceptionDescriptor(attribute.ModelType); } else { PublishExceptions.Add(new TransportExceptionDescriptor(attribute.ModelType, attribute.ExceptionType)); } } }
public void You_can_create_a_behavior() { var sut = new RetryAttribute(); var result = sut.CreateBehavior(null); Assert.NotNull(result); Assert.IsType <RetryBehavior>(result); }
public void The_attribute_arguments_are_passed_on_to_behavior() { var retryErrorTypes = new[] { typeof(Exception) }; var sut = new RetryAttribute(5, 500, retryErrorTypes); var result = (RetryBehavior)sut.CreateBehavior(null); Assert.Equal(5, result.TimesToRetry); Assert.Equal(TimeSpan.FromMilliseconds(500), result.RetryDelay); Assert.Equal(retryErrorTypes, result.RetryExceptionTypes); }
private RetryPolicy GetRetryPolicy() { int length = this.attributesCached.Length; for (int i = 0; i < length; i++) { RetryAttribute retryAttribute = this.attributesCached[i] as RetryAttribute; if (retryAttribute != null) { return(retryAttribute.RetryPolicy); } } // No attribute were found. Default is no retry return(RetryPolicy.NoRetry); }
public override TestResult[] Execute(ITestMethod testMethod) { // NOTE // This implementation will need to be refactored as we add more // execution variations. int retryCount = 1; Type eet = null; Attribute[] attr = testMethod.GetAllAttributes(false); if (attr != null) { foreach (Attribute a in attr) { if (a is RetryAttribute) { RetryAttribute retryAttr = (RetryAttribute)a; retryCount = int.Parse(retryAttr.Value); } if (a is ExpectedExceptionAttribute) { ExpectedExceptionAttribute eea = (ExpectedExceptionAttribute)a; eet = eea.ExceptionType; } } } TestResult[] results = null; var res = new List <TestResult>(); var currentCount = 0; while (currentCount < retryCount) { currentCount++; try { results = base.Execute(testMethod); } catch (Exception e) { if (eet == null) { break; } if (e.GetType().Equals(eet) == false) { break; } } if (results == null) { continue; } foreach (var testResult in results) { testResult.DisplayName = $"{testMethod.TestMethodName} - Execution number {currentCount}"; } res.AddRange(results); if (results.Any((tr) => tr.Outcome == UnitTestOutcome.Failed)) { continue; } break; } return(res.ToArray()); }
/// <summary> /// Applies retry policy to all consumers /// </summary> public ModelTypeConfigurator UseRetry(int count, int delayBetweenRetries = 50, params Type[] ignoreExceptions) { Retry = new RetryAttribute(count, delayBetweenRetries, ignoreExceptions); return(this); }
public void InitializeClient(Type interfaceType) { ConcurrentDictionary <string, ActionWrapper> concurrentDictionary1; if (RestWrapper.cache.TryGetValue(interfaceType, out concurrentDictionary1)) { return; } ConcurrentDictionary <string, ActionWrapper> concurrentDictionary2 = new ConcurrentDictionary <string, ActionWrapper>(); IRoutePrefix routePrefix = RestWrapper.GetRoutePrefix(interfaceType); this._errorInterceptor = interfaceType.GetCustomAttribute <ErrorHandlerAttribute>(); this._serviceLocator.GetService <ILogger>()?.Message("Initializing client " + interfaceType.Name); RetryAttribute customAttribute = interfaceType.GetCustomAttribute <RetryAttribute>(); if (interfaceType.GetCustomAttribute <CircuitBreakerAttribute>() != null) { CircuitBreakerContainer.Register(interfaceType, (ICircuitBreaker) new Stardust.Interstellar.Rest.Client.CircuitBreaker.CircuitBreaker(interfaceType.GetCustomAttribute <CircuitBreakerAttribute>(), this._serviceLocator) { ServiceName = interfaceType.FullName }); } else { CircuitBreakerContainer.Register(interfaceType, (ICircuitBreaker) new NullBreaker()); } foreach (MethodInfo methodInfo in interfaceType.GetMethods().Length == 0 ? ((IEnumerable <Type>)interfaceType.GetInterfaces()).First <Type>().GetMethods() : interfaceType.GetMethods()) { try { RetryAttribute retryAttribute = methodInfo.GetCustomAttribute <RetryAttribute>() ?? customAttribute; this._serviceLocator.GetService <ILogger>()?.Message("Initializing client action " + interfaceType.Name + "." + methodInfo.Name); IRoute template = (IRoute)methodInfo.GetCustomAttribute <IRouteAttribute>() ?? (IRoute)methodInfo.GetCustomAttribute <VerbAttribute>(); string actionName = this.GetActionName(methodInfo); List <VerbAttribute> list = methodInfo.GetCustomAttributes(true).OfType <VerbAttribute>().ToList <VerbAttribute>(); ActionWrapper action = new ActionWrapper() { Name = actionName, ReturnType = methodInfo.ReturnType, RouteTemplate = ExtensionsFactory.GetRouteTemplate(routePrefix, template, methodInfo, this._serviceLocator), Parameters = new List <ParameterWrapper>() }; MethodInfo method = methodInfo; IServiceProvider serviceLocator = this._serviceLocator; List <HttpMethod> httpMethods = ExtensionsFactory.GetHttpMethods(list, method, serviceLocator); List <IHeaderInspector> headerInspectors = ExtensionsFactory.GetHeaderInspectors(methodInfo, this._serviceLocator); action.UseXml = methodInfo.GetCustomAttributes().OfType <UseXmlAttribute>().Any <UseXmlAttribute>(); action.CustomHandlers = headerInspectors.Where <IHeaderInspector>((Func <IHeaderInspector, bool>)(h => this.headerHandlers.All <IHeaderHandler>((Func <IHeaderHandler, bool>)(parent => parent.GetType() != h.GetType())))).ToList <IHeaderInspector>(); action.ErrorHandler = this._errorInterceptor; action.Actions = httpMethods; if (retryAttribute != null) { action.Retry = true; action.Interval = retryAttribute.RetryInterval; action.NumberOfRetries = retryAttribute.NumberOfRetries; action.IncrementalRetry = retryAttribute.IncremetalWait; if (retryAttribute.ErrorCategorizer != (Type)null) { action.ErrorCategorizer = (IErrorCategorizer)Activator.CreateInstance(retryAttribute.ErrorCategorizer, (object)this._serviceLocator); } } ExtensionsFactory.BuildParameterInfo(methodInfo, action, this._serviceLocator); concurrentDictionary2.TryAdd(action.Name, action); } catch (Exception ex) { this._logger?.Error(ex); throw; } } if (RestWrapper.cache.TryGetValue(interfaceType, out concurrentDictionary1)) { return; } RestWrapper.cache.TryAdd(interfaceType, concurrentDictionary2); }