Skip to content

Commit

Permalink
Added sanity unit testing 2 data classes just to pass code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
asafm committed Jan 9, 2024
1 parent ccfce83 commit 95690f1
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.opentelemetry.sdk.metrics.internal.data;

import io.opentelemetry.sdk.internal.DynamicPrimitiveLongList;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class MutableExponentialHistogramBucketsTest {

@Test
void testSanity() {
MutableExponentialHistogramBuckets buckets = new MutableExponentialHistogramBuckets();
assertThat(buckets.getScale()).isEqualTo(0);
assertThat(buckets.getOffset()).isEqualTo(0);
assertThat(buckets.getTotalCount()).isEqualTo(0);
assertThat(buckets.getBucketCounts()).isEmpty();
assertThat(buckets.getReusableBucketCountsList()).isEmpty();

DynamicPrimitiveLongList bucketCounts = DynamicPrimitiveLongList.of(1,2,3);
buckets.set(1, 2, 3, bucketCounts);

assertThat(buckets.getScale()).isEqualTo(1);
assertThat(buckets.getOffset()).isEqualTo(2);
assertThat(buckets.getTotalCount()).isEqualTo(3);
assertThat(buckets.getBucketCounts()).containsExactly(1L, 2L, 3L);
assertThat(buckets.getReusableBucketCountsList()).containsExactly(1L, 2L, 3L);

MutableExponentialHistogramBuckets sameBuckets = new MutableExponentialHistogramBuckets();
sameBuckets.set(1, 2, 3, DynamicPrimitiveLongList.of(1,2,3));
assertThat(sameBuckets).isEqualTo(buckets);
assertThat(sameBuckets.hashCode()).isEqualTo(buckets.hashCode());

assertThat(buckets.toString()).isEqualTo("MutableExponentialHistogramBuckets{scale=1, offset=2, bucketCounts=[1, 2, 3], totalCount=3}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.opentelemetry.sdk.metrics.internal.data;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.internal.DynamicPrimitiveLongList;
import org.junit.jupiter.api.Test;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

class MutableExponentialHistogramPointDataTest {

@Test
public void testSanity() {
MutableExponentialHistogramPointData pointData = new MutableExponentialHistogramPointData();
assertThat(pointData.getSum()).isEqualTo(0);
assertThat(pointData.getCount()).isEqualTo(0);
assertThat(pointData.getPositiveBuckets().getTotalCount()).isEqualTo(0);
assertThat(pointData.getNegativeBuckets().getTotalCount()).isEqualTo(0);
assertThat(pointData.getExemplars()).isEmpty();

MutableExponentialHistogramBuckets positiveBuckets = new MutableExponentialHistogramBuckets();
positiveBuckets.set(/* scale= */1, /* offset= */2, /* totalCount= */3,
DynamicPrimitiveLongList.of(1, 2, 3));
MutableExponentialHistogramBuckets negativeBuckets = new MutableExponentialHistogramBuckets();
negativeBuckets.set(10, 20, 30, DynamicPrimitiveLongList.of(50, 60, 70));

pointData.set(
/* scale= */1,
/* sum= */2,
/* zeroCount= */10,
/* hasMin= */true,
/* min= */100,
/* hasMax= */true,
/* max= */1000, positiveBuckets, negativeBuckets,
/* startEpochNanos= */10,
/* epochNanos= */20,
Attributes.of(AttributeKey.stringKey("foo"), "bar"),
Collections.emptyList());

assertThat(pointData.getSum()).isEqualTo(2);
assertThat(pointData.getCount()).isEqualTo(10+30+3);
assertThat(pointData.getAttributes().get(AttributeKey.stringKey("foo"))).isEqualTo("bar");
assertThat(pointData.getAttributes().size()).isEqualTo(1);
assertThat(pointData.getScale()).isEqualTo(1);
assertThat(pointData.getZeroCount()).isEqualTo(10);
assertThat(pointData.hasMin()).isTrue();
assertThat(pointData.getMin()).isEqualTo(100);
assertThat(pointData.hasMax()).isTrue();
assertThat(pointData.getMax()).isEqualTo(1000);
assertThat(pointData.getPositiveBuckets().getTotalCount()).isEqualTo(3);
assertThat(pointData.getNegativeBuckets().getTotalCount()).isEqualTo(30);
assertThat(pointData.getPositiveBuckets().getBucketCounts()).containsExactly(1L, 2L, 3L);
assertThat(pointData.getNegativeBuckets().getBucketCounts()).containsExactly(50L, 60L, 70L);
assertThat(pointData.getStartEpochNanos()).isEqualTo(10);
assertThat(pointData.getEpochNanos()).isEqualTo(20);
assertThat(pointData.getExemplars()).isEmpty();

assertThat(pointData.toString()).isEqualTo(
"MutableExponentialHistogramPointData{startEpochNanos=10, epochNanos=20, "
+ "attributes={foo=\"bar\"}, scale=1, sum=2.0, count=43, zeroCount=10, hasMin=true, "
+ "min=100.0, hasMax=true, max=1000.0, "
+ "positiveBuckets=MutableExponentialHistogramBuckets{scale=1, offset=2, "
+ "bucketCounts=[1, 2, 3], totalCount=3}, "
+ "negativeBuckets=MutableExponentialHistogramBuckets{scale=10, offset=20, "
+ "bucketCounts=[50, 60, 70], totalCount=30}, exemplars=[]}"
);


MutableExponentialHistogramPointData samePointData = new MutableExponentialHistogramPointData();
samePointData.set(
/* scale= */1,
/* sum= */2,
/* zeroCount= */10,
/* hasMin= */true,
/* min= */100,
/* hasMax= */true,
/* max= */1000, positiveBuckets, negativeBuckets,
/* startEpochNanos= */10,
/* epochNanos= */20,
Attributes.of(AttributeKey.stringKey("foo"), "bar"),
Collections.emptyList());
assertThat(samePointData).isEqualTo(pointData);
}
}

0 comments on commit 95690f1

Please sign in to comment.