public void When_non_transient_exception_thrown_then_should_retry() { var sut = new TransientExceptionRetryPolicy(TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(10)); Func<Task> act = () => sut.Retry(() => { throw new Exception(); }, CancellationToken.None); act.ShouldThrow<Exception>(); }
public void When_non_transient_exception_thrown_then_should_retry() { var sut = new TransientExceptionRetryPolicy(TimeSpan.FromMilliseconds(1), TimeSpan.FromMilliseconds(10)); Func <Task> act = () => sut.Retry(() => { throw new Exception(); }, CancellationToken.None); act.ShouldThrow <Exception>(); }
/// <summary> /// Initializes a new instance of the <see cref="DurableCommitDispatcher"/> class. /// </summary> /// <param name="eventStoreClient">An event store client.</param> /// <param name="checkpointRepository">A checkpoint repository. Each instane of a <see cref="DurableCommitDispatcher"/> /// should have their own instance of a <see cref="ICheckpointRepository"/>.</param> /// <param name="handlerModules">A collection of handler modules to dispatch the commit to.</param> /// <param name="retryPolicy">A retry policy when a <see cref="TransientException"/> occurs. /// If none specified TransientException.None is used.</param> /// <exception cref="System.ArgumentNullException"> /// eventStoreClient /// or /// checkpointRepository /// or /// dispatchCommit /// </exception> public DurableCommitDispatcher( [NotNull] IEventStoreClient eventStoreClient, [NotNull] ICheckpointRepository checkpointRepository, [NotNull] IEnumerable<IHandlerResolver> handlerModules, TransientExceptionRetryPolicy retryPolicy = null): this(eventStoreClient, checkpointRepository, handlerModules.DispatchCommit, retryPolicy ) { Guard.EnsureNotNull(handlerModules, "handlerModule"); }
/// <summary> /// Initializes a new instance of the <see cref="DurableCommitDispatcher"/> class. /// </summary> /// <param name="eventStoreClient">An event store client.</param> /// <param name="checkpointRepository">A checkpoint repository. Each instane of a <see cref="DurableCommitDispatcher"/> /// should have their own instance of a <see cref="ICheckpointRepository"/>.</param> /// <param name="handlerModules">A collection of handler modules to dispatch the commit to.</param> /// <param name="retryPolicy">A retry policy when a <see cref="TransientException"/> occurs. /// If none specified TransientException.None is used.</param> /// <exception cref="System.ArgumentNullException"> /// eventStoreClient /// or /// checkpointRepository /// or /// dispatchCommit /// </exception> public DurableCommitDispatcher( [NotNull] IEventStoreClient eventStoreClient, [NotNull] ICheckpointRepository checkpointRepository, [NotNull] IEnumerable <IHandlerResolver> handlerModules, TransientExceptionRetryPolicy retryPolicy = null) : this(eventStoreClient, checkpointRepository, handlerModules.DispatchCommit, retryPolicy) { Guard.EnsureNotNull(handlerModules, "handlerModule"); }
/// <summary> /// Initializes a new instance of the <see cref="DurableCommitDispatcher"/> class. /// </summary> /// <param name="eventStoreClient">An event store client.</param> /// <param name="checkpointRepository">A checkpoint repository. Each instane of a <see cref="DurableCommitDispatcher"/> /// should have their own instance of a <see cref="ICheckpointRepository"/>.</param> /// <param name="dispatchCommit">A handler to dispatch the commit to.</param> /// <param name="retryPolicy">A retry policy when a <see cref="TransientException"/> occurs. /// If none specified TransientException.None is used.</param> /// <exception cref="System.ArgumentNullException"> /// eventStoreClient /// or /// checkpointRepository /// or /// dispatchCommit /// </exception> public DurableCommitDispatcher( [NotNull] IEventStoreClient eventStoreClient, [NotNull] ICheckpointRepository checkpointRepository, [NotNull] Func <ICommit, CancellationToken, Task> dispatchCommit, TransientExceptionRetryPolicy retryPolicy = null) { Guard.EnsureNotNull(eventStoreClient, "eventStoreClient"); Guard.EnsureNotNull(checkpointRepository, "checkpointRepository"); Guard.EnsureNotNull(dispatchCommit, "dispatchCommit"); _eventStoreClient = eventStoreClient; _checkpointRepository = checkpointRepository; _dispatchCommit = dispatchCommit; _retryPolicy = retryPolicy ?? TransientExceptionRetryPolicy.None(); }
public void When_transient_exception_thrown_indefinitely_then_should_repeatedly_retry_until_duration_and_then_throw() { var duration = TimeSpan.FromSeconds(1); var stopwatch = Stopwatch.StartNew(); var sut = new TransientExceptionRetryPolicy(TimeSpan.FromMilliseconds(1), duration); int count = 0; Func <Task> act = () => sut.Retry(() => { count++; throw new TransientException(); }, CancellationToken.None); act.ShouldThrow <TransientException>(); count.Should().BeGreaterThan(1); stopwatch.Elapsed.Should().BeGreaterThan(duration); }
public void When_transient_exception_thrown_indefinitely_then_should_repeatedly_retry_until_duration_and_then_throw() { var duration = TimeSpan.FromSeconds(1); var stopwatch = Stopwatch.StartNew(); var sut = new TransientExceptionRetryPolicy(TimeSpan.FromMilliseconds(1), duration); int count = 0; Func<Task> act = () => sut.Retry(() => { count++; throw new TransientException(); }, CancellationToken.None); act.ShouldThrow<TransientException>(); count.Should().BeGreaterThan(1); stopwatch.Elapsed.Should().BeGreaterThan(duration); }
public async Task Transient_policy_none_should_not_retry() { var sut = TransientExceptionRetryPolicy.None(); int count = 0; Func <Task> act = () => sut.Retry(() => { count++; if (count == 1) { throw new TransientException(); } return(Task.FromResult(0)); }, CancellationToken.None); act.ShouldThrow <TransientException>(); count.Should().Be(1); }
public async Task When_transient_exception_thrown_once_then_should_retry() { var sut = new TransientExceptionRetryPolicy(TimeSpan.FromMilliseconds(1), TimeSpan.FromSeconds(1)); int count = 0; var tcs = new TaskCompletionSource <bool>(); await sut.Retry(() => { count++; if (count == 2) { tcs.SetResult(true); return(Task.FromResult(0)); } throw new TransientException(); }, CancellationToken.None); await tcs.Task; count.Should().Be(2); }
public async Task When_transient_exception_thrown_once_then_should_retry() { var sut = new TransientExceptionRetryPolicy(TimeSpan.FromMilliseconds(1), TimeSpan.FromSeconds(1)); int count = 0; var tcs = new TaskCompletionSource<bool>(); await sut.Retry(() => { count++; if (count == 2) { tcs.SetResult(true); return Task.FromResult(0); } throw new TransientException(); }, CancellationToken.None); await tcs.Task; count.Should().Be(2); }
/// <summary> /// Initializes a new instance of the <see cref="DurableCommitDispatcher"/> class. /// </summary> /// <param name="eventStoreClient">An event store client.</param> /// <param name="checkpointRepository">A checkpoint repository. Each instane of a <see cref="DurableCommitDispatcher"/> /// should have their own instance of a <see cref="ICheckpointRepository"/>.</param> /// <param name="dispatchCommit">A handler to dispatch the commit to.</param> /// <param name="retryPolicy">A retry policy when a <see cref="TransientException"/> occurs. /// If none specified TransientException.None is used.</param> /// <exception cref="System.ArgumentNullException"> /// eventStoreClient /// or /// checkpointRepository /// or /// dispatchCommit /// </exception> public DurableCommitDispatcher( [NotNull] IEventStoreClient eventStoreClient, [NotNull] ICheckpointRepository checkpointRepository, [NotNull] Func<ICommit, CancellationToken, Task> dispatchCommit, TransientExceptionRetryPolicy retryPolicy = null) { Guard.EnsureNotNull(eventStoreClient, "eventStoreClient"); Guard.EnsureNotNull(checkpointRepository, "checkpointRepository"); Guard.EnsureNotNull(dispatchCommit, "dispatchCommit"); _eventStoreClient = eventStoreClient; _checkpointRepository = checkpointRepository; _dispatchCommit = dispatchCommit; _retryPolicy = retryPolicy ?? TransientExceptionRetryPolicy.None(); _compositeDisposable.Add(_commitsProjectedStream); }