@Test public void testWatchRemoval() throws Exception { final String path = jsonPaths[0]; final Change<JsonNode> upsert1 = Change.ofJsonUpsert(path, "1"); final Change<JsonNode> upsert2 = Change.ofJsonUpsert(path, "2"); final Revision rev1 = repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, upsert1).join(); final CompletableFuture<Entry<JsonNode>> f = repo.watch(rev1, Query.ofJson(path)); // Remove the file being watched. repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, Change.ofRemoval(path)).join(); // Should wait patiently until the file reappears. assertThatThrownBy(() -> f.get(1, TimeUnit.SECONDS)).isInstanceOf(TimeoutException.class); // Add the file back again without changing the content. repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, upsert1).join(); // Should wait patiently until the file changes really. assertThatThrownBy(() -> f.get(1, TimeUnit.SECONDS)).isInstanceOf(TimeoutException.class); // Remove the file being watched again. repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, Change.ofRemoval(path)).join(); // Should wait patiently until the file reappears. assertThatThrownBy(() -> f.get(1, TimeUnit.SECONDS)).isInstanceOf(TimeoutException.class); // Add the file back again with different content. final Revision rev2 = repo.commit(HEAD, 0L, Author.UNKNOWN, SUMMARY, upsert2).join(); // Should be notified this time because the content has changed really. final Entry<JsonNode> res = f.get(3, TimeUnit.SECONDS); assertThat(res.revision()).isEqualTo(rev2); assertThat(res.type()).isEqualTo(EntryType.JSON); assertThatJson(res.content()).isEqualTo(upsert2.content()); }
@Test public void operationOnClosedRepository() throws Exception { final CentralDogmaException expectedException = new CentralDogmaException(); final GitRepository repo = new GitRepository(mock(Project.class), new File(repoDir.getRoot(), "close_test_repo"), commonPool(), 0L, Author.SYSTEM); repo.close(() -> expectedException); assertThatThrownBy(() -> repo.find(INIT, "/**").join()).hasCause(expectedException); assertThatThrownBy(() -> repo.history(INIT, HEAD, "/**").join()).hasCause(expectedException); assertThatThrownBy(() -> repo.diff(INIT, HEAD, "/**").join()).hasCause(expectedException); assertThatThrownBy(() -> repo.previewDiff( INIT, Change.ofTextUpsert("/foo.txt", "foo")).join()).hasCause(expectedException); assertThatThrownBy(() -> repo.commit( INIT, 0, Author.SYSTEM, "foo", Change.ofTextUpsert("/foo", "foo")).join()).hasCause(expectedException); assertThatThrownBy(() -> repo.watch( INIT, "/**").get(10, TimeUnit.SECONDS)).hasCause(expectedException); assertThatThrownBy(() -> repo.watch( INIT, Query.ofJson("/foo.json")).get(10, TimeUnit.SECONDS)).hasCause(expectedException); } }
@Test public void json() throws Exception { try (Watcher<JsonNode> watcher = dogma.client().fileWatcher("directory", "my-service", Query.ofJson("/endpoint.json"))) { final CentralDogmaEndpointGroup<JsonNode> endpointGroup = CentralDogmaEndpointGroup.ofWatcher( watcher, EndpointListDecoder.JSON); endpointGroup.awaitInitialEndpoints(); assertThat(endpointGroup.endpoints()).isEqualTo(ENDPOINT_LIST); } }
@Test public void testWatchFileWithIdentityQuery() throws Exception { final Revision rev0 = rule.client() .normalizeRevision(rule.project(), rule.repo1(), Revision.HEAD) .join(); final CompletableFuture<Entry<JsonNode>> future = rule.client().watchFile( rule.project(), rule.repo1(), rev0, Query.ofJson("/test/test1.json"), 3000); assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class); // An irrelevant change should not trigger a notification. final Change<JsonNode> change1 = Change.ofJsonUpsert("/test/test2.json", "[ 3, 2, 1 ]"); final PushResult res1 = rule.client().push( rule.project(), rule.repo1(), rev0, "Add test2.json", change1).join(); final Revision rev1 = res1.revision(); assertThatThrownBy(() -> future.get(500, TimeUnit.MILLISECONDS)).isInstanceOf(TimeoutException.class); // Make a relevant change now. final Change<JsonNode> change2 = Change.ofJsonUpsert("/test/test1.json", "[ -1, -2, -3 ]"); final PushResult res2 = rule.client().push( rule.project(), rule.repo1(), rev1, "Update test1.json", change2).join(); final Revision rev2 = res2.revision(); assertThat(rev2).isEqualTo(rev0.forward(2)); assertThat(future.get(3, TimeUnit.SECONDS)).isEqualTo( Entry.ofJson(rev2, "/test/test1.json", "[-1,-2,-3]")); }
@Test(timeout = 10000) public void testWatchFileGracefulShutdown() throws Exception { testGracefulShutdown(rule.client().watchFile( rule.project(), rule.repo1(), Revision.HEAD, Query.ofJson("/test.json"), 60000)); }