public void CanRunWorkItem() { var queue = new InMemoryQueue<WorkItemData>(); var messageBus = new InMemoryMessageBus(); var handlerRegistry = new WorkItemHandlers(); var job = new WorkItemJob(queue, messageBus, handlerRegistry); handlerRegistry.Register<MyWorkItem>(async ctx => { var jobData = ctx.GetData<MyWorkItem>(); Assert.Equal("Test", jobData.SomeData); for (int i = 0; i < 10; i++) { await Task.Delay(100); ctx.ReportProgress(10 * i); } }); var jobId = queue.Enqueue(new MyWorkItem { SomeData = "Test" }, true); int statusCount = 0; messageBus.Subscribe<WorkItemStatus>(status => { Assert.Equal(jobId, status.WorkItemId); statusCount++; }); job.RunUntilEmpty(); Assert.Equal(12, statusCount); }
public void CanRunBadWorkItem() { var queue = new InMemoryQueue<WorkItemData>(); var messageBus = new InMemoryMessageBus(); var handlerRegistry = new WorkItemHandlers(); var job = new WorkItemJob(queue, messageBus, handlerRegistry); handlerRegistry.Register<MyWorkItem>(ctx => { var jobData = ctx.GetData<MyWorkItem>(); Assert.Equal("Test", jobData.SomeData); throw new ApplicationException(); }); var jobId = queue.Enqueue(new MyWorkItem { SomeData = "Test" }, true); int statusCount = 0; messageBus.Subscribe<WorkItemStatus>(status => { Assert.Equal(jobId, status.WorkItemId); statusCount++; }); job.RunUntilEmpty(); Thread.Sleep(1); Assert.Equal(1, statusCount); handlerRegistry.Register<MyWorkItem>(ctx => { var jobData = ctx.GetData<MyWorkItem>(); Assert.Equal("Test", jobData.SomeData); return TaskHelper.Completed(); }); jobId = queue.Enqueue(new MyWorkItem { SomeData = "Test" }, true); job.RunUntilEmpty(); Assert.Equal(2, statusCount); }
public void CanRunWorkItemWithClassHandler() { ServiceProvider.SetServiceProvider(typeof(MyBootstrappedServiceProvider)); var queue = new InMemoryQueue<WorkItemData>(); var messageBus = new InMemoryMessageBus(); var handlerRegistry = new WorkItemHandlers(); var job = new WorkItemJob(queue, messageBus, handlerRegistry); handlerRegistry.Register<MyWorkItem, MyWorkItemHandler>(); var jobId = queue.Enqueue(new MyWorkItem { SomeData = "Test" }, true); int statusCount = 0; messageBus.Subscribe<WorkItemStatus>(status => { Assert.Equal(jobId, status.WorkItemId); statusCount++; }); job.RunUntilEmpty(); Assert.Equal(12, statusCount); }