diff --git a/archaius-core/src/main/java/com/netflix/config/ConcurrentCompositeConfiguration.java b/archaius-core/src/main/java/com/netflix/config/ConcurrentCompositeConfiguration.java index bb97d2ac..a3e4402b 100644 --- a/archaius-core/src/main/java/com/netflix/config/ConcurrentCompositeConfiguration.java +++ b/archaius-core/src/main/java/com/netflix/config/ConcurrentCompositeConfiguration.java @@ -115,9 +115,16 @@ public class ConcurrentCompositeConfiguration extends ConcurrentMapConfiguration private final Set usedProperties = ConcurrentHashMap.newKeySet(); public Set getUsedProperties() { - return usedProperties; + return Collections.unmodifiableSet(new HashSet<>(usedProperties)); } + public Set getAndClearUsedProperties() { + synchronized (usedProperties) { + Set ret = getUsedProperties(); + usedProperties.clear(); + return ret; + } + } private List configList = new CopyOnWriteArrayList(); diff --git a/archaius-core/src/test/java/com/netflix/config/ConcurrentCompositeConfigurationTest.java b/archaius-core/src/test/java/com/netflix/config/ConcurrentCompositeConfigurationTest.java index 73416fe7..493d09c5 100644 --- a/archaius-core/src/test/java/com/netflix/config/ConcurrentCompositeConfigurationTest.java +++ b/archaius-core/src/test/java/com/netflix/config/ConcurrentCompositeConfigurationTest.java @@ -18,6 +18,7 @@ import static org.junit.Assert.*; import java.util.List; +import java.util.Set; import org.apache.commons.configuration.AbstractConfiguration; import org.apache.commons.configuration.BaseConfiguration; @@ -80,11 +81,24 @@ public void testInstrumentation() { config.addProperty("prop1", "val1"); config.addProperty("prop2", "val2"); assertEquals(config.getProperty("prop1"), "val1"); - assertEquals(config.getUsedProperties().size(), 1); - assertEquals(config.getUsedProperties().iterator().next(), "prop1"); + + // Confirm that the usage is captured + Set usedProperties = config.getUsedProperties(); + assertEquals(usedProperties.size(), 1); + assertEquals(usedProperties.iterator().next(), "prop1"); + + // Confirm that an uninstrumented call is ignored assertEquals(config.getPropertyUninstrumented("prop2"), "val2"); - assertEquals(config.getUsedProperties().size(), 1); - assertEquals(config.getUsedProperties().iterator().next(), "prop1"); + usedProperties = config.getAndClearUsedProperties(); + assertEquals(usedProperties.size(), 1); + assertEquals(usedProperties.iterator().next(), "prop1"); + + // Confirm that both usedProperties endpoints respect when the properties have been cleared + usedProperties = config.getUsedProperties(); + assertTrue(usedProperties.isEmpty()); + + usedProperties = config.getAndClearUsedProperties(); + assertTrue(usedProperties.isEmpty()); System.clearProperty(ConcurrentCompositeConfiguration.ENABLE_INSTRUMENTATION); }