aboutsummaryrefslogtreecommitdiffstats
path: root/config/src/test/java/com/yahoo/config/subscription/impl/FileConfigSubscriptionTest.java
blob: 74af35e39dc058847e408eb630f0b1b77c8cdd55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright Yahoo. 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.subscription.DirSource;
import com.yahoo.foo.SimpletypesConfig;
import com.yahoo.foo.TestReferenceConfig;
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 java.nio.file.Files;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * @author Ulf Lilleengen
 */
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");
        ConfigSubscription<SimpletypesConfig> sub = new FileConfigSubscription<>(
                new ConfigKey<>(SimpletypesConfig.class, ""),
                TEST_TYPES_FILE);
        assertTrue(sub.nextConfig(1000));
        assertThat(sub.getConfigState().getConfig().intval(), is(23));
        Thread.sleep(1000);
        writeConfig("intval", "33");
        assertTrue(sub.nextConfig(1000));
        assertThat(sub.getConfigState().getConfig().intval(), is(33));
    }

    @Test
    public void require_that_new_config_is_detected_on_reload() throws IOException {
        writeConfig("intval", "23");
        ConfigSubscription<SimpletypesConfig> sub = new FileConfigSubscription<>(
                new ConfigKey<>(SimpletypesConfig.class, ""),
                TEST_TYPES_FILE);
        assertTrue(sub.nextConfig(1000));
        assertThat(sub.getConfigState().getConfig().intval(), is(23));
        writeConfig("intval", "33");
        sub.reload(1);
        assertTrue(sub.nextConfig(1000));
        ConfigSubscription.ConfigState<SimpletypesConfig> configState = sub.getConfigState();
        assertThat(configState.getConfig().intval(), is(33));
        assertTrue(configState.isConfigChanged());
        assertTrue(configState.isGenerationChanged());

        assertTrue(sub.isConfigChangedAndReset(7L));
        assertSame(configState, sub.getConfigState());
        assertTrue(configState.isConfigChanged());
        assertTrue(configState.isGenerationChanged());
        assertTrue(sub.isConfigChangedAndReset(1L));
        assertNotSame(configState, sub.getConfigState());
        configState = sub.getConfigState();
        assertFalse(configState.isConfigChanged());
        assertFalse(configState.isGenerationChanged());

        sub.reload(2);
        assertTrue(sub.nextConfig(1000));
        configState = sub.getConfigState();
        assertThat(configState.getConfig().intval(), is(33));
        assertFalse(configState.isConfigChanged());
        assertTrue(configState.isGenerationChanged());

        assertFalse(sub.isConfigChangedAndReset(2L));
        assertNotSame(configState, sub.getConfigState());
        configState = sub.getConfigState();
        assertFalse(configState.isConfigChanged());
        assertFalse(configState.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);
        ConfigSubscription<TestReferenceConfig> sub = ConfigSubscription.get(key,
                                                                             new JrtConfigRequesters(),
                                                                             new DirSource(new File(cfgDir)),
                                                                             new TimingValues());
        assertTrue(sub.nextConfig(1000));
        assertThat(sub.getConfigState().getConfig().configId(), is(cfgId));
    }

    @Test(expected = IllegalArgumentException.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");
        ConfigSubscription<SimpletypesConfig> sub = new FileConfigSubscription<>(
                new ConfigKey<>(SimpletypesConfig.class, ""),
                TEST_TYPES_FILE);
        sub.reload(1);
        Files.delete(TEST_TYPES_FILE.toPath()); // delete file so the below statement throws exception
        sub.nextConfig(0);
    }
}