Search in sources :

Example 1 with DynamicConfig

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();
}
Also used : PrintWriter(java.io.PrintWriter) DynamicConfigDouble(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigDouble) DynamicConfigFloat(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigFloat) DynamicConfigDuration(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigDuration) DynamicConfigString(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigString) DynamicConfig(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfig) DynamicConfigCsv(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigCsv) Test(org.testng.annotations.Test) IOException(java.io.IOException) ConfigProviderFile(com.yahoo.athenz.common.server.util.config.providers.ConfigProviderFile) DynamicConfigInteger(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigInteger) Function(java.util.function.Function) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) TimeUnit(java.util.concurrent.TimeUnit) DynamicConfigLong(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigLong) Assert(org.testng.Assert) DynamicConfigBoolean(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigBoolean) Nonnull(javax.annotation.Nonnull) Utils(com.yahoo.athenz.common.server.util.Utils) Nullable(javax.annotation.Nullable) Nonnull(javax.annotation.Nonnull) ConfigProviderFile(com.yahoo.athenz.common.server.util.config.providers.ConfigProviderFile) DynamicConfig(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfig) DynamicConfigString(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigString) ConfigProviderFile(com.yahoo.athenz.common.server.util.config.providers.ConfigProviderFile) File(java.io.File) Nullable(javax.annotation.Nullable) Test(org.testng.annotations.Test)

Aggregations

Utils (com.yahoo.athenz.common.server.util.Utils)1 DynamicConfig (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfig)1 DynamicConfigBoolean (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigBoolean)1 DynamicConfigCsv (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigCsv)1 DynamicConfigDouble (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigDouble)1 DynamicConfigDuration (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigDuration)1 DynamicConfigFloat (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigFloat)1 DynamicConfigInteger (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigInteger)1 DynamicConfigLong (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigLong)1 DynamicConfigString (com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigString)1 ConfigProviderFile (com.yahoo.athenz.common.server.util.config.providers.ConfigProviderFile)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 Assert (org.testng.Assert)1