Ejemplo n.º 1
0
        public virtual async Task CanRunMultipleQueueJobs() {
            const int jobCount = 5;
            const int workItemCount = 100;

            Log.SetLogLevel<SampleQueueJob>(LogLevel.Information);
            Log.SetLogLevel<InMemoryMetricsClient>(LogLevel.None);

            using (var metrics = new InMemoryMetricsClient(true, loggerFactory: Log)) {
                var queues = new List<IQueue<SampleQueueWorkItem>>();
                try {
                    for (int i = 0; i < jobCount; i++) {
                        var q = GetSampleWorkItemQueue(retries: 1, retryDelay: TimeSpan.Zero);
                        await q.DeleteQueueAsync();
                        q.AttachBehavior(new MetricsQueueBehavior<SampleQueueWorkItem>(metrics, "test", Log));
                        queues.Add(q);
                    }
                    _logger.Info("Done setting up queues");

                    var enqueueTask = Run.InParallel(workItemCount, async index => {
                        var queue = queues[RandomData.GetInt(0, jobCount - 1)];
                        await queue.EnqueueAsync(new SampleQueueWorkItem {
                            Created = SystemClock.UtcNow,
                            Path = RandomData.GetString()
                        });
                    });
                    _logger.Info("Done enqueueing");

                    var cancellationTokenSource = new CancellationTokenSource();
                    await Run.InParallel(jobCount, async index => {
                        var queue = queues[index - 1];
                        var job = new SampleQueueJob(queue, metrics, Log);
                        await job.RunUntilEmptyAsync(cancellationTokenSource.Token);
                        cancellationTokenSource.Cancel();
                    });
                    _logger.Info("Done running jobs until empty");

                    await enqueueTask;

                    var queueStats = new List<QueueStats>();
                    for (int i = 0; i < queues.Count; i++) {
                        var stats = await queues[i].GetQueueStatsAsync();
                        _logger.Info("Queue#{i}: Working: {working} Completed: {completed} Abandoned: {abandoned} Error: {errors} Deadletter: {deadletter}", i, stats.Working, stats.Completed, stats.Abandoned, stats.Errors, stats.Deadletter);
                        queueStats.Add(stats);
                    }
                    _logger.Info("Done getting queue stats");

                    await metrics.FlushAsync();
                    _logger.Info("Done flushing metrics");

                    var queueSummary = await metrics.GetQueueStatsAsync("test.samplequeueworkitem");
                    Assert.Equal(queueStats.Sum(s => s.Completed), queueSummary.Completed.Count);
                    Assert.InRange(queueStats.Sum(s => s.Completed), 0, workItemCount);
                } finally {
                    foreach (var q in queues) {
                        await q.DeleteQueueAsync();
                        q.Dispose();
                    }
                }
            }
        }
Ejemplo n.º 2
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);
                    }
                }
            }
        }