public void InvalidExceptionShouldNotBeTransient() { // this was throwing an exception during provider lookup RetryStrategy retry = new RetryStrategy(); Assert.IsFalse(retry.IsTransientException(new InvalidOperationException())); }
public void TestRetryStrategyMultiThread(CallType callType) { var searchConfig = new SearchConfig("appId", "apiKey"); RetryStrategy retryStrategy = new RetryStrategy(searchConfig); var initialHosts = retryStrategy.GetTryableHost(callType); Assert.True(initialHosts.Count() == 4); Task task1 = Task.Run(() => { var hosts = retryStrategy.GetTryableHost(callType); retryStrategy.Decide(hosts.ElementAt(0), 200, false); Console.WriteLine(Thread.CurrentThread.Name); }); Task task2 = Task.Run(() => { var hosts = retryStrategy.GetTryableHost(callType); retryStrategy.Decide(hosts.ElementAt(0), 500, false); }); Task.WaitAll(task1, task2); var updatedHosts = retryStrategy.GetTryableHost(callType); Assert.True(updatedHosts.Count() == 3); }
/// <summary> /// Initializes a new instance of the <see cref="TopicSender" /> class, /// automatically creating the given topic if it does not exist. /// </summary> protected TopicSender(ServiceBusSettings settings, string topic, RetryStrategy retryStrategy) { this.settings = settings; this.topic = topic; tokenProvider = TokenProvider.CreateSharedSecretTokenProvider(settings.TokenIssuer, settings.TokenAccessKey); serviceUri = ServiceBusEnvironment.CreateServiceUri(settings.ServiceUriScheme, settings.ServiceNamespace, settings.ServicePath); // TODO: This could be injected. retryPolicy = new RetryPolicy <ServiceBusTransientErrorDetectionStrategy>(retryStrategy); retryPolicy.Retrying += (s, e) => { var handler = Retrying; if (handler != null) { handler(this, EventArgs.Empty); } Trace.TraceWarning("An error occurred in attempt number {1} to send a message: {0}", e.LastException.Message, e.CurrentRetryCount); }; var factory = MessagingFactory.Create(serviceUri, tokenProvider); topicClient = factory.CreateTopicClient(this.topic); }
public void TestRetryStrategyMultiThread(CallType callType) { var searchConfig = new SearchConfig("appId", "apiKey"); RetryStrategy retryStrategy = new RetryStrategy(searchConfig); var initialHosts = retryStrategy.GetTryableHost(callType); Assert.That(initialHosts, Has.Exactly(4).Items); Task task1 = Task.Run(() => { var hosts = retryStrategy.GetTryableHost(callType); retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse { HttpStatusCode = 200 }); Console.WriteLine(Thread.CurrentThread.Name); }); Task task2 = Task.Run(() => { var hosts = retryStrategy.GetTryableHost(callType); retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse { HttpStatusCode = 500 }); }); Task.WaitAll(task1, task2); var updatedHosts = retryStrategy.GetTryableHost(callType); Assert.That(updatedHosts, Has.Exactly(3).Items); }
public void TestRetryStrategyRetriableFailure(CallType callType, int httpErrorCode) { var searchConfig = new SearchConfig("appId", "apiKey"); RetryStrategy retryStrategy = new RetryStrategy(searchConfig); var hosts = retryStrategy.GetTryableHost(callType); Assert.True(hosts.Count(h => h.Up) == 4); var decision = retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse { HttpStatusCode = httpErrorCode }); Assert.True(decision.HasFlag(RetryOutcomeType.Retry)); var updatedHosts = retryStrategy.GetTryableHost(callType); Assert.True(updatedHosts.Count(h => h.Up) == 3); var decisionAfterNetworkError = retryStrategy.Decide(hosts.ElementAt(0), new AlgoliaHttpResponse { IsNetworkError = true }); Assert.True(decisionAfterNetworkError.HasFlag(RetryOutcomeType.Retry)); var updatedHostsAfterNetworkError = retryStrategy.GetTryableHost(callType); Assert.True(updatedHostsAfterNetworkError.Count(h => h.Up) == 2); }
public static IEnumerable <TSource> Retry <TSource, TException>( this IEnumerable <TSource> source, RetryStrategy retryStrategy = null, Func <Exception, bool> isTransient = null, EventHandler <RetryingEventArgs> retryingHandler = null, Func <TException, bool> @catch = null) where TException : Exception { List <TSource> results = new List <TSource>(); try { Execute( () => { foreach (TSource value in source.Skip(results.Count)) { results.Add(value); } }, retryStrategy, isTransient, retryingHandler); } catch (TException exception) when(@catch?.Invoke(exception) ?? true) { } return(results.Hide()); }
/// <summary> /// Provides the async implementation of the retry mechanism for unreliable actions and transient conditions. /// </summary> /// <param name="action">Unreliable action to execute.</param> /// <param name="retryStrategy">Retry strategy.</param> /// <param name="cancellationToken">Cancellation token.</param> /// <param name="transientExceptions">Transient exceptions.</param> /// <returns>Task that specified when action executed successfully or with error after all retries.</returns> public static Task RetryAsync( Func <Task> action, RetryStrategy retryStrategy, CancellationToken cancellationToken = default(CancellationToken), params Type[] transientExceptions) { Guard.IsNotNull(action, nameof(action)); Guard.IsNotNull(retryStrategy, nameof(retryStrategy)); // Try to convert generic task to non-generic. Func <Task <int> > nonGenericAction = () => { var tcs = new TaskCompletionSource <int>(); action().ContinueWith(t => { if (t.IsFaulted && t.Exception != null) { tcs.TrySetException(t.Exception.InnerExceptions); } else if (t.IsCanceled) { tcs.TrySetCanceled(); } else if (t.IsCompleted) { tcs.SetResult(0); } }, cancellationToken, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Current); return(tcs.Task); }; return(RetryAsyncInternal <int>(nonGenericAction, 1, retryStrategy, cancellationToken, transientExceptions)); }
public RelayServiceHostController(Func <ServiceHost> createServiceHost) { this.createServiceHost = createServiceHost; // Use exponential backoff in case of failures. Retry forever retryStrategy = new ExponentialBackoff(100000, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(60), TimeSpan.FromSeconds(1)); }
public void ShouldRetryHttpStatusCodes(HttpStatusCode statusCode, int maxFailCount, int expectedTryCount) { var tryCount = 0; var shouldRetry = new HttpTransientShouldRetry(maxFailCount); var nextWait = new NoWait(); var strategy = new RetryStrategy(shouldRetry, nextWait); try { Retry.Run(() => { tryCount++; // make sure it retries the number of expected times by throwing an exception var response = HttpMock.CreateWebResponse(statusCode, null); // using WebExceptionStatus.MessageLengthLimitExceeded because that is one that never triggers a retry // so we can be sure that we are testing the HttpStatusCode instead of WebException.Status throw new WebException("test", null, WebExceptionStatus.MessageLengthLimitExceeded, response); }, strategy); Assert.Fail("should have thrown an exception"); } catch (Exception) { Assert.AreEqual(expectedTryCount, tryCount); } }
public QueueClientConfiguration(string connectionString, ReceiveMode receiveMode, string queueName) { this.ConnectionString = connectionString; this.ReceiveMode = receiveMode; this.QueueName = queueName; this.RetryStrategy = RetryStrategy.CreateDefault(); }
public static RetryPolicy GetDefaultSqlCommandRetryPolicy() { RetryStrategy retryStrategy = RetryStrategy.DefaultFixed; var retryPolicy = new RetryPolicy(new NetworkConnectivityErrorDetectionStrategy(), retryStrategy); return(retryPolicy); }
public static RetryPolicy GetDefaultSqlAzureCommandRetryPolicy() { RetryStrategy retryStrategy = RetryStrategy.DefaultFixed; var retryPolicy = new RetryPolicy(new SqlAzureTransientErrorDetectionStrategy(), retryStrategy); return(retryPolicy); }
/// <summary> /// Configures runtime parameters of the async endpoint executor state machine /// </summary> /// <param name="timeout">Timeout for sending messages to the external endpoint and for checkpointing</param> /// <param name="retryStrategy">Timeout for sending messages to the external endpoint and for checkpointing</param> /// <param name="revivePeriod">Time spent in the Dead state before attempting to send messages to an endpoint again</param> /// <param name="throwOnDead">Should complete with exception when dead instead of transitioning to dead state</param> public EndpointExecutorConfig(TimeSpan timeout, RetryStrategy retryStrategy, TimeSpan revivePeriod, bool throwOnDead) { this.Timeout = timeout; this.RetryStrategy = retryStrategy; this.RevivePeriod = revivePeriod; this.ThrowOnDead = throwOnDead; }
public static RetryPolicy GetDefaultSqlAzureConnectionRetryPolicy() { RetryStrategy retryStrategy = RetryStrategy.DefaultExponential; var retryPolicy = new RetryPolicy(new SqlAzureTransientErrorDetectionStrategy(), retryStrategy); return(retryPolicy); }
public AddItemCommandHandler(ICartRepository cartRepository, IProductApi productApi, RetryStrategy retryStrategy, ILogger logger) { this.cartRepository = cartRepository; this.productApi = productApi; this.retryStrategy = retryStrategy; this.logger = logger; }
internal EdgeAgentConnection( IModuleClientProvider moduleClientProvider, ISerde <DeploymentConfig> desiredPropertiesSerDe, IRequestManager requestManager, IDeviceManager deviceManager, bool enableSubscriptions, TimeSpan refreshConfigFrequency, RetryStrategy retryStrategy, IDeploymentMetrics deploymentMetrics, Option <X509Certificate2> manifestTrustBundle, TimeSpan twinPullOnConnectThrottleTime) { this.desiredPropertiesSerDe = Preconditions.CheckNotNull(desiredPropertiesSerDe, nameof(desiredPropertiesSerDe)); this.deploymentConfigInfo = Option.None <DeploymentConfigInfo>(); this.reportedProperties = Option.None <TwinCollection>(); this.moduleConnection = new ModuleConnection(moduleClientProvider, requestManager, this.OnConnectionStatusChanged, this.OnDesiredPropertiesUpdated, enableSubscriptions); this.retryStrategy = Preconditions.CheckNotNull(retryStrategy, nameof(retryStrategy)); this.refreshTwinTask = new PeriodicTask(this.ForceRefreshTwin, refreshConfigFrequency, refreshConfigFrequency, Events.Log, "refresh twin config"); this.pullOnReconnect = enableSubscriptions; this.deviceManager = Preconditions.CheckNotNull(deviceManager, nameof(deviceManager)); Events.TwinRefreshInit(refreshConfigFrequency); this.deploymentMetrics = Preconditions.CheckNotNull(deploymentMetrics, nameof(deploymentMetrics)); this.initTask = this.ForceRefreshTwin(); this.manifestTrustBundle = manifestTrustBundle; this.twinPullOnConnectThrottleTime = twinPullOnConnectThrottleTime; }
private static void RunBasicTests(RetryStrategy strategy, int expectedTimeMs) { DateTime start = DateTime.UtcNow; var retry = new RetryPoliciy<AllAreTransient>(strategy); int tries = 0; Assert.Throws<MockException>( () => retry.Execute( attempt => { ++tries; throw new MockException("Deliberately fail"); }) ); DateTime finish = DateTime.UtcNow; TimeSpan time = finish.Subtract(start); Assert.AreEqual(ATTEMPTS, tries); // We expect that it could take longer than expected, but never LESS. Assert.GreaterOrEqual(expectedTimeMs, time.TotalMilliseconds, "Delay was too short."); }
public void RetryPolicy_WhenException_Retry(bool isPositive, bool expectedException, int retriesConfiguration, int expectedExecutions) { var policyRegistry = new StrategyBuilder(); var timesExecuted = 0; Exception exception = null; var strategy = new RetryStrategy { RetryCount = retriesConfiguration, OnRetry = (ex) => exception = ex }; var policy = policyRegistry.Add <Exception>(strategy).Build(); var executionResult = policy.Execute(() => { timesExecuted++; var num = GetNumber(isPositive); if (num <= 0) { throw new Exception(); } return(num); }); Assert.Equal(expectedExecutions, timesExecuted); Assert.Equal(expectedException, executionResult.Exception != null); Assert.Equal(expectedException, exception != null); }
protected override AddItemCommandHandler CreateCommandHandler() { return(new AddItemCommandHandler(Repository, productApiStub.Object, RetryStrategy.CreateAddItemCommandRetryStrategy(), loggerMock.Object)); }
public void TestReliableConnection() { int retries = 0; var retryStrategy = new RetryStrategy(); retryStrategy.MaxRetryCount = 1; retryStrategy.Retrying += (sender, re) => { Console.WriteLine("Retrying. Attempt {0}", re.Attempt); retries++; }; try { DB2ConnectionStringBuilder connectionStringBuilder = new DB2ConnectionStringBuilder(); connectionStringBuilder.ConnectionString = "Server=testserver:9999;Database=SAMPLE"; connectionStringBuilder.UserID = "db2admin"; connectionStringBuilder.Password = "******"; using (var reliable = new ReliableConnection <DB2Connection>(connectionStringBuilder.ConnectionString, retryStrategy)) { reliable.Open(); } } catch { } Assert.AreEqual(1, retries); }
public void HopTest2RetriesWrongExceptionMessage() { var strategy = new RetryStrategy(0, 0).Handle <IndexOutOfRangeException>(e => e.Message == "not this"); Assert.ThrowsException <IndexOutOfRangeException>( () => CreateZoZo(strategy).Hop() ); }
IZoZo CreateZoZo(params int[] retries) { var retryStrategy = new RetryStrategy( retries.Select(_ => TimeSpan.FromMilliseconds(_)) ).Handle <IndexOutOfRangeException>(e => e.Message == "not tried often enough"); return(CreateZoZo(retryStrategy)); }
public TopicClientConfiguration(string connectionString, ReceiveMode receiveMode, string topicName, string subscriptionName) { this.ConnectionString = connectionString; this.ReceiveMode = receiveMode; this.TopicName = topicName; this.SubscriptionName = subscriptionName; this.RetryStrategy = RetryStrategy.CreateDefault(); }
protected void GivenProductAlreadyExistsInCart(Guid productId, Guid customerId) { new AddItemCommandHandler(Repository, CreateProductApiStub().Object, RetryStrategy.CreateAddItemCommandRetryStrategy(), CreateLoggerMock().Object) .Handle(new AddItemCommand(customerId, productId)); }
public void RetryStrategyShouldCompleteWhenHandlerThrows() { RetryStrategy s = new RetryStrategy(); s.Retrying += (sender, re) => { throw new ApplicationException(); }; Assert.Throws<AggregateException>(() => s.ExecuteWithRetryAsync<int>(null, () => Task<int>.Factory.StartNew(() => { throw new ApplicationException(); })).Wait()); }
public void RetryStrategyShouldCompleteWhenHandlerThrows() { RetryStrategy s = new RetryStrategy(); s.Retrying += (sender, re) => { throw new Exception(); }; Assert.Throws <AggregateException>(() => s.ExecuteWithRetryAsync <int>(null, () => Task <int> .Factory.StartNew(() => { throw new Exception(); })).Wait()); }
public void HopTest2RetriesWrongExceptionType() { var strategy = new RetryStrategy(0, 0).Handle <InvalidCastException>(); Assert.ThrowsException <IndexOutOfRangeException>( () => CreateZoZo(strategy).Hop() ); }
/// <summary> /// Create a new instance of the <see cref="T:Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy" /> class with the specified retry strategy. /// </summary> /// <param name="retryStrategy">The retry strategy.</param> /// <param name="isTransient">The predicate function to detect whether the specified exception is transient.</param> /// <typeparam name="TException">The type of the transient exception.</typeparam> /// <returns>A new instance of the <see cref="T:Microsoft.Practices.EnterpriseLibrary.TransientFaultHandling.RetryPolicy" /> class.</returns> public static RetryPolicy Catch <TException>( this RetryStrategy retryStrategy, Func <TException, bool>?isTransient = null) where TException : Exception => CreateRetryPolicy( retryStrategy.NotNull(), isTransient is null ? exception => exception is TException : exception => exception is TException specifiedException && isTransient(specifiedException));
private void OnFailed(Exception e, RetryStrategy retryStrategy) { FailedEventHandler handler = Failed; if (handler != null) { handler(this, new ZumoAttachmentUploadFailedEventArgs(e, retryStrategy)); } }
public static IEnumerable <TResult> RetrySelect <TSource, TResult>( this IEnumerable <TSource> source, Func <TSource, TResult> selector, RetryStrategy retryStrategy = null, Func <Exception, bool> isTransient = null, EventHandler <RetryingEventArgs> retryingHandler = null, Func <Exception, bool?> @catch = null) => source.RetrySelect <TSource, TResult, Exception>( selector, retryStrategy, isTransient, retryingHandler, @catch);
public static IEnumerable <TResult> RetrySelect <TSource, TResult, TException>( this IEnumerable <TSource> source, Func <TSource, TResult> selector, RetryStrategy retryStrategy = null, Func <Exception, bool> isTransient = null, EventHandler <RetryingEventArgs> retryingHandler = null, Func <TException, bool?> @catch = null) where TException : Exception => source.TrySelect( value => Execute(() => selector(value), retryStrategy, isTransient, retryingHandler), @catch);
public void RetryStrategyShouldCompleteWhenFuncThrows() { RetryStrategy s = new RetryStrategy(); Assert.Throws<AggregateException>(() => s.ExecuteWithRetryAsync<int>(null, () => { throw new ApplicationException(); }).Wait()); }
public void RetryStrategyShouldCompleteWhenFuncReturnsNullTask() { RetryStrategy s = new RetryStrategy(); Assert.Throws<AggregateException>(() => s.ExecuteWithRetryAsync<int>(null, () => null).Wait()); }
public void TestReliableConnection() { int retries = 0; var retryStrategy = new RetryStrategy(); retryStrategy.MaxRetryCount = 1; retryStrategy.Retrying += (sender, re) => { Console.WriteLine("Retrying. Attempt {0}", re.Attempt); retries++; }; try { var builder = new MySqlConnectionStringBuilder(_connectionStringBuilder.ConnectionString); builder.Server = "localhost"; builder.Port = 9999; using (var reliable = new ReliableConnection<MySqlConnection>(builder.ConnectionString, retryStrategy)) { reliable.Open(); } } catch { } Assert.AreEqual(1, retries); }
public void TestReliableConnection() { int retries = 0; var retryStrategy = new RetryStrategy(); retryStrategy.MaxRetryCount = 1; retryStrategy.Retrying += (sender, re) => { Console.WriteLine("Retrying. Attempt {0}", re.Attempt); retries++; }; try { var builder = new OracleConnectionStringBuilder(_connectionStringBuilder.ConnectionString); builder.DataSource = "testserver:9999"; using (var reliable = new ReliableConnection<OracleConnection>(builder.ConnectionString, retryStrategy)) { reliable.Open(); } } catch { } Assert.AreEqual(1, retries); }
public void TestReliableConnection() { int retries = 0; var retryStrategy = new RetryStrategy(); retryStrategy.MaxRetryCount = 1; retryStrategy.Retrying += (sender, re) => { Console.WriteLine("Retrying. Attempt {0}", re.Attempt); retries++; }; try { DB2ConnectionStringBuilder connectionStringBuilder = new DB2ConnectionStringBuilder(); connectionStringBuilder.ConnectionString = "Server=testserver:9999;Database=SAMPLE"; connectionStringBuilder.UserID = "db2admin"; connectionStringBuilder.Password = "******"; using (var reliable = new ReliableConnection<DB2Connection>(connectionStringBuilder.ConnectionString, retryStrategy)) { reliable.Open(); } } catch { } Assert.AreEqual(1, retries); }
public void TestReliableConnection() { int retries = 0; var retryStrategy = new RetryStrategy(); retryStrategy.MaxRetryCount = 1; retryStrategy.Retrying += (sender, re) => { Console.WriteLine("Retrying. Attempt {0}", re.Attempt); retries++; }; try { var builder = new AseConnectionStringBuilder(); builder.ConnectionString = "Data Source=testserver;Port=9999;User ID=sa;Password=Password1"; using (var reliable = new ReliableConnection<AseConnection>(builder.ConnectionString, retryStrategy)) { reliable.Open(); } } catch { } Assert.AreEqual(1, retries); }