use of net.morimekta.util.FileWatcher in project providence by morimekta.
the class ProvidenceConfigTest method testReusableObject.
@Test
public void testReusableObject() throws ProvidenceConfigException {
FileWatcher watcher = new FileWatcher();
ProvidenceConfig lazy = new ProvidenceConfig(registry, watcher, false);
ProvidenceConfig strict = new ProvidenceConfig(registry, watcher, true);
File file = copyResourceTo("/net/morimekta/providence/config/files/reusable_object.cfg", temp.getRoot());
Service service = lazy.getConfig(file);
assertThat(service.getAdmin().getOauthTokenKey(), is(service.getHttp().getSignatureKeys().get("app1")));
try {
strict.getConfig(file);
fail("no exception");
} catch (ProvidenceConfigException e) {
assertThat(e.getMessage(), is("Reusable objects are not allowed in strict mode."));
assertThat(e.getFile(), is(file.getName()));
assertThat(e.asString(), is("Error in reusable_object.cfg on line 9, pos 27: Reusable objects are not allowed in strict mode.\n" + " oauth_token_key & token = b64(VGVzdCBPYXV0aCBLZXkK)\n" + "--------------------------^^^^^"));
}
}
use of net.morimekta.util.FileWatcher in project providence by morimekta.
the class ProvidenceConfigTest method testReload.
@Test
public void testReload() throws IOException {
copyResourceTo("/net/morimekta/providence/config/files/base_service.cfg", temp.getRoot());
File stageDb = copyResourceTo("/net/morimekta/providence/config/files/stage_db.cfg", temp.getRoot());
File stage = copyResourceTo("/net/morimekta/providence/config/files/stage.cfg", temp.getRoot());
FileWatcher watcher = new FileWatcher();
ProvidenceConfig config = new ProvidenceConfig(registry, watcher, true);
Supplier<Service> stage_service = config.resolveConfig(stage);
assertEquals("{\n" + " name = \"stage\"\n" + " http = {\n" + " port = 8080\n" + " context = \"/app\"\n" + " signature_keys = {\n" + " \"app1\": b64(VGVzdCBPYXV0aCBLZXkK)\n" + " }\n" + " signature_override_keys = [\n" + " \"not_really_app_1\"\n" + " ]\n" + " }\n" + " db = {\n" + " uri = \"jdbc:h2:localhost:mem\"\n" + " driver = \"org.h2.Driver\"\n" + " credentials = {\n" + " username = \"myuser\"\n" + " password = \"MyP4s5w0rd\"\n" + " }\n" + " }\n" + "}", debugString(stage_service.get()));
AtomicBoolean watcherCalled = new AtomicBoolean(false);
watcher.addWatcher(file -> watcherCalled.set(true));
File tmp = temp.newFile();
writeContentTo(getResourceAsString("/net/morimekta/providence/config/files/stage_db2.cfg"), tmp);
Files.move(tmp.toPath(), stageDb.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
if (FileSystems.getDefault().newWatchService().getClass().getName().equals("sun.nio.fs.PollingWatchService")) {
// HACK: PollingWatchService uses file modification time to trigger events. Artificially advancing the
// modification time seems to successfully trigger events in this case.
stageDb.setLastModified(System.currentTimeMillis() + 120 * 1000);
}
await().atMost(Duration.TEN_SECONDS).untilTrue(watcherCalled);
assertThat(config.resolveConfig(stage), is(sameInstance(stage_service)));
assertThat(stage_service.get(), is(sameInstance(stage_service.get())));
assertEquals("{\n" + " name = \"stage\"\n" + " http = {\n" + " port = 8080\n" + " context = \"/app\"\n" + " signature_keys = {\n" + " \"app1\": b64(VGVzdCBPYXV0aCBLZXkK)\n" + " }\n" + " signature_override_keys = [\n" + " \"not_really_app_1\"\n" + " ]\n" + " }\n" + " db = {\n" + " uri = \"jdbc:h2:localhost:mem\"\n" + " driver = \"org.h2.Driver\"\n" + " credentials = {\n" + " username = \"myuser\"\n" + " password = \"O7h3rP4ssw0rd\"\n" + " }\n" + " }\n" + "}", debugString(stage_service.get()));
}
use of net.morimekta.util.FileWatcher 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));
}
use of net.morimekta.util.FileWatcher in project providence by morimekta.
the class ProvidenceConfigTest method setUp.
@Before
public void setUp() throws IOException {
registry = new SimpleTypeRegistry();
registry.registerRecursively(Service.kDescriptor);
registry.registerRecursively(Value.kDescriptor);
registry.registerRecursively(RefMerge.kDescriptor);
watcher = new FileWatcher();
}
Aggregations