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));
}
Aggregations