OffHeapResourcesProvider implements OffHeapResources, ManageableServerComponent, StateDumpable, AutoCloseable { @Override public OffHeapResourceImpl getOffHeapResource(OffHeapResourceIdentifier identifier) { return resources.get(identifier); } OffHeapResourcesProvider(OffheapResourcesType configuration);  OffHeapResourcesProvider(Map<String, Measure<org.terracotta.common.struct.MemoryUnit>> resources); @Override Set<OffHeapResourceIdentifier> getAllIdentifiers(); @Override OffHeapResourceImpl getOffHeapResource(OffHeapResourceIdentifier identifier); @Override boolean addOffHeapResource(OffHeapResourceIdentifier identifier, long capacityInBytes); @Override void onManagementRegistryCreated(EntityManagementRegistry registry); @Override void onManagementRegistryClose(EntityManagementRegistry registry); @Override void addStateTo(StateDumpCollector dump); @Override void close();  }
@SuppressWarnings("unchecked") @Test public void testObserverExposed() { when(resourceConfig.getName()).thenReturn("foo"); when(resourceConfig.getUnit()).thenReturn(MemoryUnit.MB); when(resourceConfig.getValue()).thenReturn(BigInteger.valueOf(2)); when(configuration.getResource()).thenReturn(singletonList(resourceConfig)); OffHeapResourcesProvider provider = new OffHeapResourcesProvider(configuration); OffHeapResource offHeapResource = provider.getOffHeapResource(identifier("foo")); assertThat(offHeapResource.available(), equalTo(2L * 1024 * 1024)); assertThat(StatisticsManager.nodeFor(offHeapResource).getChildren().size(), equalTo(1)); ValueStatistic<Long> valueStatistic = (ValueStatistic<Long>) StatisticsManager.nodeFor(offHeapResource).getChildren().iterator().next().getContext().attributes().get("this"); assertThat(valueStatistic.value(), equalTo(0L)); }
@Test public void testInitializeWithValidConfig() { when(resourceConfig.getName()).thenReturn("foo"); when(resourceConfig.getUnit()).thenReturn(MemoryUnit.MB); when(resourceConfig.getValue()).thenReturn(BigInteger.valueOf(2)); when(configuration.getResource()).thenReturn(singletonList(resourceConfig)); OffHeapResourcesProvider provider = new OffHeapResourcesProvider(configuration); assertThat(provider.getOffHeapResource(identifier("foo")), notNullValue()); assertThat(provider.getOffHeapResource(identifier("foo")).available(), is(2L * 1024 * 1024)); }
@Test public void testNullReturnOnInvalidResource() { when(resourceConfig.getName()).thenReturn("foo"); when(resourceConfig.getUnit()).thenReturn(MemoryUnit.MB); when(resourceConfig.getValue()).thenReturn(BigInteger.valueOf(2)); when(configuration.getResource()).thenReturn(singletonList(resourceConfig)); OffHeapResourcesProvider provider = new OffHeapResourcesProvider(configuration); assertThat(provider.getOffHeapResource(identifier("bar")), nullValue()); }