public async Task CanRunWorkItem() { using (var queue = new InMemoryQueue <WorkItemData>(new InMemoryQueueOptions <WorkItemData> { LoggerFactory = Log })) { using (var messageBus = new InMemoryMessageBus(new InMemoryMessageBusOptions { LoggerFactory = Log })) { var handlerRegistry = new WorkItemHandlers(); var job = new WorkItemJob(queue, messageBus, handlerRegistry, Log); handlerRegistry.Register <MyWorkItem>(async ctx => { var jobData = ctx.GetData <MyWorkItem>(); Assert.Equal("Test", jobData.SomeData); for (int i = 0; i < 10; i++) { await SystemClock.SleepAsync(100); await ctx.ReportProgressAsync(10 * i); } }); string jobId = await queue.EnqueueAsync(new MyWorkItem { SomeData = "Test" }, true); int statusCount = 0; await messageBus.SubscribeAsync <WorkItemStatus>(status => { _logger.LogInformation("Progress: {Progress}", status.Progress); Assert.Equal(jobId, status.WorkItemId); Interlocked.Increment(ref statusCount); }); await job.RunAsync(); Assert.Equal(12, statusCount); } } }
public async Task CanRunWorkItem() { using (var queue = new InMemoryQueue <WorkItemData>(o => o.LoggerFactory(Log))) { using (var messageBus = new InMemoryMessageBus(o => o.LoggerFactory(Log))) { var handlerRegistry = new WorkItemHandlers(); var job = new WorkItemJob(queue, messageBus, handlerRegistry, Log); handlerRegistry.Register <MyWorkItem>(async ctx => { var jobData = ctx.GetData <MyWorkItem>(); Assert.Equal("Test", jobData.SomeData); for (int i = 0; i < 10; i++) { await SystemClock.SleepAsync(100); await ctx.ReportProgressAsync(10 * i); } }); string jobId = await queue.EnqueueAsync(new MyWorkItem { SomeData = "Test" }, true); var countdown = new AsyncCountdownEvent(12); await messageBus.SubscribeAsync <WorkItemStatus>(status => { _logger.LogInformation("Progress: {Progress}", status.Progress); Assert.Equal(jobId, status.WorkItemId); countdown.Signal(); }); await job.RunAsync(); await countdown.WaitAsync(TimeSpan.FromSeconds(2)); Assert.Equal(0, countdown.CurrentCount); } } }