Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into jrebagliatti/CVE-2021…
Browse files Browse the repository at this point in the history
  • Loading branch information
jrebagliatti committed Apr 17, 2023
2 parents a547e61 + 30b7b79 commit 4558595
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,42 @@ public void HistogramWithOnlySumCount()
Assert.False(enumerator.MoveNext());
}

[Fact]
public void MultiThreadedHistogramUpdateAndSnapShotTest()
{
var boundaries = Array.Empty<double>();
var histogramPoint = new MetricPoint(this.aggregatorStore, AggregationType.Histogram, null, boundaries, Metric.DefaultExponentialHistogramMaxBuckets, Metric.DefaultExponentialHistogramMaxScale);
var argsToThread = new ThreadArguments
{
HistogramPoint = histogramPoint,
MreToEnsureAllThreadsStart = new ManualResetEvent(false),
};

var numberOfThreads = 2;
var snapshotThread = new Thread(HistogramSnapshotThread);
Thread[] updateThreads = new Thread[numberOfThreads];
for (int i = 0; i < numberOfThreads; ++i)
{
updateThreads[i] = new Thread(HistogramUpdateThread);
updateThreads[i].Start(argsToThread);
}

snapshotThread.Start(argsToThread);

for (int i = 0; i < numberOfThreads; ++i)
{
updateThreads[i].Join();
}

snapshotThread.Join();

// last snapshot
histogramPoint.TakeSnapshot(outputDelta: true);

var lastDelta = histogramPoint.GetHistogramSum();
Assert.Equal(200, argsToThread.SumOfDelta + lastDelta);
}

internal static void AssertExponentialBucketsAreCorrect(Base2ExponentialBucketHistogram expectedHistogram, ExponentialHistogramData data)
{
Assert.Equal(expectedHistogram.Scale, data.Scale);
Expand Down Expand Up @@ -372,5 +408,55 @@ internal void ExponentialMaxScaleConfigWorks(int? maxScale)
var expectedScale = maxScale.HasValue ? maxScale : Metric.DefaultExponentialHistogramMaxScale;
Assert.Equal(expectedScale, metricPoint.GetExponentialHistogramData().Scale);
}

private static void HistogramSnapshotThread(object obj)
{
var args = obj as ThreadArguments;
var mreToEnsureAllThreadsStart = args.MreToEnsureAllThreadsStart;

if (Interlocked.Increment(ref args.ThreadStartedCount) == 3)
{
mreToEnsureAllThreadsStart.Set();
}

mreToEnsureAllThreadsStart.WaitOne();

double curSnapshotDelta;
while (Interlocked.Read(ref args.ThreadsFinishedAllUpdatesCount) != 2)
{
args.HistogramPoint.TakeSnapshot(outputDelta: true);
curSnapshotDelta = args.HistogramPoint.GetHistogramSum();
args.SumOfDelta += curSnapshotDelta;
}
}

private static void HistogramUpdateThread(object obj)
{
var args = obj as ThreadArguments;
var mreToEnsureAllThreadsStart = args.MreToEnsureAllThreadsStart;

if (Interlocked.Increment(ref args.ThreadStartedCount) == 3)
{
mreToEnsureAllThreadsStart.Set();
}

mreToEnsureAllThreadsStart.WaitOne();

for (int i = 0; i < 10; ++i)
{
args.HistogramPoint.Update(10);
}

Interlocked.Increment(ref args.ThreadsFinishedAllUpdatesCount);
}

private class ThreadArguments
{
public MetricPoint HistogramPoint;
public ManualResetEvent MreToEnsureAllThreadsStart;
public int ThreadStartedCount;
public long ThreadsFinishedAllUpdatesCount;
public double SumOfDelta;
}
}
}

0 comments on commit 4558595

Please sign in to comment.