public virtual async Task CanRunWorkItemWithMetrics() { var eventRaised = new ManualResetEvent(false); var metricsClient = new InMemoryMetricsClient(); var behavior = new MetricsQueueBehavior <WorkItemData>(metricsClient, "metric"); var queue = new InMemoryQueue <WorkItemData>(behaviors: new[] { behavior }); queue.Completed.AddHandler((sender, e) => { eventRaised.Set(); return(TaskHelper.Completed()); }); var work = new SimpleWorkItem { Id = 1, Data = "Testing" }; await queue.EnqueueAsync(work); var item = await queue.DequeueAsync(); await item.CompleteAsync(); metricsClient.DisplayStats(_writer); Assert.True(eventRaised.WaitOne(TimeSpan.FromMinutes(1))); Assert.Equal(6, metricsClient.Counters.Count); Assert.Equal(4, metricsClient.Timings.Count); Assert.Equal(1, metricsClient.Counters["metric.workitemdata.simple.enqueued"]?.RecentValue); Assert.Equal(1, metricsClient.Counters["metric.workitemdata.simple.dequeued"]?.RecentValue); Assert.Equal(1, metricsClient.Counters["metric.workitemdata.simple.completed"]?.RecentValue); Assert.True(0 < metricsClient.Timings["metric.workitemdata.simple.queuetime"]?.Count); Assert.True(0 < metricsClient.Timings["metric.workitemdata.simple.processtime"]?.Count); }
public void CanIncrementCounter() { var metrics = new InMemoryMetricsClient(); metrics.Counter("c1"); Assert.Equal(1, metrics.GetCount("c1")); metrics.Counter("c1", 5); Assert.Equal(6, metrics.GetCount("c1")); var counter = metrics.Counters["c1"]; Assert.True(counter.Rate > 400); metrics.Gauge("g1", 2.534); Assert.Equal(2.534, metrics.GetGaugeValue("g1")); metrics.Timer("t1", 50788); var stats = metrics.GetMetricStats(); Assert.Equal(1, stats.Timings.Count); metrics.DisplayStats(_writer); }
public async Task CanHandleMultipleWorkItemInstances() { const int workItemCount = 1000; var metrics = new InMemoryMetricsClient(); var queue = new InMemoryQueue <WorkItemData>(retries: 0, retryDelay: TimeSpan.Zero); queue.AttachBehavior(new MetricsQueueBehavior <WorkItemData>(metrics)); var messageBus = new InMemoryMessageBus(); var handlerRegistry = new WorkItemHandlers(); var j1 = new WorkItemJob(queue, messageBus, handlerRegistry); var j2 = new WorkItemJob(queue, messageBus, handlerRegistry); var j3 = new WorkItemJob(queue, messageBus, handlerRegistry); int errors = 0; var jobIds = new ConcurrentDictionary <string, int>(); handlerRegistry.Register <MyWorkItem>(async ctx => { var jobData = ctx.GetData <MyWorkItem>(); Assert.Equal("Test", jobData.SomeData); var jobWorkTotal = jobIds.AddOrUpdate(ctx.JobId, 1, (key, value) => value + 1); Logger.Trace().Message($"Job {ctx.JobId} processing work item #: {jobWorkTotal}").Write(); for (int i = 0; i < 10; i++) { await ctx.ReportProgressAsync(10 * i); } if (RandomData.GetBool(1)) { Interlocked.Increment(ref errors); throw new ApplicationException("Boom!"); } }); for (int i = 0; i < workItemCount; i++) { await queue.EnqueueAsync(new MyWorkItem { SomeData = "Test", Index = i }, true); } var completedItems = new List <string>(); object completedItemsLock = new object(); messageBus.Subscribe <WorkItemStatus>(status => { Logger.Trace().Message($"Progress: {status.Progress}").Write(); if (status.Progress < 100) { return; } lock (completedItemsLock) completedItems.Add(status.WorkItemId); }); var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10)); var tasks = new List <Task> { Task.Run(async() => { await j1.RunUntilEmptyAsync(cancellationTokenSource.Token); cancellationTokenSource.Cancel(); }, cancellationTokenSource.Token), Task.Run(async() => { await j2.RunUntilEmptyAsync(cancellationTokenSource.Token); cancellationTokenSource.Cancel(); }, cancellationTokenSource.Token), Task.Run(async() => { await j3.RunUntilEmptyAsync(cancellationTokenSource.Token); cancellationTokenSource.Cancel(); }, cancellationTokenSource.Token) }; try { await Task.WhenAll(tasks); await Task.Delay(100); } catch (TaskCanceledException) {} Logger.Info().Message($"Completed: {completedItems.Count} Errors: {errors}").Write(); metrics.DisplayStats(_writer); Assert.Equal(workItemCount, completedItems.Count + errors); Assert.Equal(3, jobIds.Count); Assert.Equal(workItemCount, jobIds.Sum(kvp => kvp.Value)); }