예제 #1
0
        /// <summary>
        /// Starts observing commits and dispatching them..
        /// </summary>
        /// <returns></returns>
        public async Task Start()
        {
            if (_isStarted.EnsureCalledOnce())
            {
                return;
            }

            string checkpointToken = await _checkpointRepository.Get();

            /* string checkpointToken = null;
             * await _retryPolicy.Retry(async () => checkpointToken = await _checkpointRepository.Get(), _disposed.Token); //TODO should have different retry policy? */
            _subscription = _eventStoreClient.Subscribe(checkpointToken, async commit =>
            {
                try
                {
                    await _retryPolicy.Retry(() => _dispatchCommit(commit, _disposed.Token), _disposed.Token);
                    await _retryPolicy.Retry(() => _checkpointRepository.Put(commit.CheckpointToken), _disposed.Token);
                }
                catch (Exception ex)
                {
                    Logger.ErrorException(
                        Messages.ExceptionHasOccuredWhenDispatchingACommit.FormatWith(commit.ToString()),
                        ex);
                    _projectedCommits.OnError(ex);
                    throw;
                }
                _projectedCommits.OnNext(commit);
            });
        }
        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>();
        }
        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 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);
        }