コード例 #1
0
        public async void CanWaitForCounter()
        {
            var metrics = new InMemoryMetricsClient();

            metrics.StartDisplayingStats(TimeSpan.FromMilliseconds(50), _writer);
            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test");
                metrics.Counter("Test");
            });
            var success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500), 2);

            Assert.True(success);

            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test");
            });
            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(100));

            Assert.False(success);

            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test", 2);
            });
            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500), 2);

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", TimeSpan.FromMilliseconds(500), 1,
                                                        async() => await metrics.CounterAsync("Test"));

            Assert.True(success);

            Task.Run(() => {
                Thread.Sleep(50);
                metrics.Counter("Test");
            });
            success = metrics.WaitForCounter("Test", TimeSpan.FromMilliseconds(500));
            Assert.True(success);

            metrics.DisplayStats(_writer);
        }
コード例 #2
0
        public async Task CanWaitForCounter()
        {
            var metrics = new InMemoryMetricsClient();

            metrics.StartDisplayingStats(TimeSpan.FromMilliseconds(50), _writer);
            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test");
                await metrics.CounterAsync("Test");
            });

            var success = await metrics.WaitForCounterAsync("Test", 2, TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test");
            });

            success = await metrics.WaitForCounterAsync("Test", timeout : TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", timeout : TimeSpan.FromMilliseconds(100));

            Assert.False(success);

            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test", 2);
            });

            success = await metrics.WaitForCounterAsync("Test", 2, TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            success = await metrics.WaitForCounterAsync("Test", async() => await metrics.CounterAsync("Test"), cancellationToken : TimeSpan.FromMilliseconds(500).ToCancellationToken());

            Assert.True(success);

            Task.Run(async() => {
                await Task.Delay(50);
                await metrics.CounterAsync("Test");
            });

            success = await metrics.WaitForCounterAsync("Test", timeout : TimeSpan.FromMilliseconds(500));

            Assert.True(success);

            metrics.DisplayStats(_writer);
        }
コード例 #3
0
        public virtual async Task CanHandleErrorInWorker()
        {
            var queue = GetQueue(retries: 0);

            if (queue == null)
            {
                return;
            }

            Log.SetLogLevel <InMemoryMetricsClient>(LogLevel.Trace);
            Log.SetLogLevel <MetricsQueueBehavior <SimpleWorkItem> >(LogLevel.Trace);
            Log.SetLogLevel <InMemoryQueue <SimpleWorkItem> >(LogLevel.Trace);

            using (queue) {
                await queue.DeleteQueueAsync();
                await AssertEmptyQueueAsync(queue);

                using (var metrics = new InMemoryMetricsClient(false, loggerFactory: Log)) {
                    queue.AttachBehavior(new MetricsQueueBehavior <SimpleWorkItem>(metrics, loggerFactory: Log));

                    await queue.StartWorkingAsync(w => {
                        Debug.WriteLine("WorkAction");
                        Assert.Equal("Hello", w.Value.Data);
                        throw new Exception();
                    });

                    await SystemClock.SleepAsync(1);

                    var success = await metrics.WaitForCounterAsync("simpleworkitem.hello.abandoned", async() => await queue.EnqueueAsync(new SimpleWorkItem {
                        Data = "Hello"
                    }), cancellationToken : TimeSpan.FromSeconds(2).ToCancellationToken());

                    Assert.True(success);
                    await SystemClock.SleepAsync(1);

                    var stats = await queue.GetQueueStatsAsync();

                    _logger.Info("Completed: {completed} Errors: {errors} Deadletter: {deadletter} Working: {working} ", stats.Completed, stats.Errors, stats.Deadletter, stats.Working);
                    Assert.Equal(0, stats.Completed);
                    Assert.Equal(1, stats.Errors);
                    Assert.Equal(1, stats.Deadletter);
                }
            }
        }
コード例 #4
0
        public virtual async Task CanHandleErrorInWorker()
        {
            var queue = GetQueue(retries: 0);

            if (queue == null)
            {
                return;
            }

            var metrics = new InMemoryMetricsClient();

            queue.AttachBehavior(new MetricsQueueBehavior <SimpleWorkItem>(metrics));

            using (queue)
            {
                await queue.DeleteQueueAsync();

                queue.StartWorking(w => {
                    Debug.WriteLine("WorkAction");
                    Assert.Equal("Hello", w.Value.Data);
                    throw new ApplicationException();
                });

                metrics.DisplayStats(_writer);
                var success = await metrics.WaitForCounterAsync("simpleworkitem.hello.abandoned", async() => await queue.EnqueueAsync(new SimpleWorkItem
                {
                    Data = "Hello"
                }), cancellationToken : TimeSpan.FromSeconds(1).ToCancellationToken());

                await Task.Delay(10);

                metrics.DisplayStats(_writer);
                Assert.True(success);

                var stats = await queue.GetQueueStatsAsync();

                Assert.Equal(0, stats.Completed);
                Assert.Equal(1, stats.Errors);
                Assert.Equal(1, stats.Deadletter);
            }
        }