Search in sources :

Example 1 with ConfigListener

use of net.morimekta.providence.config.ConfigListener in project providence by morimekta.

the class ProvidenceConfigSupplierTest method testReferencedFilesOnUpdatesFromCanonicalFiles.

@Test
public void testReferencedFilesOnUpdatesFromCanonicalFiles() throws IOException {
    SimpleTypeRegistry registry = new SimpleTypeRegistry();
    registry.registerRecursively(Service.kDescriptor);
    watcher = new FileWatcher();
    parser = new ProvidenceConfigParser(registry, true);
    File service = new File(tmp.getRoot(), "service.config").getAbsoluteFile();
    File include = new File(tmp.getRoot(), "include.config").getAbsoluteFile();
    File v1 = tmp.newFolder("..dist", "v1").getAbsoluteFile();
    File service1 = new File(v1, "service.config").getAbsoluteFile();
    File include1 = new File(v1, "include.config").getAbsoluteFile();
    File v2 = tmp.newFolder("..dist", "v2").getAbsoluteFile();
    File service2 = new File(v2, "service.config").getAbsoluteFile();
    File include2 = new File(v2, "include.config").getAbsoluteFile();
    // write v1 config
    writeContentTo("config.ServicePort {\n" + "  port = 8080\n" + "}\n", include1);
    writeContentTo("include \"include.config\" as inc\n" + "config.Service {\n" + "  name = \"test\"\n" + "  http = inc\n" + "}\n", service1);
    // write v2 config
    writeContentTo("config.ServicePort {\n" + "  port = 80\n" + "}\n", include2);
    writeContentTo("include \"include.config\" as inc\n" + "config.Service {\n" + "  name = \"other\"\n" + "  http = inc\n" + "}\n", service2);
    Files.createSymbolicLink(service.toPath(), service1.toPath());
    Files.createSymbolicLink(include.toPath(), include1.toPath());
    ProvidenceConfigSupplier<Service, Service._Field> config = new ProvidenceConfigSupplier<>(service, null, watcher, parser, clock);
    Service serviceConfig = config.get();
    AtomicReference<Service> serviceRef = new AtomicReference<>(serviceConfig);
    assertThat(serviceConfig.getName(), is("test"));
    assertThat(serviceConfig.getHttp().getPort(), is((short) 8080));
    AtomicBoolean updated = new AtomicBoolean();
    ConfigListener<Service, Service._Field> listener = cf -> {
        serviceRef.set(cf);
        updated.set(true);
    };
    config.addListener(listener);
    File tmpFile = new File(tmp.getRoot(), "..tmp");
    Files.createSymbolicLink(tmpFile.toPath(), service2.toPath());
    Files.move(tmpFile.toPath(), service.toPath(), StandardCopyOption.REPLACE_EXISTING);
    waitAtMost(Duration.ONE_SECOND).untilTrue(updated);
    serviceConfig = serviceRef.get();
    assertThat(serviceConfig.getName(), is("other"));
    assertThat(serviceConfig.getHttp().getPort(), is((short) 80));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeClock(net.morimekta.testing.time.FakeClock) AtomicReference(java.util.concurrent.atomic.AtomicReference) Awaitility.waitAtMost(org.awaitility.Awaitility.waitAtMost) StandardCopyOption(java.nio.file.StandardCopyOption) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) Assert.assertThat(org.junit.Assert.assertThat) Pair(net.morimekta.util.Pair) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Mockito.atLeast(org.mockito.Mockito.atLeast) SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) Before(org.junit.Before) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) ImmutableSet(com.google.common.collect.ImmutableSet) ProvidenceConfigException(net.morimekta.providence.config.ProvidenceConfigException) Duration(org.awaitility.Duration) Files(java.nio.file.Files) TestConfigSupplier(net.morimekta.providence.config.util.TestConfigSupplier) FileWatcher(net.morimekta.util.FileWatcher) Test(org.junit.Test) IOException(java.io.IOException) Mockito.doNothing(org.mockito.Mockito.doNothing) Mockito.when(org.mockito.Mockito.when) ConfigListener(net.morimekta.providence.config.ConfigListener) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) Rule(org.junit.Rule) Mockito.atMost(org.mockito.Mockito.atMost) ResourceUtils.writeContentTo(net.morimekta.testing.ResourceUtils.writeContentTo) Database(net.morimekta.test.providence.config.Database) Mockito.reset(org.mockito.Mockito.reset) TemporaryFolder(org.junit.rules.TemporaryFolder) Service(net.morimekta.test.providence.config.Service) Mockito.mock(org.mockito.Mockito.mock) SimpleTypeRegistry(net.morimekta.providence.util.SimpleTypeRegistry) Service(net.morimekta.test.providence.config.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FileWatcher(net.morimekta.util.FileWatcher) File(java.io.File) Test(org.junit.Test)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 File (java.io.File)1 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ConfigListener (net.morimekta.providence.config.ConfigListener)1 ProvidenceConfigException (net.morimekta.providence.config.ProvidenceConfigException)1 TestConfigSupplier (net.morimekta.providence.config.util.TestConfigSupplier)1 SimpleTypeRegistry (net.morimekta.providence.util.SimpleTypeRegistry)1 Database (net.morimekta.test.providence.config.Database)1 Service (net.morimekta.test.providence.config.Service)1 ResourceUtils.writeContentTo (net.morimekta.testing.ResourceUtils.writeContentTo)1 FakeClock (net.morimekta.testing.time.FakeClock)1 FileWatcher (net.morimekta.util.FileWatcher)1 Pair (net.morimekta.util.Pair)1 Awaitility.waitAtMost (org.awaitility.Awaitility.waitAtMost)1 Duration (org.awaitility.Duration)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1