Introduce experiment comparison methods for suppliers.

This commit is contained in:
Jon Chambers
2020-06-09 17:14:46 -04:00
committed by Jon Chambers
parent 0713da7393
commit 0671f05c05
10 changed files with 112 additions and 35 deletions

View File

@@ -1,6 +1,5 @@
package org.whispersystems.textsecuregcm.experiment;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Timer;
@@ -9,9 +8,7 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
@@ -20,7 +17,6 @@ import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.anyVararg;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -37,11 +33,11 @@ public class ExperimentTest {
}
@Test
public void compareResultMatch() {
public void compareFutureResultMatch() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareResult(12, CompletableFuture.completedFuture(12));
new Experiment(meterRegistry, "test").compareFutureResult(12, CompletableFuture.completedFuture(12));
@SuppressWarnings("unchecked") final ArgumentCaptor<Iterable<Tag>> tagCaptor = ArgumentCaptor.forClass(Iterable.class);
@@ -54,11 +50,11 @@ public class ExperimentTest {
}
@Test
public void compareResultMismatch() {
public void compareFutureResultMismatch() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareResult(12, CompletableFuture.completedFuture(77));
new Experiment(meterRegistry, "test").compareFutureResult(12, CompletableFuture.completedFuture(77));
@SuppressWarnings("unchecked") final ArgumentCaptor<Iterable<Tag>> tagCaptor = ArgumentCaptor.forClass(Iterable.class);
@@ -71,11 +67,11 @@ public class ExperimentTest {
}
@Test
public void compareResultError() {
public void compareFutureResultError() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareResult(12, CompletableFuture.failedFuture(new RuntimeException("OH NO")));
new Experiment(meterRegistry, "test").compareFutureResult(12, CompletableFuture.failedFuture(new RuntimeException("OH NO")));
@SuppressWarnings("unchecked") final ArgumentCaptor<Iterable<Tag>> tagCaptor = ArgumentCaptor.forClass(Iterable.class);
@@ -88,11 +84,72 @@ public class ExperimentTest {
}
@Test
public void compareResultNoExperimentData() {
public void compareFutureResultNoExperimentData() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareResult(12, CompletableFuture.completedFuture(null));
new Experiment(meterRegistry, "test").compareFutureResult(12, CompletableFuture.completedFuture(null));
verify(timer, never()).record(anyLong(), eq(TimeUnit.NANOSECONDS));
}
@Test
public void compareSupplierResultMatch() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareSupplierResult(12, () -> 12);
@SuppressWarnings("unchecked") final ArgumentCaptor<Iterable<Tag>> tagCaptor = ArgumentCaptor.forClass(Iterable.class);
verify(meterRegistry).timer(anyString(), tagCaptor.capture());
final Set<Tag> tags = getTagSet(tagCaptor.getValue());
assertEquals(tags, Set.of(Tag.of(Experiment.OUTCOME_TAG, Experiment.MATCH_OUTCOME)));
verify(timer).record(anyLong(), eq(TimeUnit.NANOSECONDS));
}
@Test
public void compareSupplierResultMismatch() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareSupplierResult(12, () -> 77);
@SuppressWarnings("unchecked") final ArgumentCaptor<Iterable<Tag>> tagCaptor = ArgumentCaptor.forClass(Iterable.class);
verify(meterRegistry).timer(anyString(), tagCaptor.capture());
final Set<Tag> tags = getTagSet(tagCaptor.getValue());
assertEquals(tags, Set.of(Tag.of(Experiment.OUTCOME_TAG, Experiment.MISMATCH_OUTCOME)));
verify(timer).record(anyLong(), eq(TimeUnit.NANOSECONDS));
}
@Test
public void compareSupplierResultError() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareSupplierResult(12, () -> { throw new RuntimeException("OH NO"); });
@SuppressWarnings("unchecked") final ArgumentCaptor<Iterable<Tag>> tagCaptor = ArgumentCaptor.forClass(Iterable.class);
verify(meterRegistry).timer(anyString(), tagCaptor.capture());
final Set<Tag> tags = getTagSet(tagCaptor.getValue());
assertEquals(tags, Set.of(Tag.of(Experiment.OUTCOME_TAG, Experiment.ERROR_OUTCOME), Tag.of(Experiment.CAUSE_TAG, "RuntimeException")));
verify(timer).record(anyLong(), eq(TimeUnit.NANOSECONDS));
}
@Test
public void compareSupplierResultNoExperimentData() {
final Timer timer = mock(Timer.class);
when(meterRegistry.timer(anyString(), ArgumentMatchers.<Iterable<Tag>>any())).thenReturn(timer);
new Experiment(meterRegistry, "test").compareSupplierResult(12, () -> null);
verify(timer, never()).record(anyLong(), eq(TimeUnit.NANOSECONDS));
}