Skip to content

Commit

Permalink
Fix it so that the test actually tests something
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest committed Mar 15, 2023
1 parent bf41884 commit 2a86f96
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
12 changes: 4 additions & 8 deletions src/OpenTelemetry/Metrics/ExponentialHistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace OpenTelemetry.Metrics;

public sealed class ExponentialHistogramBuckets
{
private int size;
private long[] buckets = Array.Empty<long>();

internal ExponentialHistogramBuckets()
Expand All @@ -29,7 +28,7 @@ internal ExponentialHistogramBuckets()

public int Offset { get; private set; }

public Enumerator GetEnumerator() => new(this.size, this.buckets);
public Enumerator GetEnumerator() => new(this.buckets);

internal void SnapshotBuckets(CircularBufferBuckets buckets)
{
Expand All @@ -39,7 +38,6 @@ internal void SnapshotBuckets(CircularBufferBuckets buckets)
}

this.Offset = buckets.Offset;
this.size = buckets.Size;
buckets.Copy(this.buckets);
}

Expand All @@ -58,14 +56,12 @@ internal ExponentialHistogramBuckets Copy()
// Note: Does not implement IEnumerator<> to prevent accidental boxing.
public struct Enumerator
{
private readonly int size;
private readonly long[] buckets;
private int index;

internal Enumerator(int size, long[] buckets)
internal Enumerator(long[] buckets)
{
this.index = size;
this.size = size;
this.index = 0;
this.buckets = buckets;
this.Current = default;
}
Expand All @@ -85,7 +81,7 @@ internal Enumerator(int size, long[] buckets)
/// collection.</returns>
public bool MoveNext()
{
if (this.index < this.size)
if (this.index < this.buckets.Length)
{
this.Current = this.buckets[this.index++];
return true;
Expand Down
27 changes: 23 additions & 4 deletions test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,36 @@ internal static void AssertExponentialBucketsAreCorrect(Base2ExponentialBucketHi
Assert.Equal(expectedHistogram.PositiveBuckets.Offset, data.PositiveBuckets.Offset);
Assert.Equal(expectedHistogram.NegativeBuckets.Offset, data.NegativeBuckets.Offset);

var index = expectedHistogram.PositiveBuckets.Offset;
expectedHistogram.Snapshot();
var expectedData = expectedHistogram.GetExponentialHistogramData();

var actual = new List<long>();
foreach (var bucketCount in data.PositiveBuckets)
{
Assert.Equal(expectedHistogram.PositiveBuckets[index++], bucketCount);
actual.Add(bucketCount);
}

index = expectedHistogram.NegativeBuckets.Offset;
var expected = new List<long>();
foreach (var bucketCount in expectedData.PositiveBuckets)
{
expected.Add(bucketCount);
}

Assert.Equal(expected, actual);

actual = new List<long>();
foreach (var bucketCount in data.NegativeBuckets)
{
Assert.Equal(expectedHistogram.PositiveBuckets[index++], bucketCount);
actual.Add(bucketCount);
}

expected = new List<long>();
foreach (var bucketCount in expectedData.NegativeBuckets)
{
expected.Add(bucketCount);
}

Assert.Equal(expected, actual);
}

[Theory]
Expand Down

0 comments on commit 2a86f96

Please sign in to comment.