Skip to content

Commit

Permalink
More interesting unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Nov 15, 2021
1 parent 7c17519 commit c23fda4
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

using System;
using System.Diagnostics.Metrics;
#if NET461
using System.Linq;
#endif
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Metrics;
Expand All @@ -42,6 +44,16 @@ public async Task EnterExitCollectTest()
throw new InvalidOperationException("PrometheusExporter could not be found on MeterProvider.");
}

int runningCollectCount = 0;
var collectFunc = exporter.Collect;
exporter.Collect = (timeout) =>
{
bool result = collectFunc(timeout);
runningCollectCount++;
Thread.Sleep(5000);
return result;
};

var counter = meter.CreateCounter<int>("counter_int", description: "Prometheus help text goes here \n escaping.");
counter.Add(100);

Expand All @@ -64,12 +76,30 @@ public async Task EnterExitCollectTest()

await Task.WhenAll(collectTasks).ConfigureAwait(false);

Assert.Equal(1, runningCollectCount);

byte[] firstBatch = collectTasks[0].Result;
for (int i = 1; i < collectTasks.Length; i++)
{
Assert.Equal(firstBatch, collectTasks[i].Result);
}

counter.Add(100);

// This should use the cache and ignore the second counter update.
var task = exporter.CollectionManager.EnterCollect();
Assert.True(task.IsCompleted);
ArraySegment<byte> data = await task.ConfigureAwait(false);
try
{
Assert.Equal(1, runningCollectCount);
Assert.Equal(firstBatch, data);
}
finally
{
exporter.CollectionManager.ExitCollect();
}

Thread.Sleep(exporter.Options.ScrapeResponseCacheDurationInMilliseconds);

counter.Add(100);
Expand All @@ -92,6 +122,7 @@ public async Task EnterExitCollectTest()

await Task.WhenAll(collectTasks).ConfigureAwait(false);

Assert.Equal(2, runningCollectCount);
Assert.NotEqual(firstBatch, collectTasks[0].Result);

firstBatch = collectTasks[0].Result;
Expand Down

0 comments on commit c23fda4

Please sign in to comment.