diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /config/src/test/java/com/yahoo/config/subscription/impl/FileConfigSubscriptionTest.java |
Publish
Diffstat (limited to 'config/src/test/java/com/yahoo/config/subscription/impl/FileConfigSubscriptionTest.java')
-rw-r--r-- | config/src/test/java/com/yahoo/config/subscription/impl/FileConfigSubscriptionTest.java | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/config/src/test/java/com/yahoo/config/subscription/impl/FileConfigSubscriptionTest.java b/config/src/test/java/com/yahoo/config/subscription/impl/FileConfigSubscriptionTest.java new file mode 100644 index 00000000000..ce9323f0ec7 --- /dev/null +++ b/config/src/test/java/com/yahoo/config/subscription/impl/FileConfigSubscriptionTest.java @@ -0,0 +1,105 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.subscription.impl; + +import com.yahoo.config.ConfigurationRuntimeException; +import com.yahoo.foo.SimpletypesConfig; +import com.yahoo.foo.TestReferenceConfig; +import com.yahoo.config.subscription.ConfigSubscriber; +import com.yahoo.config.subscription.DirSource; +import com.yahoo.config.subscription.FileSource; +import com.yahoo.vespa.config.ConfigKey; +import com.yahoo.vespa.config.TimingValues; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +/** + * @author lulf + * @since 5.1.7 + */ +public class FileConfigSubscriptionTest { + private File TEST_TYPES_FILE; + + @Before + public void setUp() throws IOException { + TEST_TYPES_FILE = File.createTempFile("fooconfig", ".cfg"); + } + + private void writeConfig(String field, String value) throws IOException { + FileWriter writer = new FileWriter(TEST_TYPES_FILE); + writer.write(field + " " + value); + writer.close(); + } + + @Test + public void require_that_new_config_is_detected_in_time() throws IOException, InterruptedException { + writeConfig("intval", "23"); + ConfigSubscriber subscriber = new ConfigSubscriber(new FileSource(TEST_TYPES_FILE)); + ConfigSubscription<SimpletypesConfig> sub = new FileConfigSubscription<>( + new ConfigKey<>(SimpletypesConfig.class, ""), + subscriber, + TEST_TYPES_FILE); + assertTrue(sub.nextConfig(1000)); + assertThat(sub.config.intval(), is(23)); + Thread.sleep(1000); + writeConfig("intval", "33"); + assertTrue(sub.nextConfig(1000)); + assertThat(sub.config.intval(), is(33)); + } + + @Test + public void require_that_new_config_is_detected_on_reload() throws IOException { + writeConfig("intval", "23"); + ConfigSubscriber subscriber = new ConfigSubscriber(new FileSource(TEST_TYPES_FILE)); + ConfigSubscription<SimpletypesConfig> sub = new FileConfigSubscription<>( + new ConfigKey<>(SimpletypesConfig.class, ""), + subscriber, + TEST_TYPES_FILE); + assertTrue(sub.nextConfig(1000)); + assertThat(sub.config.intval(), is(23)); + writeConfig("intval", "33"); + sub.reload(1); + assertTrue(sub.nextConfig(1000)); + assertThat(sub.config.intval(), is(33)); + assertTrue(sub.isConfigChanged()); + assertTrue(sub.isGenerationChanged()); + sub.reload(2); + assertTrue(sub.nextConfig(1000)); + assertThat(sub.config.intval(), is(33)); + assertFalse(sub.isConfigChanged()); + assertTrue(sub.isGenerationChanged()); + } + + @Test + public void require_that_dir_config_id_reference_is_not_changed() { + final String cfgDir = "src/test/resources/configs/foo"; + final String cfgId = "dir:" + cfgDir; + final ConfigKey<TestReferenceConfig> key = new ConfigKey<>(TestReferenceConfig.class, cfgId); + ConfigSubscriber subscriber = new ConfigSubscriber(); + ConfigSubscription<TestReferenceConfig> sub = ConfigSubscription.get(key, subscriber, new DirSource(new File(cfgDir)), new TimingValues()); + assertTrue(sub.nextConfig(1000)); + assertThat(sub.config.configId(), is(cfgId)); + } + + @Test(expected = ConfigurationRuntimeException.class) + public void require_that_bad_file_throws_exception() throws IOException { + // A little trick to ensure that we can create the subscriber, but that we get an error when reading. + writeConfig("intval", "23"); + ConfigSubscriber subscriber = new ConfigSubscriber(new FileSource(TEST_TYPES_FILE)); + ConfigSubscription<SimpletypesConfig> sub = new FileConfigSubscription<>( + new ConfigKey<>(SimpletypesConfig.class, ""), + subscriber, + TEST_TYPES_FILE); + sub.reload(1); + assertTrue(TEST_TYPES_FILE.setReadable(false)); + sub.nextConfig(0); + } +} |