public void StartBatchJob_WithInfiniteItems_RunsMoreThanArbitraryTimes(int arbitraryRuns) { var batchSize = 1; BatchJobOptions options = new BatchJobOptions() { MaxItems = -1, BatchSize = batchSize, DelayInSecs = 0 }; BatchJob job = new BatchJob(options, Mock.Of <ILogger>()); IOrderHandler handler = Mock.Of <IOrderHandler>(); Mock.Get(handler).Setup(h => h.HandleBatch(batchSize)).Verifiable(); // fire and forget since it is infinite thread job.Start(handler.HandleBatch).Wait(1000); // give time for iterations job.Stop(); Mock.Get(handler).Verify((m) => m.HandleBatch(batchSize), Times.AtLeast(arbitraryRuns)); }
public void StartBatchJob_WithInvalidOptions_ThrowsArgumentException( int maxItems, int batchSize, int delayInSecs) { BatchJobOptions options = new BatchJobOptions() { MaxItems = maxItems, BatchSize = batchSize, DelayInSecs = delayInSecs }; BatchManager manager = CreateBatchManager(); OrderHandler handler = CreateOrderHandler(); Assert.Throws <ArgumentException>(() => manager.StartBatchJob(handler.HandleBatch, options)); }
public void StopBatchJob_WithNoExistingJob_ThrowsInvalidOperation() { BatchJobOptions options = new BatchJobOptions() { MaxItems = 100, BatchSize = 2, DelayInSecs = 1 }; BatchManager manager = CreateBatchManager(); OrderHandler handler = CreateOrderHandler(); Assert.False(manager.HasActiveBatchJob()); Assert.Throws <InvalidOperationException>(() => manager.StopBatchJob()); }
public void StartBatchJob_WithNoExistingJob_StartsBatchJob() { BatchJobOptions options = new BatchJobOptions() { MaxItems = 100, BatchSize = 2, DelayInSecs = 1 }; BatchManager manager = CreateBatchManager(); OrderHandler handler = CreateOrderHandler(); Assert.False(manager.HasActiveBatchJob()); manager.StartBatchJob(handler.HandleBatch, options); Assert.True(manager.HasActiveBatchJob()); }
public IActionResult PostStartProducer(BatchJobOptions options) { IActionResult result; try { batchManager.StartBatchJob(orderHandler.HandleBatch, options); } catch (Exception ex) { result = new BadRequestObjectResult(ex.Message); return(result); } result = new OkResult(); return(result); }
public async Task StartBatchJob_WithMaxItemsAndBatchSize_RunsAppropriateNumberOfTimes(int maxItems, int batchSize) { var expectedIterations = (maxItems / batchSize) + (maxItems % batchSize == 0 ? 0 : 1); BatchJobOptions options = new BatchJobOptions() { MaxItems = maxItems, BatchSize = batchSize, DelayInSecs = 0 }; BatchJob job = new BatchJob(options, Mock.Of <ILogger>()); IOrderHandler handler = Mock.Of <IOrderHandler>(); Mock.Get(handler).Setup(h => h.HandleBatch(It.IsAny <int>())).Verifiable(); await job.Start(handler.HandleBatch); Mock.Get(handler).Verify((m) => m.HandleBatch(It.IsAny <int>()), Times.Exactly(expectedIterations)); }