use of com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfig in project athenz by yahoo.
the class DynamicConfigTest method testDynamic.
@Test
public void testDynamic() throws IOException, InterruptedException {
File configFile = File.createTempFile("ConfigProviderFileTest.testDynamic", ".conf");
writeFile(configFile, "" + "string-key: value-1\n" + "duration-key-reload: 50\n");
// Our config-manager translates all config-values to upper-case.
try (ConfigManager configManager = new ConfigManager("duration-key-reload", 10000000000L, TimeUnit.MILLISECONDS) {
@Override
protected String translateConfigValue(@Nonnull String configKey, @Nullable String configValue) {
return (configValue == null) ? null : configValue.toUpperCase();
}
}.addProvider(new ConfigProviderFile()).addConfigSource("prop-file://" + configFile)) {
DynamicConfigTester dynamicConfig = new DynamicConfigTester(configManager, "string-key", "default-value");
// Add a change-callback that always throw.
// This shouldn't prevent the next change-callback to be called.
DynamicConfig.ChangeCallback<String> throwingChangeCallback = (newValue, oldValue, _dynamicConfig) -> {
throw new RuntimeException("DynamicConfigTest.testDynamic.throwingChangeCallback");
};
dynamicConfig.registerChangeCallback(throwingChangeCallback);
// Add a "normal" change-callback.
DynamicConfig.ChangeCallback<String> normalChangeCallback = (newValue, oldValue, _dynamicConfig) -> {
assertEquals(dynamicConfig, _dynamicConfig);
assertEquals("VALUE-1", oldValue);
assertEquals("VALUE-2", newValue);
dynamicChangedCount++;
};
dynamicConfig.registerChangeCallback(normalChangeCallback);
assertEquals("VALUE-1", dynamicConfig.get());
// Make a config-change - that cause DynamicConfigTester to throw.
writeFile(configFile, "" + "string-key: THROWING_VALUE\n" + "duration-key-reload: 10\n");
long startTime = System.currentTimeMillis();
while (!dynamicConfig.didThrow) {
if ((System.currentTimeMillis() - startTime) > 2000) {
fail("No refresh for THROWING_VALUE");
}
Thread.sleep(1);
}
// Make a proper config-change.
writeFile(configFile, "" + "string-key: value-2\n" + "duration-key-reload: 10\n");
startTime = System.currentTimeMillis();
while (!dynamicConfig.get().equals("VALUE-2")) {
if ((System.currentTimeMillis() - startTime) > 2000) {
fail("No refresh for VALUE-2");
}
Thread.sleep(1);
}
// No more updates
configManager.close();
writeFile(configFile, "" + "string-key: value-3\n" + "duration-key-reload: 10\n");
Thread.sleep(500);
assertEquals("VALUE-2", dynamicConfig.get());
assertTrue(dynamicConfig.unregisterChangeCallback(normalChangeCallback));
assertFalse(dynamicConfig.unregisterChangeCallback(normalChangeCallback));
assertEquals(1, dynamicChangedCount);
}
@SuppressWarnings("unused") boolean deleted = configFile.delete();
}
Aggregations