public void WorkerExecuteRetry() { IJob job = new TestJob() { ThrowException = true, Retries = 1 }; SignalsRecord signals = new SignalsRecord() { QueueNames = "*", WorkerSignal = WorkerSignal.None, WorkingSignal = WorkingSignal.None }; QueueRecord queued = new QueueRecord() { Id = 12, ApplicationName = BlueCollarSection.Section.ApplicationName, Data = JsonConvert.SerializeObject(job), JobName = job.Name, JobType = JobSerializer.GetTypeName(job.GetType()), QueuedOn = DateTime.UtcNow, QueueName = "*", TryNumber = 1 }; WorkingRecord working = Worker.CreateWorking(queued, 1, null, DateTime.UtcNow); working.Id = 13; var transaction = new Mock <IDbTransaction>(); var repository = new Mock <IRepository>(); repository.Setup(r => r.BeginTransaction()).Returns(transaction.Object); repository.Setup(r => r.BeginTransaction(It.IsAny <IsolationLevel>())).Returns(transaction.Object); repository.Setup(r => r.CreateWorking(It.IsAny <WorkingRecord>(), It.IsAny <IDbTransaction>())).Returns(working); repository.Setup(r => r.GetQueued(It.IsAny <string>(), It.IsAny <QueueNameFilters>(), It.IsAny <DateTime>(), It.IsAny <IDbTransaction>())).Returns(queued); repository.Setup(r => r.GetWorkingSignals(It.IsAny <long>(), It.IsAny <long?>(), It.IsAny <IDbTransaction>())).Returns(signals); var factory = new Mock <IRepositoryFactory>(); factory.Setup(f => f.Create()).Returns(repository.Object); var logger = new Mock <ILogger>(); using (Worker worker = new Worker(BlueCollarSection.Section.ApplicationName, 1, "Test Worker", null, 1, false, factory.Object, logger.Object)) { worker.Start(); Thread.Sleep(1500); Assert.AreEqual(WorkerStatus.Working, worker.Status); } repository.Verify(r => r.CreateHistory(It.Is <HistoryRecord>(h => h.Status == HistoryStatus.Failed), It.IsAny <IDbTransaction>())); repository.Verify(r => r.CreateQueued(It.Is <QueueRecord>(q => q.TryNumber == 2), It.IsAny <IDbTransaction>())); }
public void WorkerDequeue() { IJob job = new TestJob() { Id = Guid.NewGuid() }; SignalsRecord signals = new SignalsRecord() { QueueNames = "*", WorkerSignal = WorkerSignal.None, WorkingSignal = WorkingSignal.None }; QueueRecord queued = new QueueRecord() { Id = 12, ApplicationName = BlueCollarSection.Section.ApplicationName, Data = JsonConvert.SerializeObject(job), JobName = job.Name, JobType = JobSerializer.GetTypeName(job.GetType()), QueuedOn = DateTime.UtcNow, QueueName = "*", TryNumber = 1 }; var transaction = new Mock <IDbTransaction>(); var repository = new Mock <IRepository>(); repository.Setup(r => r.BeginTransaction()).Returns(transaction.Object); repository.Setup(r => r.BeginTransaction(It.IsAny <IsolationLevel>())).Returns(transaction.Object); repository.Setup(r => r.GetQueued(It.IsAny <string>(), It.IsAny <QueueNameFilters>(), It.IsAny <DateTime>(), It.IsAny <IDbTransaction>())).Returns(queued); repository.Setup(r => r.GetWorkingSignals(It.IsAny <long>(), It.IsAny <long?>(), It.IsAny <IDbTransaction>())).Returns(signals); var factory = new Mock <IRepositoryFactory>(); factory.Setup(f => f.Create()).Returns(repository.Object); var logger = new Mock <ILogger>(); using (Worker worker = new Worker(BlueCollarSection.Section.ApplicationName, 1, "Test Worker", null, 1, false, factory.Object, logger.Object)) { worker.Start(); Thread.Sleep(1500); } repository.Verify(r => r.GetQueued(BlueCollarSection.Section.ApplicationName, It.IsAny <QueueNameFilters>(), It.IsAny <DateTime>(), It.IsAny <IDbTransaction>())); repository.Verify(r => r.DeleteQueued(12, It.IsAny <IDbTransaction>())); repository.Verify(r => r.CreateWorking(It.Is <WorkingRecord>(w => w.ApplicationName == BlueCollarSection.Section.ApplicationName && w.WorkerId == 1), It.IsAny <IDbTransaction>())); }
public void SchedulerRefreshSchedules() { ScheduleRecord schedule = new ScheduleRecord() { ApplicationName = BlueCollarSection.Section.ApplicationName, Enabled = true, Id = 1, Name = "Test", QueueName = "*", RepeatType = ScheduleRepeatType.Days, RepeatValue = 1, StartOn = DateTime.UtcNow.FloorWithSeconds() }; ScheduledJobRecord scheduledJob = new ScheduledJobRecord() { Data = @"{""SleepDuration"":1000}", Id = 1, JobType = JobSerializer.GetTypeName(typeof(TestJob)), Schedule = schedule, ScheduleId = 1 }; schedule.ScheduledJobs.Add(scheduledJob); var transaction = new Mock <IDbTransaction>(); var repository = new Mock <IRepository>(); repository.Setup(r => r.BeginTransaction()).Returns(transaction.Object); repository.Setup(r => r.GetSchedules(BlueCollarSection.Section.ApplicationName, It.IsAny <IDbTransaction>())).Returns(new ScheduleRecord[] { schedule }); var factory = new Mock <IRepositoryFactory>(); factory.Setup(f => f.Create()).Returns(repository.Object); var logger = new Mock <ILogger>(); Scheduler scheduler = new Scheduler(1, BlueCollarSection.Section.ApplicationName, QueueNameFilters.Any(), 1, factory.Object, logger.Object); Assert.AreEqual(0, scheduler.Schedules.Count()); scheduler.RefreshSchedules(); Assert.AreEqual(1, scheduler.Schedules.Count()); }