mirror of
https://github.com/google/nomulus.git
synced 2025-05-14 16:37:13 +02:00
Add a convenient method to DistributionMetricSubject
Currently to assert that a given Metric<Distribution> as a certain distribution for some labels, the caller needs to manually create an ImmutableDistribution and pass it to #hasValueForLabels method. With this change, an ImmutableSet of data points can be passed to #hasDataSetForLabels method. Also switched to use expectThrow backport from JUnit 4.13. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=173544521
This commit is contained in:
parent
4a9b8b918a
commit
4a5b9fc288
4 changed files with 135 additions and 38 deletions
|
@ -107,7 +107,7 @@ abstract class AbstractMetricSubject<T, S extends AbstractMetricSubject<T, S>>
|
||||||
}
|
}
|
||||||
if (!metricPoint.value().equals(value)) {
|
if (!metricPoint.value().equals(value)) {
|
||||||
failWithBadResults(
|
failWithBadResults(
|
||||||
String.format("has a value of %s for labels", value),
|
String.format("has a value of %s for labels", getMessageRepresentation(value)),
|
||||||
Joiner.on(':').join(labels),
|
Joiner.on(':').join(labels),
|
||||||
"has a value of",
|
"has a value of",
|
||||||
getMessageRepresentation(metricPoint.value()));
|
getMessageRepresentation(metricPoint.value()));
|
||||||
|
|
|
@ -16,12 +16,17 @@ package google.registry.monitoring.metrics.contrib;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertAbout;
|
import static com.google.common.truth.Truth.assertAbout;
|
||||||
|
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
import com.google.common.collect.BoundType;
|
import com.google.common.collect.BoundType;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
import com.google.common.collect.Range;
|
import com.google.common.collect.Range;
|
||||||
import com.google.common.truth.FailureMetadata;
|
import com.google.common.truth.FailureMetadata;
|
||||||
import google.registry.monitoring.metrics.Distribution;
|
import google.registry.monitoring.metrics.Distribution;
|
||||||
|
import google.registry.monitoring.metrics.ImmutableDistribution;
|
||||||
import google.registry.monitoring.metrics.Metric;
|
import google.registry.monitoring.metrics.Metric;
|
||||||
import google.registry.monitoring.metrics.MetricPoint;
|
import google.registry.monitoring.metrics.MetricPoint;
|
||||||
|
import google.registry.monitoring.metrics.MutableDistribution;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
@ -36,6 +41,8 @@ import javax.annotation.Nullable;
|
||||||
* .hasNoOtherValues();
|
* .hasNoOtherValues();
|
||||||
* assertThat(myDistributionMetric)
|
* assertThat(myDistributionMetric)
|
||||||
* .doesNotHaveAnyValueForLabels("label1", "label2");
|
* .doesNotHaveAnyValueForLabels("label1", "label2");
|
||||||
|
* assertThat(myDistributionMetric)
|
||||||
|
* .hasDataSetForLabels(ImmutableSet.of(data1, data2, data3), "label1", "label2");
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* <p>The assertions treat an empty distribution as no value at all. This is not how the data is
|
* <p>The assertions treat an empty distribution as no value at all. This is not how the data is
|
||||||
|
@ -94,4 +101,25 @@ public final class DistributionMetricSubject
|
||||||
sb.append('}');
|
sb.append('}');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that the distribution for the given label can be constructed from the given data set.
|
||||||
|
*
|
||||||
|
* <p>Note that this only tests that the distribution has the same binned histogram as it would if
|
||||||
|
* it had recorded the specified data points. It could have in fact collected different data
|
||||||
|
* points that resulted in the same histogram, but that information is lost to us and cannot be
|
||||||
|
* tested.
|
||||||
|
*/
|
||||||
|
public And<DistributionMetricSubject> hasDataSetForLabels(
|
||||||
|
ImmutableSet<? extends Number> dataSet, String... labels) {
|
||||||
|
ImmutableList<MetricPoint<Distribution>> metricPoints = actual().getTimestampedValues();
|
||||||
|
if (metricPoints.isEmpty()) {
|
||||||
|
failWithBadResults(
|
||||||
|
"has a distribution for labels", Joiner.on(':').join(labels), "has", "no values");
|
||||||
|
}
|
||||||
|
MutableDistribution targetDistribution =
|
||||||
|
new MutableDistribution(metricPoints.get(0).value().distributionFitter());
|
||||||
|
dataSet.forEach(data -> targetDistribution.add(data.doubleValue()));
|
||||||
|
return hasValueForLabels(ImmutableDistribution.copyOf(targetDistribution), labels);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ java_library(
|
||||||
deps = [
|
deps = [
|
||||||
"//java/google/registry/monitoring/metrics",
|
"//java/google/registry/monitoring/metrics",
|
||||||
"//java/google/registry/monitoring/metrics/contrib",
|
"//java/google/registry/monitoring/metrics/contrib",
|
||||||
|
"//javatests/google/registry/testing",
|
||||||
"@com_google_guava",
|
"@com_google_guava",
|
||||||
"@com_google_truth",
|
"@com_google_truth",
|
||||||
"@com_google_truth_extensions_truth_java8_extension",
|
"@com_google_truth_extensions_truth_java8_extension",
|
||||||
|
|
|
@ -15,9 +15,8 @@
|
||||||
package google.registry.monitoring.metrics.contrib;
|
package google.registry.monitoring.metrics.contrib;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static com.google.common.truth.Truth8.assertThat;
|
|
||||||
import static google.registry.monitoring.metrics.contrib.DistributionMetricSubject.assertThat;
|
import static google.registry.monitoring.metrics.contrib.DistributionMetricSubject.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
import static google.registry.testing.JUnitBackports.expectThrows;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
import google.registry.monitoring.metrics.EventMetric;
|
import google.registry.monitoring.metrics.EventMetric;
|
||||||
|
@ -54,17 +53,15 @@ public class DistributionMetricSubjectTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWrongNumberOfLabels_fails() {
|
public void testWrongNumberOfLabels_fails() {
|
||||||
try {
|
AssertionError e =
|
||||||
assertThat(metric).hasAnyValueForLabels("Domestic");
|
expectThrows(
|
||||||
fail("Expected assertion error");
|
AssertionError.class, () -> assertThat(metric).hasAnyValueForLabels("Domestic"));
|
||||||
} catch (AssertionError e) {
|
assertThat(e)
|
||||||
assertThat(e)
|
.hasMessageThat()
|
||||||
.hasMessageThat()
|
.isEqualTo(
|
||||||
.isEqualTo(
|
"Not true that </test/event/sheep> has a value for labels <Domestic>."
|
||||||
"Not true that </test/event/sheep> has a value for labels <Domestic>."
|
+ " It has labeled values <[Bighorn:Blue =>"
|
||||||
+ " It has labeled values <[Bighorn:Blue =>"
|
+ " {[4.0..16.0)=1}, Domestic:Green => {[1.0..4.0)=1}]>");
|
||||||
+ " {[4.0..16.0)=1}, Domestic:Green => {[1.0..4.0)=1}]>");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -89,33 +86,104 @@ public class DistributionMetricSubjectTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDoesNotHaveValueForLabels_failure() {
|
public void testDoesNotHaveValueForLabels_failure() {
|
||||||
try {
|
AssertionError e =
|
||||||
assertThat(metric).doesNotHaveAnyValueForLabels("Domestic", "Green");
|
expectThrows(
|
||||||
fail("Expected assertion error");
|
AssertionError.class,
|
||||||
} catch (AssertionError e) {
|
() -> assertThat(metric).doesNotHaveAnyValueForLabels("Domestic", "Green"));
|
||||||
assertThat(e)
|
assertThat(e)
|
||||||
.hasMessageThat()
|
.hasMessageThat()
|
||||||
.isEqualTo(
|
.isEqualTo(
|
||||||
"Not true that </test/event/sheep> has no value for labels <Domestic:Green>."
|
"Not true that </test/event/sheep> has no value for labels <Domestic:Green>."
|
||||||
+ " It has a value of <{[1.0..4.0)=1}>");
|
+ " It has a value of <{[1.0..4.0)=1}>");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnexpectedValue_failure() {
|
public void testUnexpectedValue_failure() {
|
||||||
try {
|
AssertionError e =
|
||||||
assertThat(metric)
|
expectThrows(
|
||||||
.hasAnyValueForLabels("Domestic", "Green")
|
AssertionError.class,
|
||||||
.and()
|
() ->
|
||||||
.hasNoOtherValues();
|
assertThat(metric)
|
||||||
fail("Expected assertion error");
|
.hasAnyValueForLabels("Domestic", "Green")
|
||||||
} catch (AssertionError e) {
|
.and()
|
||||||
assertThat(e)
|
.hasNoOtherValues());
|
||||||
.hasMessageThat()
|
assertThat(e)
|
||||||
.isEqualTo(
|
.hasMessageThat()
|
||||||
"Not true that </test/event/sheep> has <no other nondefault values>."
|
.isEqualTo(
|
||||||
+ " It has labeled values <[Bighorn:Blue =>"
|
"Not true that </test/event/sheep> has <no other nondefault values>."
|
||||||
+ " {[4.0..16.0)=1}, Domestic:Green => {[1.0..4.0)=1}]>");
|
+ " It has labeled values <[Bighorn:Blue =>"
|
||||||
}
|
+ " {[4.0..16.0)=1}, Domestic:Green => {[1.0..4.0)=1}]>");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExpectedDataSet_success() {
|
||||||
|
metric.record(7.5, "Domestic", "Green");
|
||||||
|
assertThat(metric).hasDataSetForLabels(ImmutableSet.of(2.5, 7.5), "Domestic", "Green");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExpectedDataSetsChained_success() {
|
||||||
|
metric.record(7.5, "Domestic", "Green");
|
||||||
|
assertThat(metric)
|
||||||
|
.hasDataSetForLabels(ImmutableSet.of(2.5, 7.5), "Domestic", "Green")
|
||||||
|
.and()
|
||||||
|
.hasDataSetForLabels(ImmutableSet.of(10), "Bighorn", "Blue")
|
||||||
|
.and()
|
||||||
|
.hasNoOtherValues();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnexpectedDataSet_failure() {
|
||||||
|
AssertionError e =
|
||||||
|
expectThrows(
|
||||||
|
AssertionError.class,
|
||||||
|
() ->
|
||||||
|
assertThat(metric)
|
||||||
|
.hasDataSetForLabels(ImmutableSet.of(2.5, 7.5), "Domestic", "Green"));
|
||||||
|
assertThat(e)
|
||||||
|
.hasMessageThat()
|
||||||
|
.isEqualTo(
|
||||||
|
"Not true that </test/event/sheep> has a value of"
|
||||||
|
+ " {[1.0..4.0)=1,[4.0..16.0)=1} for labels <Domestic:Green>."
|
||||||
|
+ " It has a value of <{[1.0..4.0)=1}>");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNonExistentLabels_failure() {
|
||||||
|
AssertionError e =
|
||||||
|
expectThrows(
|
||||||
|
AssertionError.class,
|
||||||
|
() ->
|
||||||
|
assertThat(metric)
|
||||||
|
.hasDataSetForLabels(ImmutableSet.of(2.5, 7.5), "Domestic", "Blue"));
|
||||||
|
assertThat(e)
|
||||||
|
.hasMessageThat()
|
||||||
|
.isEqualTo(
|
||||||
|
"Not true that </test/event/sheep> has a value for labels <Domestic:Blue>."
|
||||||
|
+ " It has labeled values <[Bighorn:Blue => {[4.0..16.0)=1},"
|
||||||
|
+ " Domestic:Green => {[1.0..4.0)=1}]>");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmptyMetric_failure() {
|
||||||
|
EventMetric emptyMetric =
|
||||||
|
MetricRegistryImpl.getDefault()
|
||||||
|
.newEventMetric(
|
||||||
|
"/test/event/goat",
|
||||||
|
"Sheep Latency",
|
||||||
|
"sheeplatency",
|
||||||
|
LABEL_DESCRIPTORS,
|
||||||
|
EventMetric.DEFAULT_FITTER);
|
||||||
|
AssertionError e =
|
||||||
|
expectThrows(
|
||||||
|
AssertionError.class,
|
||||||
|
() ->
|
||||||
|
assertThat(emptyMetric)
|
||||||
|
.hasDataSetForLabels(ImmutableSet.of(2.5, 7.5), "Domestic", "Blue"));
|
||||||
|
assertThat(e)
|
||||||
|
.hasMessageThat()
|
||||||
|
.isEqualTo(
|
||||||
|
"Not true that </test/event/goat> has a distribution for labels <Domestic:Blue>."
|
||||||
|
+ " It has <no values>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue