From dbc1c44694dae7f24c438b24784b825c7b4783c8 Mon Sep 17 00:00:00 2001 From: Brian Harrington Date: Wed, 25 Oct 2023 12:28:34 -0500 Subject: [PATCH] additional tests --- .../java/com/netflix/spectator/api/Timer.java | 10 +- .../spectator/api/DefaultTimerTest.java | 167 +++++++++++++++++- 2 files changed, 166 insertions(+), 11 deletions(-) diff --git a/spectator-api/src/main/java/com/netflix/spectator/api/Timer.java b/spectator-api/src/main/java/com/netflix/spectator/api/Timer.java index 1a5f7fdb8..31f4bb741 100644 --- a/spectator-api/src/main/java/com/netflix/spectator/api/Timer.java +++ b/spectator-api/src/main/java/com/netflix/spectator/api/Timer.java @@ -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. @@ -192,7 +192,7 @@ default T recordSupplier(Supplier 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. @@ -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. @@ -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. @@ -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. diff --git a/spectator-api/src/test/java/com/netflix/spectator/api/DefaultTimerTest.java b/spectator-api/src/test/java/com/netflix/spectator/api/DefaultTimerTest.java index fde213a3c..9f94b5feb 100644 --- a/spectator-api/src/test/java/com/netflix/spectator/api/DefaultTimerTest.java +++ b/spectator-api/src/test/java/com/netflix/spectator/api/DefaultTimerTest.java @@ -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; }); @@ -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"); }); @@ -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"); }); @@ -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