From 688a502e41c801f70b064db2b8158fd13517c1f3 Mon Sep 17 00:00:00 2001 From: Lucas Wang Date: Mon, 18 Mar 2019 14:46:31 -0700 Subject: [PATCH 1/2] WIP: added a test for best effort queries --- src/test/java/io/dgraph/AclTest.java | 5 ++- .../java/io/dgraph/DgraphAsyncClientTest.java | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/test/java/io/dgraph/AclTest.java b/src/test/java/io/dgraph/AclTest.java index a053588..1f799b8 100644 --- a/src/test/java/io/dgraph/AclTest.java +++ b/src/test/java/io/dgraph/AclTest.java @@ -179,7 +179,10 @@ private void verifyOperation(boolean shouldFail, String operation, Runnable runn return; } - assertFalse(shouldFail, "the " + operation + " should have failed"); + // assertFalse(shouldFail, ); + if (shouldFail != false) { + throw new RuntimeException("the " + operation + " should have failed"); + } } private void resetUser() throws Exception { diff --git a/src/test/java/io/dgraph/DgraphAsyncClientTest.java b/src/test/java/io/dgraph/DgraphAsyncClientTest.java index f491fa8..a48ed77 100644 --- a/src/test/java/io/dgraph/DgraphAsyncClientTest.java +++ b/src/test/java/io/dgraph/DgraphAsyncClientTest.java @@ -197,4 +197,39 @@ public void testCheckVersion() { assertTrue(v.getTag().length() > 0); // assertEquals(v.getTag().charAt(0), 'v'); } + + @Test + public void testBestEffortQueries() throws InterruptedException { + Operation op = Operation.newBuilder().setSchema("name: string @index(exact) .").build(); + dgraphAsyncClient.alter(op).join(); + + // Add data + JsonObject json = new JsonObject(); + json.addProperty("name", "Alice"); + + Mutation mu = + Mutation.newBuilder() + // .setCommitNow(true) + .setSetJson(ByteString.copyFromUtf8(json.toString())) + .build(); // this mutation won't be committed + Response mutationResp = dgraphAsyncClient.newTransaction().mutate(mu).join(); + System.out.println("mutation response:" + mutationResp); + + // sleep for 5 seconds for the max assigned ts to be propagated + Thread.sleep(5000); + + // create a new transaction to verify the best effort read + AsyncTransaction readTxn = dgraphAsyncClient.newReadOnlyTransaction(); + readTxn.setBestEffort(true); + String query = "query me($a: string) { me(func: eq(name, $a)) { name }}"; + Map vars = Collections.singletonMap("$a", "Alice"); + Response res = readTxn.queryWithVars(query, vars).join(); + + // Verify data as expected + JsonParser parser = new JsonParser(); + json = parser.parse(res.getJson().toStringUtf8()).getAsJsonObject(); + assertTrue(json.has("me")); + String name = json.getAsJsonArray("me").get(0).getAsJsonObject().get("name").getAsString(); + assertEquals(name, "Alice"); + } } From 8068d2b53f0bb51e4339ba65b9df29323fe90de7 Mon Sep 17 00:00:00 2001 From: shivaji-dgraph Date: Thu, 11 Jul 2024 12:06:09 +0530 Subject: [PATCH 2/2] fix failing test --- src/test/java/io/dgraph/DgraphAsyncClientTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/dgraph/DgraphAsyncClientTest.java b/src/test/java/io/dgraph/DgraphAsyncClientTest.java index a48ed77..22b2d6b 100644 --- a/src/test/java/io/dgraph/DgraphAsyncClientTest.java +++ b/src/test/java/io/dgraph/DgraphAsyncClientTest.java @@ -209,10 +209,10 @@ public void testBestEffortQueries() throws InterruptedException { Mutation mu = Mutation.newBuilder() - // .setCommitNow(true) + .setCommitNow(true) .setSetJson(ByteString.copyFromUtf8(json.toString())) - .build(); // this mutation won't be committed - Response mutationResp = dgraphAsyncClient.newTransaction().mutate(mu).join(); + .build(); + Response mutationResp = dgraphAsyncClient.newTransaction().mutate(mu).join(); System.out.println("mutation response:" + mutationResp); // sleep for 5 seconds for the max assigned ts to be propagated