Exemple #1
0
        public void WorkItemsWillGetMovedToDeadletter()
        {
            var queue = new InMemoryQueue <SimpleWorkItem>(retries: 1, workItemTimeoutMilliseconds: 10, retryDelayMilliseconds: 10);

            queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello"
            }).Wait();
            var workItem = queue.DequeueAsync(0).Result;

            Assert.Equal("Hello", workItem.Value.Data);
            Assert.Equal(1, queue.Dequeued);

            Assert.Equal(0, queue.Count);
            // wait for the task to be auto abandoned
            Task.Delay(100).Wait();
            Assert.Equal(1, queue.Count);
            Assert.Equal(1, queue.Abandoned);

            // work item should be retried 1 time.
            workItem = queue.DequeueAsync(0).Result;
            Assert.Equal("Hello", workItem.Value.Data);
            Assert.Equal(2, queue.Dequeued);

            Task.Delay(100).Wait();
            // work item should be moved to deadletter queue after retries.
            Assert.Equal(1, queue.DeadletterCount);
            Assert.Equal(2, queue.Abandoned);
        }
        public void WorkItemsWillGetMovedToDeadletter() {
            var queue = new InMemoryQueue<SimpleWorkItem>(retries: 1, workItemTimeoutMilliseconds: 10);
            queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello"
            });
            var workItem = queue.DequeueAsync(0).Result;
            Assert.Equal("Hello", workItem.Value.Data);
            Assert.Equal(1, queue.Dequeued);

            Assert.Equal(0, queue.Count);
            // wait for the task to be auto abandoned
            Task.Delay(100).Wait();
            Assert.Equal(1, queue.Count);
            Assert.Equal(1, queue.Abandoned);

            // work item should be retried 1 time.
            workItem = queue.DequeueAsync(0).Result;
            Assert.Equal("Hello", workItem.Value.Data);
            Assert.Equal(2, queue.Dequeued);

            Task.Delay(100).Wait();
            // work item should be moved to deadletter queue after retries.
            Assert.Equal(1, queue.DeadletterCount);
            Assert.Equal(2, queue.Abandoned);
        }
        public virtual async Task CanGetBufferedQueueMetrics() {
            using (var metrics = GetMetricsClient(true) as IBufferedMetricsClient) {
                var stats = metrics as IMetricsClientStats;
                if (stats == null)
                    return;

                using (var queue = new InMemoryQueue<SimpleWorkItem>(behaviors: new[] { new MetricsQueueBehavior<SimpleWorkItem>(metrics, loggerFactory: Log) }, loggerFactory: Log)) {
                    await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "1" });
                    await SystemClock.SleepAsync(50);
                    var entry = await queue.DequeueAsync(TimeSpan.Zero);
                    await SystemClock.SleepAsync(30);
                    await entry.CompleteAsync();
                    await SystemClock.SleepAsync(500);  // give queue metrics time

                    await metrics.FlushAsync();

                    var queueStats = await stats.GetQueueStatsAsync("simpleworkitem");
                    Assert.Equal(1, queueStats.Count.Max);
                    Assert.Equal(0, queueStats.Count.Last);
                    Assert.Equal(1, queueStats.Enqueued.Count);
                    Assert.Equal(1, queueStats.Dequeued.Count);
                    Assert.Equal(1, queueStats.Completed.Count);
                    Assert.InRange(queueStats.QueueTime.AverageDuration, 50, 200);
                    Assert.InRange(queueStats.ProcessTime.AverageDuration, 30, 200);
                }
            }
        }
        public async Task BotService_AGivenCommand_EnqueueMessage()
        {
            var stock            = @"Symbol,Date,Time,Open,High,Low,Close,Volume
AAPL.US,2019-04-26,22:00:20,204.9,205,202.12,204.3,18649102";
            var fakeStockService = new FakeStockService(stock);
            var messageQueue     = new InMemoryQueue <Message>();
            var botService       = new BotService(fakeStockService, messageQueue);

            await botService.QueryAndSend(stockCode : "aapl.us");

            var lastMessage = await messageQueue.DequeueAsync();

            Assert.IsNotNull(lastMessage);
        }
        public void WorkItemsWillTimeout() {
            var queue = new InMemoryQueue<SimpleWorkItem>(workItemTimeoutMilliseconds: 10);
            queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello"
            });
            var workItem = queue.DequeueAsync(0).Result;
            Assert.Equal("Hello", workItem.Value.Data);

            Assert.Equal(0, queue.Count);
            // wait for the task to be auto abandoned
            Task.Delay(100).Wait();
            Assert.Equal(1, queue.Count);
            Assert.Equal(1, queue.Abandoned);
        }
        public void CanQueueAndDequeueWorkItem() {
            var queue = new InMemoryQueue<SimpleWorkItem>();
            queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello"
            });
            Assert.Equal(1, queue.Count);

            var workItem = queue.DequeueAsync(0).Result;
            Assert.NotNull(workItem);
            Assert.Equal("Hello", workItem.Value.Data);
            Assert.Equal(0, queue.Count);
            Assert.Equal(1, queue.Dequeued);

            workItem.CompleteAsync().Wait();
            Assert.Equal(1, queue.Completed);
        }
Exemple #7
0
        public void WorkItemsWillTimeout()
        {
            var queue = new InMemoryQueue <SimpleWorkItem>(workItemTimeoutMilliseconds: 10, retryDelayMilliseconds: 0);

            queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello"
            });
            var workItem = queue.DequeueAsync(0).Result;

            Assert.Equal("Hello", workItem.Value.Data);

            Assert.Equal(0, queue.Count);
            // wait for the task to be auto abandoned
            Task.Delay(100).Wait();
            Assert.Equal(1, queue.Count);
            Assert.Equal(1, queue.Abandoned);
        }
Exemple #8
0
        public void CanQueueAndDequeueWorkItem()
        {
            var queue = new InMemoryQueue <SimpleWorkItem>();

            queue.EnqueueAsync(new SimpleWorkItem {
                Data = "Hello"
            });
            Assert.Equal(1, queue.Count);

            var workItem = queue.DequeueAsync(0).Result;

            Assert.NotNull(workItem);
            Assert.Equal("Hello", workItem.Value.Data);
            Assert.Equal(0, queue.Count);
            Assert.Equal(1, queue.Dequeued);

            workItem.CompleteAsync().Wait();
            Assert.Equal(1, queue.Completed);
        }
        private async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation("Queued Hosted Service is starting.");

            while (!cancellationToken.IsCancellationRequested)
            {
                var workItem = await _taskQueue.DequeueAsync(cancellationToken);

                try
                {
                    await ProcessOnce(workItem, cancellationToken);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex,
                                     $"Error occurred executing {nameof(workItem)}.");
                }
            }

            _logger.LogInformation("Queued Hosted Service is stopping.");
        }
        public virtual async Task CanGetBufferedQueueMetricsAsync()
        {
            using (var metrics = GetMetricsClient(true) as IBufferedMetricsClient) {
                var stats = metrics as IMetricsClientStats;
                if (stats == null)
                {
                    return;
                }

                using (var behavior = new MetricsQueueBehavior <SimpleWorkItem>(metrics, reportCountsInterval: TimeSpan.FromMilliseconds(25), loggerFactory: Log)) {
                    using (var queue = new InMemoryQueue <SimpleWorkItem>(new InMemoryQueueOptions <SimpleWorkItem> {
                        Behaviors = new [] { behavior }, LoggerFactory = Log
                    })) {
                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "1" });

                        await SystemClock.SleepAsync(50);

                        var entry = await queue.DequeueAsync(TimeSpan.Zero);

                        await SystemClock.SleepAsync(15);

                        await entry.CompleteAsync();

                        await SystemClock.SleepAsync(100); // give queue metrics time

                        await metrics.FlushAsync();

                        var queueStats = await stats.GetQueueStatsAsync("simpleworkitem");

                        Assert.Equal(1, queueStats.Count.Max);
                        Assert.Equal(0, queueStats.Count.Last);
                        Assert.Equal(1, queueStats.Enqueued.Count);
                        Assert.Equal(1, queueStats.Dequeued.Count);
                        Assert.Equal(1, queueStats.Completed.Count);
                        Assert.InRange(queueStats.QueueTime.AverageDuration, 45, 250);
                        Assert.InRange(queueStats.ProcessTime.AverageDuration, 10, 250);
                    }
                }
            }
        }
        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 virtual async Task CanGetBufferedQueueMetrics()
        {
            using (var metrics = GetMetricsClient(true) as IBufferedMetricsClient) {
                var stats = metrics as IMetricsClientStats;
                if (stats == null)
                {
                    return;
                }

                using (var queue = new InMemoryQueue <SimpleWorkItem>(behaviors: new[] { new MetricsQueueBehavior <SimpleWorkItem>(metrics, loggerFactory: Log) }, loggerFactory: Log)) {
                    await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "1" });

                    await SystemClock.SleepAsync(50);

                    var entry = await queue.DequeueAsync(TimeSpan.Zero);

                    await SystemClock.SleepAsync(30);

                    await entry.CompleteAsync();

                    await SystemClock.SleepAsync(500);  // give queue metrics time

                    await metrics.FlushAsync();

                    var queueStats = await stats.GetQueueStatsAsync("simpleworkitem");

                    Assert.Equal(1, queueStats.Count.Max);
                    Assert.Equal(0, queueStats.Count.Last);
                    Assert.Equal(1, queueStats.Enqueued.Count);
                    Assert.Equal(1, queueStats.Dequeued.Count);
                    Assert.Equal(1, queueStats.Completed.Count);
                    Assert.InRange(queueStats.QueueTime.AverageDuration, 50, 200);
                    Assert.InRange(queueStats.ProcessTime.AverageDuration, 30, 200);
                }
            }
        }
        public virtual async Task CanRunWorkItemWithMetricsAsync()
        {
            int completedCount = 0;

            using (var metrics = new InMemoryMetricsClient(new InMemoryMetricsClientOptions {
                Buffered = false, LoggerFactory = Log
            })) {
                var behavior = new MetricsQueueBehavior <WorkItemData>(metrics, "metric", TimeSpan.FromMilliseconds(100), loggerFactory: Log);
                var options  = new InMemoryQueueOptions <WorkItemData> {
                    Behaviors = new[] { behavior }, LoggerFactory = Log
                };
                using (var queue = new InMemoryQueue <WorkItemData>(options)) {
                    Func <object, CompletedEventArgs <WorkItemData>, Task> handler = (sender, e) => {
                        completedCount++;
                        return(Task.CompletedTask);
                    };

                    using (queue.Completed.AddHandler(handler)) {
                        _logger.Trace("Before enqueue");
                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "Testing" });

                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 2, Data = "Testing" });

                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 3, Data = "Testing" });

                        await SystemClock.SleepAsync(100);

                        _logger.Trace("Before dequeue");
                        var item = await queue.DequeueAsync();

                        await item.CompleteAsync();

                        item = await queue.DequeueAsync();

                        await item.CompleteAsync();

                        item = await queue.DequeueAsync();

                        await item.AbandonAsync();

                        _logger.Trace("Before asserts");
                        Assert.Equal(2, completedCount);

                        await SystemClock.SleepAsync(100); // flush metrics queue behaviors

                        await metrics.FlushAsync();

                        Assert.InRange((await metrics.GetGaugeStatsAsync("metric.workitemdata.count")).Max, 1, 3);
                        Assert.InRange((await metrics.GetGaugeStatsAsync("metric.workitemdata.working")).Max, 0, 1);

                        Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.simple.enqueued"));
                        Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.enqueued"));

                        Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.simple.dequeued"));
                        Assert.Equal(3, await metrics.GetCounterCountAsync("metric.workitemdata.dequeued"));

                        Assert.Equal(2, await metrics.GetCounterCountAsync("metric.workitemdata.simple.completed"));
                        Assert.Equal(2, await metrics.GetCounterCountAsync("metric.workitemdata.completed"));

                        Assert.Equal(1, await metrics.GetCounterCountAsync("metric.workitemdata.simple.abandoned"));
                        Assert.Equal(1, await metrics.GetCounterCountAsync("metric.workitemdata.abandoned"));

                        var queueTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.simple.queuetime");

                        Assert.Equal(3, queueTiming.Count);
                        queueTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.queuetime");

                        Assert.Equal(3, queueTiming.Count);

                        var processTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.simple.processtime");

                        Assert.Equal(3, processTiming.Count);
                        processTiming = await metrics.GetTimerStatsAsync("metric.workitemdata.processtime");

                        Assert.Equal(3, processTiming.Count);

                        var queueStats = await metrics.GetQueueStatsAsync("metric.workitemdata");

                        Assert.Equal(3, queueStats.Enqueued.Count);
                        Assert.Equal(3, queueStats.Dequeued.Count);
                        Assert.Equal(2, queueStats.Completed.Count);
                        Assert.Equal(1, queueStats.Abandoned.Count);
                        Assert.InRange(queueStats.Count.Max, 1, 3);
                        Assert.InRange(queueStats.Working.Max, 0, 1);

                        var subQueueStats = await metrics.GetQueueStatsAsync("metric.workitemdata", "simple");

                        Assert.Equal(3, subQueueStats.Enqueued.Count);
                        Assert.Equal(3, subQueueStats.Dequeued.Count);
                        Assert.Equal(2, subQueueStats.Completed.Count);
                        Assert.Equal(1, subQueueStats.Abandoned.Count);
                    }
                }
            }
        }
Exemple #14
0
        public virtual async Task CanRunWorkItemWithMetrics()
        {
            int completedCount = 0;

            //Log.MinimumLevel = LogLevel.Trace;
            //Log.SetLogLevel<ScheduledTimer>(LogLevel.Information);
            //Log.SetLogLevel<InMemoryCacheClient>(LogLevel.Information);

            using (var metricsClient = new InMemoryMetricsClient(false, loggerFactory: Log)) {
                var behavior = new MetricsQueueBehavior <WorkItemData>(metricsClient, "metric", loggerFactory: Log, reportCountsInterval: TimeSpan.Zero);
                using (var queue = new InMemoryQueue <WorkItemData>(behaviors: new[] { behavior }, loggerFactory: Log)) {
                    Func <object, CompletedEventArgs <WorkItemData>, Task> handler = (sender, e) => {
                        completedCount++;
                        return(Task.CompletedTask);
                    };

                    using (queue.Completed.AddHandler(handler)) {
                        _logger.Trace("Before enqueue");
                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "Testing" });

                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 2, Data = "Testing" });

                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 3, Data = "Testing" });

                        await SystemClock.SleepAsync(100);

                        _logger.Trace("Before dequeue");
                        var item = await queue.DequeueAsync();

                        await item.CompleteAsync();

                        item = await queue.DequeueAsync();

                        await item.CompleteAsync();

                        item = await queue.DequeueAsync();

                        await item.AbandonAsync();

                        _logger.Trace("Before asserts");
                        Assert.Equal(2, completedCount);

                        await SystemClock.SleepAsync(100);

                        Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.count")).Max, 1, 3);
                        Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.working")).Max, 0, 1);

                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.enqueued"));
                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.enqueued"));

                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.dequeued"));
                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.dequeued"));

                        Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.completed"));
                        Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.completed"));

                        Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.abandoned"));
                        Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.abandoned"));

                        var queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.queuetime");

                        Assert.Equal(3, queueTiming.Count);
                        queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.queuetime");

                        Assert.Equal(3, queueTiming.Count);

                        var processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.processtime");

                        Assert.Equal(3, processTiming.Count);
                        processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.processtime");

                        Assert.Equal(3, processTiming.Count);

                        var queueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata");

                        Assert.Equal(3, queueStats.Enqueued.Count);
                        Assert.Equal(3, queueStats.Dequeued.Count);
                        Assert.Equal(2, queueStats.Completed.Count);
                        Assert.Equal(1, queueStats.Abandoned.Count);
                        Assert.InRange(queueStats.Count.Max, 1, 3);
                        Assert.InRange(queueStats.Working.Max, 0, 1);

                        var subQueueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata", "simple");

                        Assert.Equal(3, subQueueStats.Enqueued.Count);
                        Assert.Equal(3, subQueueStats.Dequeued.Count);
                        Assert.Equal(2, subQueueStats.Completed.Count);
                        Assert.Equal(1, subQueueStats.Abandoned.Count);
                    }
                }
            }
        }
Exemple #15
0
        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);
        }
Exemple #16
0
        public virtual async Task CanRunWorkItemWithMetrics()
        {
            int completedCount = 0;

            Log.MinimumLevel = LogLevel.Trace;

            var metricsClient = new InMemoryMetricsClient(false, loggerFactory: Log);
            var behavior      = new MetricsQueueBehavior <WorkItemData>(metricsClient, "metric", loggerFactory: Log, reportCountsInterval: TimeSpan.Zero);
            var queue         = new InMemoryQueue <WorkItemData>(behaviors: new[] { behavior }, loggerFactory: Log);

            queue.Completed.AddHandler((sender, e) => {
                completedCount++;
                return(TaskHelper.Completed);
            });

            _logger.Trace("Before enqueue");
            await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "Testing" });

            await queue.EnqueueAsync(new SimpleWorkItem { Id = 2, Data = "Testing" });

            await queue.EnqueueAsync(new SimpleWorkItem { Id = 3, Data = "Testing" });

            await Task.Delay(100);

            _logger.Trace("Before dequeue");
            var item = await queue.DequeueAsync();

            await item.CompleteAsync();

            item = await queue.DequeueAsync();

            await item.CompleteAsync();

            item = await queue.DequeueAsync();

            await item.AbandonAsync();

            _logger.Trace("Before asserts");
            Assert.Equal(2, completedCount);

            Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.count")).Max, 2, 3);
            Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.working")).Max, 0, 1);

            Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.enqueued"));
            Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.enqueued"));

            Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.dequeued"));
            Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.dequeued"));

            Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.completed"));
            Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.completed"));

            Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.abandoned"));
            Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.abandoned"));

            var queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.queuetime");

            Assert.Equal(3, queueTiming.Count);
            queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.queuetime");

            Assert.Equal(3, queueTiming.Count);

            var processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.processtime");

            Assert.Equal(3, processTiming.Count);
            processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.processtime");

            Assert.Equal(3, processTiming.Count);

            var queueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata");

            Assert.Equal(3, queueStats.Enqueued.Count);
            Assert.Equal(3, queueStats.Dequeued.Count);
            Assert.Equal(2, queueStats.Completed.Count);
            Assert.Equal(1, queueStats.Abandoned.Count);
            Assert.Equal(3, queueStats.Count.Max);
            Assert.InRange(queueStats.Working.Max, 0, 1);

            var subQueueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata", "simple");

            Assert.Equal(3, subQueueStats.Enqueued.Count);
            Assert.Equal(3, subQueueStats.Dequeued.Count);
            Assert.Equal(2, subQueueStats.Completed.Count);
            Assert.Equal(1, subQueueStats.Abandoned.Count);
        }
Exemple #17
0
        public virtual async Task CanRunWorkItemWithMetrics() {
            int completedCount = 0;
            //Log.MinimumLevel = LogLevel.Trace;
            //Log.SetLogLevel<ScheduledTimer>(LogLevel.Information);
            //Log.SetLogLevel<InMemoryCacheClient>(LogLevel.Information);

            using (var metricsClient = new InMemoryMetricsClient(false, loggerFactory: Log)) {
                var behavior = new MetricsQueueBehavior<WorkItemData>(metricsClient, "metric", loggerFactory: Log, reportCountsInterval: TimeSpan.Zero);
                using (var queue = new InMemoryQueue<WorkItemData>(behaviors: new[] { behavior }, loggerFactory: Log)) {
                    Func<object, CompletedEventArgs<WorkItemData>, Task> handler = (sender, e) => {
                        completedCount++;
                        return Task.CompletedTask;
                    };

                    using (queue.Completed.AddHandler(handler)) {
                        _logger.Trace("Before enqueue");
                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 1, Data = "Testing" });
                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 2, Data = "Testing" });
                        await queue.EnqueueAsync(new SimpleWorkItem { Id = 3, Data = "Testing" });

                        await SystemClock.SleepAsync(100);

                        _logger.Trace("Before dequeue");
                        var item = await queue.DequeueAsync();
                        await item.CompleteAsync();

                        item = await queue.DequeueAsync();
                        await item.CompleteAsync();

                        item = await queue.DequeueAsync();
                        await item.AbandonAsync();

                        _logger.Trace("Before asserts");
                        Assert.Equal(2, completedCount);

                        await SystemClock.SleepAsync(100);

                        Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.count")).Max, 1, 3);
                        Assert.InRange((await metricsClient.GetGaugeStatsAsync("metric.workitemdata.working")).Max, 0, 1);

                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.enqueued"));
                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.enqueued"));

                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.dequeued"));
                        Assert.Equal(3, await metricsClient.GetCounterCountAsync("metric.workitemdata.dequeued"));

                        Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.completed"));
                        Assert.Equal(2, await metricsClient.GetCounterCountAsync("metric.workitemdata.completed"));

                        Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.simple.abandoned"));
                        Assert.Equal(1, await metricsClient.GetCounterCountAsync("metric.workitemdata.abandoned"));

                        var queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.queuetime");
                        Assert.Equal(3, queueTiming.Count);
                        queueTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.queuetime");
                        Assert.Equal(3, queueTiming.Count);

                        var processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.simple.processtime");
                        Assert.Equal(3, processTiming.Count);
                        processTiming = await metricsClient.GetTimerStatsAsync("metric.workitemdata.processtime");
                        Assert.Equal(3, processTiming.Count);

                        var queueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata");
                        Assert.Equal(3, queueStats.Enqueued.Count);
                        Assert.Equal(3, queueStats.Dequeued.Count);
                        Assert.Equal(2, queueStats.Completed.Count);
                        Assert.Equal(1, queueStats.Abandoned.Count);
                        Assert.InRange(queueStats.Count.Max, 1, 3);
                        Assert.InRange(queueStats.Working.Max, 0, 1);

                        var subQueueStats = await metricsClient.GetQueueStatsAsync("metric.workitemdata", "simple");
                        Assert.Equal(3, subQueueStats.Enqueued.Count);
                        Assert.Equal(3, subQueueStats.Dequeued.Count);
                        Assert.Equal(2, subQueueStats.Completed.Count);
                        Assert.Equal(1, subQueueStats.Abandoned.Count);
                    }
                }
            }
        }