FunctionStatsManager { public FunctionStats getFunctionStatsByName(String name) { MeterRegistry meterRegistryFromSupplier = meterRegistrySupplier.get(); if (statsDisabled && meterRegistryFromSupplier == null) { return dummyFunctionStats; } return computeIfAbsent(functionExecutionStatsMap, name, key -> create(key, meterRegistryFromSupplier)); } FunctionStatsManager(boolean statsDisabled, StatisticsFactory statisticsFactory,
      Supplier<MeterRegistry> meterRegistrySupplier); @VisibleForTesting  FunctionStatsManager(boolean statsDisabled, StatisticsFactory statisticsFactory,
      FunctionServiceStats functionServiceStats, Supplier<MeterRegistry> meterRegistrySupplier); FunctionServiceStats getFunctionServiceStats(); FunctionStats getFunctionStatsByName(String name); void close(); static FunctionStats getFunctionStats(String functionID, InternalDistributedSystem ds); static FunctionStats getFunctionStats(String functionID);  }
@Test public void getFunctionStatsByName_usesStatisticsFromFactory_ifStatsEnabled_andMeterRegistrySupplied() { FunctionStatsManager functionStatsManager = new FunctionStatsManager(false, statisticsFactory, () -> meterRegistry); Statistics statisticsFromFactory = mock(Statistics.class); when(statisticsFactory.createAtomicStatistics(any(), any())) .thenReturn(statisticsFromFactory); FunctionStats functionStats = functionStatsManager.getFunctionStatsByName("foo"); assertThat(functionStats.getStatistics()) .isSameAs(statisticsFromFactory); }
@Test public void getFunctionStatsByName_usesStatisticsFromFactory_ifStatsEnabled_andMeterRegistryIsNull() { FunctionStatsManager functionStatsManager = new FunctionStatsManager(false, statisticsFactory, () -> null); Statistics statisticsFromFactory = mock(Statistics.class); when(statisticsFactory.createAtomicStatistics(any(), any())) .thenReturn(statisticsFromFactory); FunctionStats functionStats = functionStatsManager.getFunctionStatsByName("foo"); assertThat(functionStats.getStatistics()) .isSameAs(statisticsFromFactory); }
@Test public void getFunctionStatsByName_usesMeterRegistryFromSupplier_ifStatsDisabled_andMeterRegistrySupplied() { MeterRegistry meterRegistryFromSupplier = new SimpleMeterRegistry(); FunctionStatsManager functionStatsManager = new FunctionStatsManager(true, statisticsFactory, () -> meterRegistryFromSupplier); FunctionStats functionStats = functionStatsManager.getFunctionStatsByName("foo"); assertThat(functionStats.getMeterRegistry()) .isSameAs(meterRegistryFromSupplier); }
@Test public void getFunctionStatsByName_usesMeterRegistryFromSupplier_ifStatsEnabled_andMeterRegistrySupplied() { MeterRegistry meterRegistryFromSupplier = new SimpleMeterRegistry(); FunctionStatsManager functionStatsManager = new FunctionStatsManager(false, statisticsFactory, () -> meterRegistryFromSupplier); FunctionStats functionStats = functionStatsManager.getFunctionStatsByName("foo"); assertThat(functionStats.getMeterRegistry()) .isSameAs(meterRegistryFromSupplier); }
@Test public void getFunctionStatsByName_returnsSameInstanceForGivenName() { FunctionStatsManager functionStatsManager = new FunctionStatsManager(false, statisticsFactory, () -> meterRegistry); FunctionStats first = functionStatsManager.getFunctionStatsByName("foo"); FunctionStats second = functionStatsManager.getFunctionStatsByName("foo"); assertThat(second) .isSameAs(first); }