Example #1
0
        public void CanBeReset()
        {
            var timesWaited  = new List <TimeSpan>();
            var backoffTimes = new[]
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(2),
            };
            var helper = new BackoffHelper(backoffTimes)
            {
                waitAction = timesWaited.Add
            };

            helper.Wait();
            helper.Wait();

            helper.Reset();

            helper.Wait();
            helper.Wait();

            timesWaited.Count.ShouldBe(4);
            timesWaited[0].ShouldBe(TimeSpan.FromSeconds(1));
            timesWaited[1].ShouldBe(TimeSpan.FromSeconds(2));
            timesWaited[2].ShouldBe(TimeSpan.FromSeconds(1));
            timesWaited[3].ShouldBe(TimeSpan.FromSeconds(2));
        }
Example #2
0
        public void WaitsForAsLongAsTheGivenBackoffTimesSpecify()
        {
            var timesWaited  = new List <TimeSpan>();
            var backoffTimes = new []
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(2),
                TimeSpan.FromSeconds(3),
            };
            var helper = new BackoffHelper(backoffTimes)
            {
                waitAction = timesWaited.Add
            };

            helper.Wait();
            helper.Wait();
            helper.Wait();

            timesWaited.Count.ShouldBe(3);
            timesWaited[0].ShouldBe(TimeSpan.FromSeconds(1));
            timesWaited[1].ShouldBe(TimeSpan.FromSeconds(2));
            timesWaited[2].ShouldBe(TimeSpan.FromSeconds(3));
        }
Example #3
0
        public void KeepsWaitingForAsLongAsTheLastTimeSpanSpecifies()
        {
            var timesWaited  = new List <TimeSpan>();
            var backoffTimes = new []
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(2),
            };
            var helper = new BackoffHelper(backoffTimes)
            {
                waitAction = timesWaited.Add
            };

            helper.Wait();
            helper.Wait();
            helper.Wait();
            helper.Wait();
            helper.Wait();

            timesWaited.Count.ShouldBe(5);
            timesWaited[2].ShouldBe(TimeSpan.FromSeconds(2));
            timesWaited[3].ShouldBe(TimeSpan.FromSeconds(2));
            timesWaited[4].ShouldBe(TimeSpan.FromSeconds(2));
        }
            async Task DoWork()
            {
                using (var op = _parallelOperationsManager.TryBegin())
                {
                    if (!op.CanContinue())
                    {
                        return;
                    }

                    using (var transactionContext = new DefaultTransactionContext())
                    {
                        AmbientTransactionContext.Current = transactionContext;
                        try
                        {
                            var message = await _transport.Receive(transactionContext);

                            if (message == null)
                            {
                                // finish the tx and wait....
                                await transactionContext.Complete();

                                await _backoffHelper.Wait();

                                return;
                            }

                            _backoffHelper.Reset();

                            var context = new IncomingStepContext(message, transactionContext);
                            transactionContext.Items[StepContext.StepContextKey] = context;

                            var stagedReceiveSteps = _pipeline.ReceivePipeline();

                            await _pipelineInvoker.Invoke(context, stagedReceiveSteps);

                            await transactionContext.Complete();
                        }
                        catch (Exception exception)
                        {
                            _log.Error(exception, "Unhandled exception in task worker");
                        }
                        finally
                        {
                            AmbientTransactionContext.Current = null;
                        }
                    }
                }
            }