Beispiel #1
0
        public void WhenScheduleIsNull_GetFireTimeAfter_ThrowsException()
        {
            // Arrange.
            var target = new ESyncTrigger();

            // Act.
            target.GetFireTimeAfter(null);
        }
Beispiel #2
0
        public void WhenNextScheduleTimeIsNull_GetFireTimeAfter_ReturnsNull()
        {
            // Arrange.
            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(Arg.IsAny<DateTime>())).Returns(null as DateTime?);

            var target = new ESyncTrigger(schedule);

            // Act.
            var result = target.GetFireTimeAfter(null);

            // Assert.
            Assert.IsNull(result);
        }
Beispiel #3
0
        public void WhenNextScheduleTimeIsGreaterThanEndTime_GetFireTimeAfter_ReturnsNull()
        {
            // Arrange.
            var endTime = new DateTime(2013, 1, 16, 0, 0, 0, DateTimeKind.Local);
            var nextTime = new DateTime(2013, 1, 16, 0, 0, 1, DateTimeKind.Local);

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(Arg.IsAny<DateTime>())).Returns(nextTime);

            var target = new ESyncTrigger(schedule) { EndTimeUtc = endTime.ToUniversalTime() };

            // Act.
            var result = target.GetFireTimeAfter(null);

            // Assert.
            Assert.IsNull(result);
        }
        public void WhenScheduleHasChanged_UpdateJob_CreatesNewTrigger()
        {
            // Arrange.
            var syncGuid = Guid.Parse("DF1F8724-297C-45AE-8E70-B924FD375620");
            var scheduleGuid = Guid.Parse("{210B8E36-B81D-4E29-AFC0-19C7CA7FCCD1}");
            var jobName = syncGuid.ToString();
            var triggerName = scheduleGuid.ToString();
            const string processName = "TestProcess";

            var newSchedule = Mock.Create<ISchedule>(Behavior.Loose);
            Mock.Arrange(() => newSchedule.Guid).Returns(scheduleGuid);
            Mock.Arrange(() => newSchedule.IsActive).Returns(true);
            Mock.Arrange(() => newSchedule.Equals(Arg.IsAny<ISchedule>())).Returns(false);

            var synchronizer = Mock.Create<IProcessSynchronizer>(Behavior.Loose);

            Mock.Arrange(() => synchronizer.Guid).Returns(syncGuid);
            Mock.Arrange(() => synchronizer.ProcessName).Returns(processName);
            Mock.Arrange(() => synchronizer.Schedules).Returns(new[] { newSchedule });

            var jobDetail = new JobDetail(jobName, QuartzScheduler.QuartzGroupName, typeof(SyncProcessWorker), false, true, false);

            var oldSchedule = Mock.Create<ISchedule>(Behavior.Loose);
            Mock.Arrange(() => oldSchedule.Guid).Returns(scheduleGuid);
            Mock.Arrange(() => oldSchedule.Equals(Arg.IsAny<ISchedule>())).Returns(false);

            var trigger = new ESyncTrigger
            {
                Name = triggerName,
                Schedule = oldSchedule
            };

            var jobTriggers = new List<Trigger> { trigger };

            var scheduler = Mock.Create<IScheduler>(Behavior.Loose);

            // Job is already scheduled.
            Mock.Arrange(() => scheduler.GetJobDetail(jobName, QuartzScheduler.QuartzGroupName)).Returns(jobDetail);

            Mock.Arrange(() => scheduler.GetTriggersOfJob(jobName, QuartzScheduler.QuartzGroupName)).Returns(jobTriggers.ToArray);

            Mock.Arrange(() => scheduler.UnscheduleJob(Arg.IsAny<string>(), QuartzScheduler.QuartzGroupName))
                .DoInstead<string, string>((tn, gn) => jobTriggers.Remove(jobTriggers.FirstOrDefault(t => t.Name == tn)));

            var logger = Mock.Create<ILogger>(Behavior.Loose);
            var triggerFactory = Mock.Create<ITriggerFactory>(Behavior.CallOriginal);
            Mock.Arrange(() => triggerFactory.CreateTrigger(Arg.IsAny<ISchedule>())).Returns<ISchedule>(
                s =>
                    {
                        var newTrigger = Mock.Create<ESyncTrigger>(Behavior.Loose);
                        Mock.Arrange(() => newTrigger.ComputeFirstFireTimeUtc(Arg.IsAny<ICalendar>())).Returns(DateTime.UtcNow);
                        newTrigger.Schedule = s;

                        return newTrigger;
                    });

            var target = new DummyQuartzScheduler { Logger = logger, TriggerFactory = triggerFactory };

            target.SetScheduler(scheduler);

            // Act.
            target.UpdateJob(synchronizer);

            // Assert.
            Mock.Assert(() => scheduler.UnscheduleJob(triggerName, QuartzScheduler.QuartzGroupName), Occurs.Once());
            Mock.Assert(() => scheduler.ScheduleJob(Arg.IsAny<Trigger>()), Occurs.Once());
            Mock.Assert(() => logger.Log(LogSeverity.Error, Arg.IsAny<string>(), Arg.IsAny<Exception>()), Occurs.Never());
        }
        public void WhenScheduleIsNotActive_UpdateJob_DeletesTrigger()
        {
            // Arrange.
            var syncGuid = Guid.Parse("DF1F8724-297C-45AE-8E70-B924FD375620");
            var scheduleGuid = Guid.Parse("{210B8E36-B81D-4E29-AFC0-19C7CA7FCCD1}");
            var jobName = syncGuid.ToString();
            var triggerName = scheduleGuid.ToString();
            const string processName = "TestProcess";

            var newSchedule = Mock.Create<ISchedule>(Behavior.Loose);
            Mock.Arrange(() => newSchedule.Guid).Returns(scheduleGuid);
            Mock.Arrange(() => newSchedule.IsActive).Returns(false);

            var synchronizer = Mock.Create<IProcessSynchronizer>(Behavior.Loose);

            Mock.Arrange(() => synchronizer.Guid).Returns(syncGuid);
            Mock.Arrange(() => synchronizer.ProcessName).Returns(processName);
            Mock.Arrange(() => synchronizer.Schedules).Returns(new[] { newSchedule });

            var jobDetail = new JobDetail(jobName, QuartzScheduler.QuartzGroupName, typeof(SyncProcessWorker), false, true, false);

            var oldSchedule = Mock.Create<ISchedule>(Behavior.Loose);
            Mock.Arrange(() => oldSchedule.Guid).Returns(scheduleGuid);

            // Make old schedule and new schedule be equal.
            Mock.Arrange(() => oldSchedule.Equals(newSchedule)).Returns(true);
            Mock.Arrange(() => newSchedule.Equals(oldSchedule)).Returns(true);

            var trigger = new ESyncTrigger { Name = triggerName, Schedule = oldSchedule };

            var scheduler = Mock.Create<IScheduler>(Behavior.Loose);

            // Job is already scheduled.
            Mock.Arrange(() => scheduler.GetJobDetail(jobName, QuartzScheduler.QuartzGroupName)).Returns(jobDetail);

            Mock.Arrange(() => scheduler.GetTriggersOfJob(jobName, QuartzScheduler.QuartzGroupName)).Returns(new Trigger[] { trigger });

            var logger = Mock.Create<ILogger>(Behavior.Loose);

            var target = new DummyQuartzScheduler { Logger = logger };
            target.SetScheduler(scheduler);

            // Act.
            target.UpdateJob(synchronizer);

            // Assert.
            Mock.Assert(() => scheduler.UnscheduleJob(triggerName, QuartzScheduler.QuartzGroupName), Occurs.Once());
            Mock.Assert(() => scheduler.ScheduleJob(Arg.IsAny<Trigger>()), Occurs.Never());
            Mock.Assert(() => logger.Log(LogSeverity.Error, Arg.IsAny<string>(), Arg.IsAny<Exception>()), Occurs.Never());
        }
        public void WhenScheduleIsNotFound_UpdateJob_DeletesTrigger()
        {
            // Arrange.
            var syncGuid = Guid.Parse("DF1F8724-297C-45AE-8E70-B924FD375620");
            var scheduleGuid = Guid.Parse("{210B8E36-B81D-4E29-AFC0-19C7CA7FCCD1}");
            var jobName = syncGuid.ToString();
            var triggerName = scheduleGuid.ToString();
            const string processName = "TestProcess";

            var synchronizerMock = Mock.Create<IProcessSynchronizer>(Behavior.Loose);

            Mock.Arrange(() => synchronizerMock.Guid).Returns(syncGuid);
            Mock.Arrange(() => synchronizerMock.ProcessName).Returns(processName);
            Mock.Arrange(() => synchronizerMock.Schedules).Returns(new ISchedule[] { });

            var jobDetail = new JobDetail(jobName, QuartzScheduler.QuartzGroupName, typeof(SyncProcessWorker), false, true, false);

            var oldSchedule = Mock.Create<ISchedule>(Behavior.Loose);
            Mock.Arrange(() => oldSchedule.Guid).Returns(scheduleGuid);
            var trigger = new ESyncTrigger
            {
                Name = triggerName,
                Schedule = oldSchedule
            };

            var schedulerMock = Mock.Create<IScheduler>(Behavior.Loose);

            // Job is already scheduled.
            Mock.Arrange(() => schedulerMock.GetJobDetail(jobName, QuartzScheduler.QuartzGroupName)).Returns(jobDetail);

            Mock.Arrange(() => schedulerMock.GetTriggersOfJob(jobName, QuartzScheduler.QuartzGroupName)).Returns(new Trigger[] { trigger });

            var loggerMock = Mock.Create<ILogger>(Behavior.CallOriginal);

            var target = new DummyQuartzScheduler { Logger = loggerMock };
            target.SetScheduler(schedulerMock);

            // Act.
            target.UpdateJob(synchronizerMock);

            // Assert.
            Mock.Assert(() => schedulerMock.UnscheduleJob(triggerName, QuartzScheduler.QuartzGroupName), Occurs.Once());
            Mock.Assert(() => schedulerMock.ScheduleJob(Arg.IsAny<Trigger>()), Occurs.Never());
        }
Beispiel #7
0
        public void GetFireTimeAfter_ReturnsNextScheduleTime()
        {
            // Arrange.
            var afterTime = new DateTime(2013, 1, 16, 0, 0, 0, DateTimeKind.Local);
            var nextTime = new DateTime(2013, 1, 16, 0, 0, 1, DateTimeKind.Local);

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(afterTime)).Returns(nextTime);

            var target = new ESyncTrigger(schedule);

            // Act.
            var result = target.GetFireTimeAfter(afterTime.ToUniversalTime());

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreEqual(nextTime, result.Value.ToLocalTime());
        }
Beispiel #8
0
        public void WhenNextFireTimeIsNotNull_GetMayFireAgain_ReturnsTrue()
        {
            // Arrange.
            var startTime = new DateTime(2013, 1, 16, 1, 0, 0, DateTimeKind.Local);
            var nextTime = new DateTime(2013, 1, 16, 2, 0, 0, DateTimeKind.Local);

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);

            Mock.Arrange(() => schedule.GetNextTimeAfter(startTime)).Returns(nextTime);

            var target = new ESyncTrigger(schedule)
                {
                    StartTimeUtc = startTime.ToUniversalTime()
                };

            // Act.
            target.ComputeFirstFireTimeUtc(null);

            // Assert.
            Assert.IsTrue(target.GetMayFireAgain());
        }
Beispiel #9
0
        public void WhenNextFireTimeIsNull_GetMayFireAgain_ReturnsFalse()
        {
            var target = new ESyncTrigger();

            Assert.IsFalse(target.GetMayFireAgain());
        }
Beispiel #10
0
        public void TriggerDoesNotHaveMillisecondPrecision()
        {
            var target = new ESyncTrigger();

            Assert.IsFalse(target.HasMillisecondPrecision);
        }
Beispiel #11
0
        public void UpdateAfterMisfireWithFireOnceNowInstruction_Updates_NextFireTime()
        {
            // Arrange.
            var currentTime = new DateTime(2013, 1, 16, 0, 0, 0, DateTimeKind.Local);
            var nextTime = new DateTime(2013, 1, 16, 1, 0, 0, DateTimeKind.Local);

            var timeProvider = Mock.Create<ITimeProvider>(Behavior.CallOriginal);
            Mock.Arrange(() => timeProvider.UtcNow).Returns(currentTime.ToUniversalTime());

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(currentTime)).Returns(nextTime);

            var target = new ESyncTrigger(schedule)
            {
                TimeProvider = timeProvider,
                MisfireInstruction = ESyncTrigger.FireOnceNow
            };

            // Act.
            target.UpdateAfterMisfire(null);
            var result = target.GetNextFireTimeUtc();

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreEqual(currentTime, result.Value.ToLocalTime());
        }
Beispiel #12
0
        public void GetFireTimeAfter_Returns_NextFireTimeIncludedInCalendar()
        {
            // Arrange.
            var startTime = new DateTime(2013, 1, 16, 0, 0, 0, DateTimeKind.Local);
            var nextTime1 = new DateTime(2013, 1, 16, 1, 0, 0, DateTimeKind.Local);
            var nextTime2 = new DateTime(2013, 1, 16, 2, 0, 0, DateTimeKind.Local);
            var nextTime3 = new DateTime(2013, 1, 16, 3, 0, 0, DateTimeKind.Local);
            var nextTime4 = new DateTime(2013, 1, 16, 4, 0, 0, DateTimeKind.Local);

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(startTime)).Returns(nextTime1);
            Mock.Arrange(() => schedule.GetNextTimeAfter(nextTime1)).Returns(nextTime2);
            Mock.Arrange(() => schedule.GetNextTimeAfter(nextTime2)).Returns(nextTime3);
            Mock.Arrange(() => schedule.GetNextTimeAfter(nextTime3)).Returns(nextTime4);

            var calendar = Mock.Create<ICalendar>(Behavior.CallOriginal);
            Mock.Arrange(() => calendar.IsTimeIncluded(nextTime1.ToUniversalTime())).Returns(false);
            Mock.Arrange(() => calendar.IsTimeIncluded(nextTime2.ToUniversalTime())).Returns(false);
            Mock.Arrange(() => calendar.IsTimeIncluded(nextTime3.ToUniversalTime())).Returns(true);
            Mock.Arrange(() => calendar.IsTimeIncluded(nextTime4.ToUniversalTime())).Returns(true);

            var target = new ESyncTrigger(schedule) { StartTimeUtc = startTime.ToUniversalTime() };

            // Act.
            var result = target.GetFireTimeAfter(startTime.ToUniversalTime(), calendar);

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreEqual(nextTime3, result.Value.ToLocalTime());
        }
Beispiel #13
0
        public void Triggered_Updates_NextAndPreviousFireTime()
        {
            // Arrange.
            var startTime = new DateTime(2013, 1, 16, 0, 0, 0, DateTimeKind.Local);
            var nextTime1 = new DateTime(2013, 1, 16, 1, 0, 0, DateTimeKind.Local);
            var nextTime2 = new DateTime(2013, 1, 16, 2, 0, 0, DateTimeKind.Local);

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(startTime)).Returns(nextTime1);
            Mock.Arrange(() => schedule.GetNextTimeAfter(nextTime1)).Returns(nextTime2);

            var target = new ESyncTrigger(schedule) { StartTimeUtc = startTime.ToUniversalTime() };

            // Act.
            target.ComputeFirstFireTimeUtc(null);
            target.Triggered(null);

            var previousTimeUtc = target.GetPreviousFireTimeUtc();
            var nextTimeUtc = target.GetNextFireTimeUtc();

            // Assert.
            Assert.IsNotNull(previousTimeUtc);
            Assert.IsNotNull(nextTimeUtc);
            Assert.AreEqual(nextTime1, previousTimeUtc.Value.ToLocalTime());
            Assert.AreEqual(nextTime2, nextTimeUtc.Value.ToLocalTime());
        }
Beispiel #14
0
        public void ComputeFirstFireTimeUtc_Updates_NextFireTime()
        {
            // Arrange.
            var startTime = new DateTime(2013, 1, 16, 1, 0, 0, DateTimeKind.Local);
            var nextTime = new DateTime(2013, 1, 16, 2, 0, 0, DateTimeKind.Local);

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(startTime)).Returns(nextTime);

            var target = new ESyncTrigger(schedule)
            {
                StartTimeUtc = startTime.ToUniversalTime()
            };

            // Act.
            var result = target.ComputeFirstFireTimeUtc(null);
            var nextTimeUtc = target.GetNextFireTimeUtc();

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreEqual(nextTime, result.Value.ToLocalTime());
            Assert.IsNotNull(nextTimeUtc);
            Assert.AreEqual(nextTime, nextTimeUtc.Value.ToLocalTime());
        }
Beispiel #15
0
        public void WhenAfterTimeIsNull_GetFireTimeAfter_ReturnsNextTimeAfterCurrentDate()
        {
            // Arrange.
            var currentTime = new DateTime(2013, 1, 16, 0, 0, 0, DateTimeKind.Local);
            var nextTime = new DateTime(2013, 1, 16, 0, 0, 1, DateTimeKind.Local);

            var schedule = Mock.Create<ISchedule>(Behavior.CallOriginal);
            Mock.Arrange(() => schedule.GetNextTimeAfter(currentTime)).Returns(nextTime);

            var timeProvider = Mock.Create<ITimeProvider>(Behavior.CallOriginal);
            Mock.Arrange(() => timeProvider.UtcNow).Returns(currentTime.ToUniversalTime());

            var target = new ESyncTrigger(schedule) { TimeProvider = timeProvider };

            // Act.
            var result = target.GetFireTimeAfter(null);

            // Assert.
            Assert.IsNotNull(result);
            Assert.AreEqual(nextTime, result.Value.ToLocalTime());
        }