Skip to content

Commit

Permalink
additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brharrington committed Oct 25, 2023
1 parent ad6ec53 commit dbc1c44
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 11 deletions.
10 changes: 5 additions & 5 deletions spectator-api/src/main/java/com/netflix/spectator/api/Timer.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ default void recordRunnable(Runnable f) {
}

/**
* Executes the callable `f` and records the time taken.
* Executes the supplier `f` and records the time taken.
*
* @param f
* Function to execute and measure the execution time.
Expand All @@ -192,7 +192,7 @@ default <T> T recordSupplier(Supplier<T> f) {
}

/**
* Executes the callable `f` and records the time taken.
* Executes the supplier `f` and records the time taken.
*
* @param f
* Function to execute and measure the execution time.
Expand All @@ -211,7 +211,7 @@ default boolean recordBooleanSupplier(BooleanSupplier f) {
}

/**
* Executes the callable `f` and records the time taken.
* Executes the supplier `f` and records the time taken.
*
* @param f
* Function to execute and measure the execution time.
Expand All @@ -230,7 +230,7 @@ default double recordDoubleSupplier(DoubleSupplier f) {
}

/**
* Executes the callable `f` and records the time taken.
* Executes the supplier `f` and records the time taken.
*
* @param f
* Function to execute and measure the execution time.
Expand All @@ -249,7 +249,7 @@ default int recordIntSupplier(IntSupplier f) {
}

/**
* Executes the callable `f` and records the time taken.
* Executes the supplier `f` and records the time taken.
*
* @param f
* Function to execute and measure the execution time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testRecordZeroBatch() throws Exception {
public void testRecordCallable() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
int v = t.record(() -> {
int v = t.recordCallable(() -> {
clock.setMonotonicTime(500L);
return 42;
});
Expand All @@ -128,7 +128,7 @@ public void testRecordCallableException() throws Exception {
clock.setMonotonicTime(100L);
boolean seen = false;
try {
t.record(() -> {
t.recordCallable(() -> {
clock.setMonotonicTime(500L);
throw new Exception("foo");
});
Expand All @@ -144,18 +144,173 @@ public void testRecordCallableException() throws Exception {
public void testRecordRunnable() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
t.record(() -> clock.setMonotonicTime(500L));
t.recordRunnable(() -> clock.setMonotonicTime(500L));
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordRunnableException() throws Exception {
public void testRecordRunnableException() {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
boolean seen = false;
try {
t.record(() -> {
t.recordRunnable(() -> {
clock.setMonotonicTime(500L);
throw new RuntimeException("foo");
});
} catch (Exception e) {
seen = true;
}
Assertions.assertTrue(seen);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordSupplier() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
String value = t.recordSupplier(() -> {
clock.setMonotonicTime(500L);
return "foo";
});
Assertions.assertEquals(value, "foo");
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordSupplierException() {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
boolean seen = false;
try {
t.recordSupplier(() -> {
clock.setMonotonicTime(500L);
throw new RuntimeException("foo");
});
} catch (Exception e) {
seen = true;
}
Assertions.assertTrue(seen);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordBooleanSupplier() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
boolean value = t.recordBooleanSupplier(() -> {
clock.setMonotonicTime(500L);
return true;
});
Assertions.assertTrue(value);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordBooleanSupplierException() {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
boolean seen = false;
try {
t.recordBooleanSupplier(() -> {
clock.setMonotonicTime(500L);
throw new RuntimeException("foo");
});
} catch (Exception e) {
seen = true;
}
Assertions.assertTrue(seen);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordIntSupplier() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
int value = t.recordIntSupplier(() -> {
clock.setMonotonicTime(500L);
return 42;
});
Assertions.assertEquals(value, 42);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordIntSupplierException() {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
boolean seen = false;
try {
t.recordIntSupplier(() -> {
clock.setMonotonicTime(500L);
throw new RuntimeException("foo");
});
} catch (Exception e) {
seen = true;
}
Assertions.assertTrue(seen);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordLongSupplier() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
long value = t.recordLongSupplier(() -> {
clock.setMonotonicTime(500L);
return 42L;
});
Assertions.assertEquals(value, 42L);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordLongSupplierException() {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
boolean seen = false;
try {
t.recordLongSupplier(() -> {
clock.setMonotonicTime(500L);
throw new RuntimeException("foo");
});
} catch (Exception e) {
seen = true;
}
Assertions.assertTrue(seen);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordDoubleSupplier() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
double value = t.recordDoubleSupplier(() -> {
clock.setMonotonicTime(500L);
return 42.5;
});
Assertions.assertEquals(value, 42.5);
Assertions.assertEquals(t.count(), 1L);
Assertions.assertEquals(t.totalTime(), 400L);
}

@Test
public void testRecordDoubleSupplierException() {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
clock.setMonotonicTime(100L);
boolean seen = false;
try {
t.recordDoubleSupplier(() -> {
clock.setMonotonicTime(500L);
throw new RuntimeException("foo");
});
Expand All @@ -174,7 +329,7 @@ private int square(int v) {
@Test
public void testCallable() throws Exception {
Timer t = new DefaultTimer(clock, NoopId.INSTANCE);
int v2 = t.record(() -> square(42));
int v2 = t.recordCallable(() -> square(42));
}

@Test
Expand Down

0 comments on commit dbc1c44

Please sign in to comment.