private void sampleCallbacks() { if (cancelCriterion.isCancelInProgress()) { executor.shutdown(); } int errors = 0; int suppliers = 0; long start = System.nanoTime(); try { for (Statistics stats : statisticsManager.getStatsList()) { StatisticsImpl statistics = (StatisticsImpl) stats; errors += statistics.invokeSuppliers(); suppliers += statistics.getSupplierCount(); } } catch (VirtualMachineError e) { SystemFailure.initiateFailure(e); } catch (Throwable throwable) { logger.error("Error invoking statistic suppliers", throwable); } finally { long end = System.nanoTime(); statSamplerStats.incSampleCallbackDuration(TimeUnit.NANOSECONDS.toMillis(end - start)); statSamplerStats.incSampleCallbackErrors(errors); statSamplerStats.setSampleCallbacks(suppliers); } }
@Test public void invokeSuppliersShouldLogErrorOnlyOnce() { Logger logger = mock(Logger.class); StatisticsImpl.logger = logger; IntSupplier supplier1 = mock(IntSupplier.class); when(supplier1.getAsInt()).thenThrow(NullPointerException.class); stats.setIntSupplier(4, supplier1); assertEquals(1, stats.invokeSuppliers()); // String message, Object p0, Object p1, Object p2 verify(logger, times(1)).warn(anyString(), isNull(), anyInt(), isA(NullPointerException.class)); assertEquals(1, stats.invokeSuppliers()); // Make sure the logger isn't invoked again verify(logger, times(1)).warn(anyString(), isNull(), anyInt(), isA(NullPointerException.class)); }
@Test public void taskShouldSampleStatistics() { Runnable sampleTask = invokeStartAndGetTask(); StatisticsImpl stats1 = mock(StatisticsImpl.class); StatisticsImpl stats2 = mock(StatisticsImpl.class); when(stats1.invokeSuppliers()).thenReturn(3); when(stats2.invokeSuppliers()).thenReturn(2); when(stats1.getSupplierCount()).thenReturn(7); when(stats2.getSupplierCount()).thenReturn(8); when(statisticsManager.getStatsList()).thenReturn(Arrays.asList(stats1, stats2)); sampleTask.run(); verify(statSamplerStats).setSampleCallbacks(eq(15)); verify(statSamplerStats).incSampleCallbackErrors(5); verify(statSamplerStats).incSampleCallbackDuration(anyLong()); }
@Test public void invokeSuppliersShouldCatchSupplierErrorsAndReturnCount() { IntSupplier supplier1 = mock(IntSupplier.class); when(supplier1.getAsInt()).thenThrow(NullPointerException.class); stats.setIntSupplier(4, supplier1); assertEquals(1, stats.invokeSuppliers()); verify(supplier1).getAsInt(); }
@Test public void invokeIntSuppliersShouldUpdateStats() { IntSupplier supplier1 = mock(IntSupplier.class); when(supplier1.getAsInt()).thenReturn(23); stats.setIntSupplier(4, supplier1); assertEquals(0, stats.invokeSuppliers()); verify(supplier1).getAsInt(); assertEquals(23, stats.getInt(4)); }
@Test public void invokeLongSuppliersShouldUpdateStats() { LongSupplier supplier1 = mock(LongSupplier.class); when(supplier1.getAsLong()).thenReturn(23L); stats.setLongSupplier(4, supplier1); assertEquals(0, stats.invokeSuppliers()); verify(supplier1).getAsLong(); assertEquals(23L, stats.getLong(4)); }
@Test public void invokeDoubleSuppliersShouldUpdateStats() { DoubleSupplier supplier1 = mock(DoubleSupplier.class); when(supplier1.getAsDouble()).thenReturn(23.3); stats.setDoubleSupplier(4, supplier1); assertEquals(0, stats.invokeSuppliers()); verify(supplier1).getAsDouble(); assertEquals(23.3, stats.getDouble(4), 0.1f); }