public void Now_ShouldReturnADateTimeWithLocalKind()
        {
            var service = new DefaultTimeService();
            var now     = service.Now;

            Assert.That(now.Kind, Is.EqualTo(DateTimeKind.Local));
        }
        public void UtcNow_ShouldReturnADateTimeWithUtcKind()
        {
            var service = new DefaultTimeService();
            var now     = service.UtcNow;

            Assert.That(now.Kind, Is.EqualTo(DateTimeKind.Utc));
        }
        public void Now_ShouldReflectTheCurrentTime()
        {
            var service = new DefaultTimeService();

            var before = DateTime.Now;
            var now    = service.Now;
            var after  = DateTime.Now;

            Assert.That(now, Is.GreaterThanOrEqualTo(before));
            Assert.That(now, Is.LessThanOrEqualTo(after));
        }
        public void Delay_ShouldReturnATaskThatEffectsTheRequestedDelay(Func <ITimeService, Task> createTask)
        {
            var service = new DefaultTimeService();

            var start = DateTime.UtcNow;
            var task  = createTask(service);
            var end   = DateTime.UtcNow;

            task.Wait();
            var now = DateTime.UtcNow;

            Assert.That(now, Is.GreaterThanOrEqualTo(start + DelayTimeSpan - Leeway));
            Assert.That(now, Is.LessThanOrEqualTo(end + DelayTimeSpan + Leeway));
        }
        public void Delay_WhenCreatedWithACancellationToken_ShouldBeCancellable(
            Func <ITimeService, CancellationToken, Task> createTask)
        {
            var service = new DefaultTimeService();

            var tokenSource = new CancellationTokenSource();

            Task.Delay(CancelTimeSpan).ContinueWith(_ => tokenSource.Cancel());

            var start = DateTime.UtcNow;
            var task  = createTask(service, tokenSource.Token);
            var end   = DateTime.UtcNow;

            var aggregateException = Assert.Throws <AggregateException>(task.Wait);
            var now = DateTime.UtcNow;

            Assert.That(aggregateException.InnerExceptions.First(), Is.InstanceOf <TaskCanceledException>());
            Assert.That(now, Is.GreaterThanOrEqualTo(start + CancelTimeSpan - Leeway));
            Assert.That(now, Is.LessThanOrEqualTo(end + CancelTimeSpan + Leeway));
        }