public async Task PinSubContext()
        {
            var targetTime  = TimeSpan.FromMilliseconds(12312456);
            var targetTime1 = targetTime + TimeSpan.FromMinutes(1);
            var targetTime2 = targetTime + TimeSpan.FromMinutes(2);

            using (ClockTimer.Pin(targetTime))
            {
                Task <TimeSpan> task1 = this.DelayedDate(targetTime1);
                Task <TimeSpan> task2 = this.DelayedDate(targetTime2);
                TimeSpan[]      dates = await Task.WhenAll(task1, task2);

                var timer = ClockTimer.StartNew();
                timer.Stop();
                var ambiantDate = timer.Elapsed;

                Assert.Contains(targetTime1, dates);
                Assert.Contains(targetTime2, dates);
                Assert.Equal(targetTime, ambiantDate);
            }

            var nowtimer = ClockTimer.StartNew();

            nowtimer.Stop();
            var now = nowtimer.Elapsed;

            // we move away from the pinned after the using statement
            Assert.NotEqual(targetTime, now);
        }
Ejemplo n.º 2
0
        private static void logTime()
        {
            var w = ClockTimer.StartNew();

            Console.Write(Clock.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            Thread.Sleep(100);
            w.Stop();
            Console.WriteLine($" - in {w.Elapsed}");
        }
        public async Task <TimeSpan> DelayedNow(bool continueOnCapturedContext)
        {
            await Task.Delay(1).ConfigureAwait(continueOnCapturedContext); // to force a proper delay

            var timer = ClockTimer.StartNew();

            timer.Stop();
            return(timer.Elapsed);
        }
        public async Task <TimeSpan> DelayedDate(TimeSpan pinnedDate)
        {
            using (ClockTimer.Pin(pinnedDate))
            {
                var timer = ClockTimer.StartNew();
                await Task.Delay(1); // to force a proper delay

                timer.Stop();
                return(timer.Elapsed);
            }
        }
        private async Task RunTest(int count)
        {
            var targetTime = TimeSpan.FromMilliseconds(12312456);

            targetTime = targetTime + TimeSpan.FromMinutes(count);

            using (ClockTimer.Pin(targetTime))
            {
                Task <TimeSpan> task1 = this.DelayedNow(true);
                Task <TimeSpan> task2 = this.DelayedNow(false);
                Task <TimeSpan> task3 = this.DelayedNow(true);
                TimeSpan[]      dates = await Task.WhenAll(task1, task2, task3);

                Assert.All(dates, x =>
                {
                    Assert.Equal(targetTime, x);
                });
            }

            // we move away from the pinned after the using statement
            var timer = ClockTimer.StartNew();

            timer.Stop();
            var now = timer.Elapsed;

            Assert.NotEqual(targetTime, now);

            using (ClockTimer.Pin(targetTime))
            {
                Task <TimeSpan> task1 = this.DelayedNow(true);
                Task <TimeSpan> task2 = this.DelayedNow(false);
                Task <TimeSpan> task3 = this.DelayedNow(true);
                TimeSpan[]      dates = await Task.WhenAll(task1, task2, task3);

                Assert.All(dates, x =>
                {
                    Assert.Equal(targetTime, x);
                });
            }
        }