Example #1
0
        private async Task TickTo(DateTime toDate, bool throws)
        {
            if (toDate < _dateService.GetNow())
            {
                throw new ArgumentException("Cannot run to a date prior to what is currently now in the date simulation service.");
            }

            while (true)
            {
                var now = _dateService.GetNow();

                var job = await _store.AcquireJobAsync();

                if (job != null)
                {
                    await ExecuteAndReleaseJobAsync(job, throws);
                }
                else if (now < toDate)
                {
                    var next = await _store.GetNextJobDueTimeAsync();

                    _dateService.SetNow(next ?? toDate);
                }
                else
                {
                    break;
                }
            }
        }
        public async Task Advance_To_Date_Multiple_Jobs()
        {
            //Arrange
            var job = new JobDescription
            {
                Id   = Guid.NewGuid(),
                Type = typeof(TestJobWithoutDependencies).AssemblyQualifiedName,
            };


            var job2 = new JobDescription
            {
                Id      = Guid.NewGuid(),
                Type    = typeof(TestJobWithoutDependencies).AssemblyQualifiedName,
                DueTime = new DateTime(2018, 2, 2)
            };

            _testingBatchStore.GetNextJobDueTimeAsync().Returns(Task.FromResult((DateTime?)job2.DueTime), Task.FromResult((DateTime?)null));

            var now = new DateTime(2000, 1, 1);
            var to  = new DateTime(2019, 2, 2);

            _dateSimulationService.GetNow().Returns(now, now, now, job2.DueTime, job2.DueTime, to);

            _testingBatchStore.AcquireJobAsync().Returns(Task.FromResult(job), Task.FromResult((JobDescription)null), Task.FromResult(job2), Task.FromResult((JobDescription)null));

            //Act
            await _engine.AdvanceToDateAsync(to);

            //Assert
            _dateSimulationService.Received(6).GetNow();
            _dateSimulationService.Received(1).SetNow(job2.DueTime);
            _dateSimulationService.Received(1).SetNow(to);
            await _testingBatchStore.Received(2).GetNextJobDueTimeAsync();

            await _testingBatchStore.Received(5).AcquireJobAsync();

            await _testingBatchStore.Received(1).ReleaseJobAsync(job.Id, Arg.Is <JobResult>(x => x.State == ExecutionState.Finished));

            await _testingBatchStore.Received(1).ReleaseJobAsync(job2.Id, Arg.Is <JobResult>(x => x.State == ExecutionState.Finished));
        }