public async Task TestNoOverlap() { /* Setup mocks */ TaskCompletionSource <bool> scrapeTaskSource = new TaskCompletionSource <bool>(); TaskCompletionSource <bool> uploadTaskSource = new TaskCompletionSource <bool>(); var scraper = new Mock <IMetricsScraper>(); scraper.Setup(s => s.ScrapeEndpointsAsync(CancellationToken.None)).Returns(async() => { await scrapeTaskSource.Task; return(this.PrometheousMetrics(Enumerable.Range(1, 10).Select(i => ($"module_{i}", 1.0)).ToArray())); }); var storage = new Mock <IMetricsStorage>(); var uploader = new Mock <IMetricsPublisher>(); uploader.Setup(u => u.PublishAsync(It.IsAny <IEnumerable <Metric> >(), It.IsAny <CancellationToken>())).Returns(async() => await uploadTaskSource.Task); MetricsWorker worker = new MetricsWorker(scraper.Object, storage.Object, uploader.Object); /* test scraper first */ var scrapeTask = worker.Scrape(CancellationToken.None); await Task.Delay(1); var uploadTask = worker.Upload(CancellationToken.None); await Task.Delay(1); uploadTaskSource.SetResult(true); await Task.Delay(1); Assert.False(scrapeTask.IsCompleted); Assert.False(uploadTask.IsCompleted); scrapeTaskSource.SetResult(true); await Task.Delay(1); await Task.WhenAll(scrapeTask, uploadTask); /* test uploader first */ scrapeTaskSource = new TaskCompletionSource <bool>(); uploadTaskSource = new TaskCompletionSource <bool>(); uploadTask = worker.Upload(CancellationToken.None); await Task.Delay(1); scrapeTask = worker.Scrape(CancellationToken.None); await Task.Delay(1); scrapeTaskSource.SetResult(true); await Task.Delay(1); Assert.False(scrapeTask.IsCompleted); Assert.False(uploadTask.IsCompleted); uploadTaskSource.SetResult(true); await Task.Delay(1); await Task.WhenAll(scrapeTask, uploadTask); }
public async Task TestUploadContent() { /* test data */ var metrics = Enumerable.Range(1, 10).Select(i => new Metric(DateTime.UtcNow, "test_metric", 3, $"tag_{i}")).ToList(); /* Setup mocks */ var scraper = new Mock <IMetricsScraper>(); var storage = new Mock <IMetricsStorage>(); storage.Setup(s => s.GetAllMetricsAsync()).ReturnsAsync(metrics); var uploader = new Mock <IMetricsPublisher>(); IEnumerable <Metric> uploadedData = Enumerable.Empty <Metric>(); uploader.Setup(u => u.PublishAsync(It.IsAny <IEnumerable <Metric> >(), It.IsAny <CancellationToken>())).Callback((Action <IEnumerable <Metric>, CancellationToken>)((d, _) => uploadedData = d)).ReturnsAsync(true); MetricsWorker worker = new MetricsWorker(scraper.Object, storage.Object, uploader.Object); /* test */ await worker.Upload(CancellationToken.None); Assert.Equal(metrics.OrderBy(x => x.Tags), uploadedData.OrderBy(x => x.Tags)); Assert.Single(storage.Invocations.Where(i => i.Method.Name == "GetAllMetricsAsync")); Assert.Single(storage.Invocations.Where(i => i.Method.Name == "RemoveAllReturnedMetricsAsync")); Assert.Single(uploader.Invocations); }
public async Task TestBasicUploading() { /* Setup mocks */ var scraper = new Mock <IMetricsScraper>(); var storage = new Mock <IMetricsStorage>(); storage.Setup(s => s.GetAllMetricsAsync()).ReturnsAsync(Enumerable.Empty <Metric>()); TaskCompletionSource <object> uploadStarted = new TaskCompletionSource <object>(); TaskCompletionSource <bool> finishUpload = new TaskCompletionSource <bool>(); var uploader = new Mock <IMetricsPublisher>(); IEnumerable <Metric> uploadedData = Enumerable.Empty <Metric>(); uploader.Setup(u => u.PublishAsync(It.IsAny <IEnumerable <Metric> >(), It.IsAny <CancellationToken>())).Callback((Action <IEnumerable <Metric>, CancellationToken>)((data, __) => { uploadedData = data; uploadStarted.SetResult(null); })).Returns(finishUpload.Task); MetricsWorker worker = new MetricsWorker(scraper.Object, storage.Object, uploader.Object); /* test */ Task workerTask = worker.Upload(CancellationToken.None); await uploadStarted.Task; uploadedData.ToList(); Assert.Equal(1, uploader.Invocations.Count); Assert.Single(storage.Invocations.Where(i => i.Method.Name == "GetAllMetricsAsync")); Assert.Empty(storage.Invocations.Where(i => i.Method.Name == "RemoveAllReturnedMetricsAsync")); finishUpload.SetResult(true); Assert.Single(storage.Invocations.Where(i => i.Method.Name == "GetAllMetricsAsync")); Assert.Single(storage.Invocations.Where(i => i.Method.Name == "RemoveAllReturnedMetricsAsync")); }
public async Task TestUploadIsLazy() { /* test data */ int metricsCalls = 0; IEnumerable <Metric> Metrics() { metricsCalls++; return(Enumerable.Range(1, 10).Select(i => new Metric(DateTime.UtcNow, "1", 3, new Dictionary <string, string> { { "id", $"{i}" } }))); } /* Setup mocks */ var scraper = new Mock <IMetricsScraper>(); var storage = new Mock <IMetricsStorage>(); storage.Setup(s => s.GetAllMetricsAsync()).ReturnsAsync(Metrics); var uploader = new Mock <IMetricsPublisher>(); IEnumerable <Metric> uploadedData = Enumerable.Empty <Metric>(); uploader.Setup(u => u.PublishAsync(It.IsAny <IEnumerable <Metric> >(), It.IsAny <CancellationToken>())).Callback((Action <IEnumerable <Metric>, CancellationToken>)((d, _) => uploadedData = d)).ReturnsAsync(true); MetricsWorker worker = new MetricsWorker(scraper.Object, storage.Object, uploader.Object); /* test */ await worker.Upload(CancellationToken.None); int numMetrics = 0; foreach (Metric metric in uploadedData) { Assert.Equal(numMetrics++ / 10 + 1, metricsCalls); } Assert.Single(storage.Invocations.Where(i => i.Method.Name == "GetAllMetricsAsync")); Assert.Single(storage.Invocations.Where(i => i.Method.Name == "RemoveAllReturnedMetricsAsync")); Assert.Single(uploader.Invocations); }
public async Task TestRetryFail() { int maxRetries = 5; /* Setup mocks */ var scraper = new Mock <IMetricsScraper>(); var storage = new Mock <IMetricsStorage>(); storage.Setup(s => s.GetAllMetricsAsync()).ReturnsAsync(Enumerable.Empty <Metric>()); var uploader = new Mock <IMetricsPublisher>(); uploader.Setup(u => u.PublishAsync(It.IsAny <IEnumerable <Metric> >(), It.IsAny <CancellationToken>())).ReturnsAsync(false); MetricsWorker.RetryStrategy = new FakeRetryStrategy(maxRetries); MetricsWorker worker = new MetricsWorker(scraper.Object, storage.Object, uploader.Object); /* test */ await worker.Upload(CancellationToken.None); Assert.Equal(maxRetries + 1, uploader.Invocations.Count); // It tries once, then retries maxRetryTimes. Assert.Single(storage.Invocations.Where(i => i.Method.Name == "RemoveAllReturnedMetricsAsync")); }
public async Task TestRetry() { int targetRetries = 10; /* Setup mocks */ var scraper = new Mock <IMetricsScraper>(); var storage = new Mock <IMetricsStorage>(); storage.Setup(s => s.GetAllMetricsAsync()).ReturnsAsync(Enumerable.Empty <Metric>()); var uploader = new Mock <IMetricsPublisher>(); int actualRetries = 0; uploader.Setup(u => u.PublishAsync(It.IsAny <IEnumerable <Metric> >(), It.IsAny <CancellationToken>())).ReturnsAsync(() => ++ actualRetries == targetRetries); MetricsWorker.RetryStrategy = new FakeRetryStrategy(); MetricsWorker worker = new MetricsWorker(scraper.Object, storage.Object, uploader.Object); /* test */ await worker.Upload(CancellationToken.None); Assert.Equal(targetRetries, actualRetries); Assert.Equal(targetRetries, uploader.Invocations.Count); Assert.Single(storage.Invocations.Where(i => i.Method.Name == "RemoveAllReturnedMetricsAsync")); }