Exemple #1
0
        public void Reset()
        {
            Instant   instant1 = Instant.FromUnixTimeTicks(100L);
            Instant   instant2 = Instant.FromUnixTimeTicks(500L);
            FakeClock clock    = new FakeClock(instant1);

            Assert.AreEqual(instant1, clock.GetCurrentInstant());
            clock.Reset(instant2);
            Assert.AreEqual(instant2, clock.GetCurrentInstant());
        }
        public void Reset()
        {
            Instant   instant1 = new Instant(100L);
            Instant   instant2 = new Instant(500L);
            FakeClock clock    = new FakeClock(instant1);

            Assert.AreEqual(instant1, clock.Now);
            clock.Reset(instant2);
            Assert.AreEqual(instant2, clock.Now);
        }
Exemple #3
0
        public static FakeClock SetToIso(this FakeClock clock, string offsetDateTimePatternIso)
        {
            clock.Reset(
                OffsetDateTimePattern
                .GeneralIso
                .Parse(offsetDateTimePatternIso)
                .Value
                .ToInstant()
                );

            return(clock);
        }
Exemple #4
0
        public void CanNotSellAnExpiredPie(
            string expiryDateString,
            string currentDateString,
            [Frozen(Matching.ImplementedInterfaces)] FakeClock fakeClock,
            [Frozen(Matching.ImplementedInterfaces)] ProductValidationService productValidationService,
            [Frozen(Matching.ImplementedInterfaces)] DiscountService discountService,
            Basket basket)
        {
            productValidationService.AddValidator(new ExpiredPieValidator(fakeClock));

            LocalDate expiryDate  = LocalDatePattern.CreateWithCurrentCulture("dd/MM/yyyy").Parse(expiryDateString).Value;
            Instant   currentDate = InstantPattern.CreateWithCurrentCulture("dd/MM/yyyy").Parse(currentDateString).Value;

            fakeClock.Reset(currentDate);

            Pie pie = new Pie(default(Price), expiryDate);

            AddResult result = basket.Add(pie);

            result.Success.Should().BeFalse();
        }
        public T Run <T>(Func <Task <T> > taskProvider)
        {
            var simulatedTimeout      = GetCurrentInstant() + Duration.FromHours(24);
            var realCts               = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            Task <Task <T> > mainTask = Task <Task <T> > .Factory.StartNew(taskProvider, CancellationToken.None, TaskCreationOptions.None, _taskScheduler);

            while (true)
            {
                // Run all tasks
                bool ranAll = _taskScheduler.RunUntilIdle(realCts.Token);
                if (!ranAll)
                {
                    throw new SchedulerException("Real time has reached timeout. Probably caused by recusive task creation.");
                }
                if (mainTask.IsCompleted && mainTask.Result.IsCompleted)
                {
                    return(mainTask.Result.Result);
                }

                /*if (mainTask.IsCompleted)
                 * {
                 *  if (mainTask.Exception != null)
                 *  {
                 *      throw mainTask.Exception;
                 *  }
                 *  if (mainTask.Result.IsCompleted)
                 *  {
                 *      if (mainTask.Result.Exception != null)
                 *      {
                 *          throw mainTask.Result.Exception;
                 *      }
                 *      return mainTask.Result.Result;
                 *  }
                 * }*/
                // Cancel Tasks, or move to next clock time
                var tasksToComplete = new List <TaskCompletionSource <int> >();
                lock (_lock)
                {
                    if (_delays.Count == 0)
                    {
                        throw new SchedulerException("Inconsistent state, delay queue should have content. This is probably caused by a misconfigured await.");
                    }
                    bool anyCancelled = false;
                    var  node         = _delays.First;
                    while (node != null)
                    {
                        var next = node.Next;
                        if (node.Value.CancellationToken.IsCancellationRequested)
                        {
                            node.Value.Tcs.SetCanceled();
                            _delays.Remove(node);
                            anyCancelled = true;
                        }
                        node = next;
                    }
                    if (!anyCancelled)
                    {
                        var delayTask = _delays.First.Value;
                        while (_delays.Count > 0 && _delays.First.Value.Scheduled <= delayTask.Scheduled)
                        {
                            tasksToComplete.Add(_delays.First.Value.Tcs);
                            _delays.RemoveFirst();
                        }
                        _fakeClock.Reset(delayTask.Scheduled);
                        if (GetCurrentInstant() > simulatedTimeout)
                        {
                            throw new SchedulerException("Simulated time has reached timeout.");
                        }
                    }
                }
                // Results must be set after the clock has changed, and outside the lock
                foreach (var tcs in tasksToComplete)
                {
                    tcs.SetResult(0);
                }
            }
        }
Exemple #6
0
        public void ConvertDateStringIntoDateTime()
        {
            string timeZone = "America/Los_Angeles";

            fakeClock.Reset(GetLocalInstant(new LocalDateTime(2020, 1, 15, 11, 30), timeZone));

            ZonedDateTime nextDayDateTime = helperMethods.ConvertDateStringIntoDateTime("day", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 16, 12, 0), timeZone),
                         nextDayDateTime);

            ZonedDateTime sundayDateTime = helperMethods.ConvertDateStringIntoDateTime("sunday", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 19, 12, 0), timeZone),
                         sundayDateTime);

            ZonedDateTime mondayDateTime = helperMethods.ConvertDateStringIntoDateTime("monday", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 20, 12, 0), timeZone),
                         mondayDateTime);

            ZonedDateTime tuesdayDateTime = helperMethods.ConvertDateStringIntoDateTime("tuesday", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 21, 12, 0), timeZone),
                         tuesdayDateTime);

            ZonedDateTime wednesdayDateTime = helperMethods.ConvertDateStringIntoDateTime("wednesday", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 22, 12, 0), timeZone),
                         wednesdayDateTime);

            ZonedDateTime thursdayDateTime = helperMethods.ConvertDateStringIntoDateTime("thursday", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 16, 12, 0), timeZone),
                         thursdayDateTime);

            ZonedDateTime fridayDateTime = helperMethods.ConvertDateStringIntoDateTime("friday", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 17, 12, 0), timeZone),
                         fridayDateTime);

            ZonedDateTime saturdayDateTime = helperMethods.ConvertDateStringIntoDateTime("saturday", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 18, 12, 0), timeZone),
                         saturdayDateTime);

            ZonedDateTime beginningOfTheMonthDateTime = helperMethods.ConvertDateStringIntoDateTime("beginning of the month", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 2, 1, 12, 0), timeZone),
                         beginningOfTheMonthDateTime);

            ZonedDateTime endOfTheMonthDateTime = helperMethods.ConvertDateStringIntoDateTime("end of the month", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 31, 12, 0), timeZone),
                         endOfTheMonthDateTime);

            ZonedDateTime dateAfterNowDateTime = helperMethods.ConvertDateStringIntoDateTime("18", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 18, 12, 0), timeZone),
                         dateAfterNowDateTime);

            ZonedDateTime dateBeforeNowDateTime = helperMethods.ConvertDateStringIntoDateTime("14", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 2, 14, 12, 0), timeZone),
                         dateBeforeNowDateTime);

            ZonedDateTime parsedLocalDateDateTime = helperMethods.ConvertDateStringIntoDateTime("2020-01-20", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 20, 12, 0), timeZone),
                         parsedLocalDateDateTime);

            ZonedDateTime parsedLocalDateTimeDateTime = helperMethods.ConvertDateStringIntoDateTime("2020-01-20T10:30", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 20, 10, 30), timeZone),
                         parsedLocalDateTimeDateTime);

            ZonedDateTime parsedLocalDateTimeWithSecondsDateTime = helperMethods.ConvertDateStringIntoDateTime("2020-01-20T10:30:01", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 1, 20, 10, 30, 1), timeZone),
                         parsedLocalDateTimeWithSecondsDateTime);

            // "edge cases" aka February
            // on Feb 29th, requesting 30th of every month will get set to March 30th because we don't schedule
            // on the day we're on
            fakeClock.Reset(GetLocalInstant(new LocalDateTime(2020, 2, 29, 11, 30), timeZone));
            ZonedDateTime march30 = helperMethods.ConvertDateStringIntoDateTime("30", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 3, 30, 12, 0), timeZone),
                         march30);

            // on Feb 15th, requesting 30th of every month will get set to Feb 29th because Feb only has 29 days
            fakeClock.Reset(GetLocalInstant(new LocalDateTime(2020, 2, 15, 11, 30), timeZone));
            ZonedDateTime feb29 = helperMethods.ConvertDateStringIntoDateTime("30", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 2, 29, 12, 0), timeZone),
                         feb29);

            // on March 31st, requesting 31st of every month will get set to April 30th because we don't schedule
            // on the day we're on and April only has 30 days
            fakeClock.Reset(GetLocalInstant(new LocalDateTime(2020, 3, 31, 11, 30), timeZone));
            ZonedDateTime april30 = helperMethods.ConvertDateStringIntoDateTime("31", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 4, 30, 12, 0), timeZone),
                         april30);

            // on Jan 31st, requesting the 31st of every month will get set to Feb 29th because we don't schedule
            // on the day we're on and Feb only has 29 days (in a leap year)
            fakeClock.Reset(GetLocalInstant(new LocalDateTime(2020, 1, 31, 11, 30), timeZone));
            ZonedDateTime feb292 = helperMethods.ConvertDateStringIntoDateTime("31", timeZone, fakeClock);

            Assert.Equal(CreateZonedDateTime(new LocalDateTime(2020, 2, 29, 12, 0), timeZone),
                         feb292);
        }