summaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/test/java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@oath.com>2019-03-22 18:23:40 +0100
committerGitHub <noreply@github.com>2019-03-22 18:23:40 +0100
commita62e538f10f8d92af21de38000f97aea6ca7a500 (patch)
tree5a067c41c5a02ca06c6343d8e4f22f25a9d6ee7a /vespa-http-client/src/test/java
parent125c97eedfa07ab3d5c32bc775e5bb50b3feebd3 (diff)
parentb9335fafb8bba7d9bb162ad90ec3192ba7c84fb1 (diff)
Merge pull request #8881 from vespa-engine/bratseth/remove-dependencies
Bratseth/remove dependencies
Diffstat (limited to 'vespa-http-client/src/test/java')
-rw-r--r--vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java52
-rw-r--r--vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/operationProcessor/IncompleteResultsThrottlerTest.java128
-rw-r--r--vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java2
3 files changed, 128 insertions, 54 deletions
diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java
new file mode 100644
index 00000000000..0081497fb20
--- /dev/null
+++ b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/JsonTestHelper.java
@@ -0,0 +1,52 @@
+// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.vespa.http.client;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Joiner;
+import org.hamcrest.MatcherAssert;
+
+import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;
+
+import java.io.UncheckedIOException;
+
+public class JsonTestHelper {
+
+ private static final ObjectMapper mapper = new ObjectMapper();
+
+ /**
+ * Convenience method to input JSON without escaping double quotes and newlines
+ * Each parameter represents a line of JSON encoded data
+ * The lines are joined with newline and single quotes are replaced with double quotes
+ */
+ public static String inputJson(String... lines) {
+ return Joiner.on("\n").join(lines).replaceAll("'", "\"");
+ }
+
+ /** Structurally compare two JSON encoded strings */
+ public static void assertJsonEquals(String inputJson, String expectedJson) {
+ MatcherAssert.assertThat(inputJson, sameJSONAs(expectedJson));
+ }
+
+ /** Structurally compare a {@link JsonNode} and a JSON string. */
+ public static void assertJsonEquals(JsonNode left, String rightJson) {
+ try {
+ String leftJson = mapper.writeValueAsString(left);
+ assertJsonEquals(leftJson, rightJson);
+ } catch (JsonProcessingException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+
+ /** Structurally compare two {@link JsonNode}s. */
+ public static void assertJsonEquals(JsonNode left, JsonNode right) {
+ try {
+ String leftJson = mapper.writeValueAsString(left);
+ String rightJson = mapper.writeValueAsString(right);
+ assertJsonEquals(leftJson, rightJson);
+ } catch (JsonProcessingException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+}
diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/operationProcessor/IncompleteResultsThrottlerTest.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/operationProcessor/IncompleteResultsThrottlerTest.java
index 47aeb4d3b0f..1267514b0d8 100644
--- a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/operationProcessor/IncompleteResultsThrottlerTest.java
+++ b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/core/operationProcessor/IncompleteResultsThrottlerTest.java
@@ -1,9 +1,7 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core.operationProcessor;
-import com.yahoo.collections.Tuple2;
import com.yahoo.vespa.http.client.core.ThrottlePolicy;
-import com.yahoo.vespa.http.client.core.operationProcessor.IncompleteResultsThrottler;
import org.junit.Test;
import java.util.ArrayList;
@@ -35,57 +33,6 @@ public class IncompleteResultsThrottlerTest {
}
/**
- * A mock 'gateway' this is slower with more requests in-flight. It starts to become really much slower at
- * 'breakPoint' number of parallel requests.
- */
- class MockServer {
- final LinkedList<Tuple2<Long, IncompleteResultsThrottler> > messageDoneByTime = new LinkedList<>();
- final int breakPoint;
- final Random random = new Random();
- long time = 0;
-
- MockServer(int breakPoint) {
- this.breakPoint = breakPoint;
- }
-
- /**
- * Figures out when next processed data will be ready.
- * @return time in ms for next request to be finished.
- */
- long nextRequestFinished() {
- if (messageDoneByTime.isEmpty()) {
- return Integer.MAX_VALUE;
- }
- return messageDoneByTime.peek().first;
- }
-
- /**
- * Advance simulation time and call finished on any requests.
- * @param time to move to
- */
- void moveTime(long time) {
- this.time = time;
- while (!messageDoneByTime.isEmpty() && messageDoneByTime.peek().first <= time) {
- messageDoneByTime.pop().second.resultReady(true);
- }
- }
-
- /**
- * New request.
- * @param blocker do callback on blocker when request is done.
- */
- void newRequest(IncompleteResultsThrottler blocker) {
- long nextTime = (long)(20 + 0.1 * messageDoneByTime.size());
-
- if (messageDoneByTime.size() > breakPoint) {
- nextTime += (long) (40 + (random.nextDouble()) * 0.01 * messageDoneByTime.size()* messageDoneByTime.size());
- }
- nextTime += time + random.nextInt()%4;
- messageDoneByTime.push(new Tuple2<>(nextTime, blocker));
- }
- }
-
- /**
* Simulate running requests.
* @param clientCount number of parallel clients.
* @param breakPoint how many requests the server should handle in parallel before it gets slower.
@@ -267,4 +214,79 @@ public class IncompleteResultsThrottlerTest {
int distance = Math.abs(sweetSpot - size);
return 1 + 20 * distance;
}
+
+ /**
+ * A mock 'gateway' this is slower with more requests in-flight. It starts to become really much slower at
+ * 'breakPoint' number of parallel requests.
+ */
+ class MockServer {
+ final LinkedList<Tuple2<Long, IncompleteResultsThrottler> > messageDoneByTime = new LinkedList<>();
+ final int breakPoint;
+ final Random random = new Random();
+ long time = 0;
+
+ MockServer(int breakPoint) {
+ this.breakPoint = breakPoint;
+ }
+
+ /**
+ * Figures out when next processed data will be ready.
+ * @return time in ms for next request to be finished.
+ */
+ long nextRequestFinished() {
+ if (messageDoneByTime.isEmpty()) {
+ return Integer.MAX_VALUE;
+ }
+ return messageDoneByTime.peek().first;
+ }
+
+ /**
+ * Advance simulation time and call finished on any requests.
+ * @param time to move to
+ */
+ void moveTime(long time) {
+ this.time = time;
+ while (!messageDoneByTime.isEmpty() && messageDoneByTime.peek().first <= time) {
+ messageDoneByTime.pop().second.resultReady(true);
+ }
+ }
+
+ /**
+ * New request.
+ * @param blocker do callback on blocker when request is done.
+ */
+ void newRequest(IncompleteResultsThrottler blocker) {
+ long nextTime = (long)(20 + 0.1 * messageDoneByTime.size());
+
+ if (messageDoneByTime.size() > breakPoint) {
+ nextTime += (long) (40 + (random.nextDouble()) * 0.01 * messageDoneByTime.size()* messageDoneByTime.size());
+ }
+ nextTime += time + random.nextInt()%4;
+ messageDoneByTime.push(new Tuple2<>(nextTime, blocker));
+ }
+ }
+
+ private static class Tuple2<T1, T2> {
+
+ public final T1 first;
+ public final T2 second;
+
+ public Tuple2(final T1 first, final T2 second) {
+ this.first = first;
+ this.second = second;
+ }
+
+ @Override
+ public int hashCode() { throw new UnsupportedOperationException(); }
+
+ @Override
+ public boolean equals(final Object obj) { throw new UnsupportedOperationException(); }
+
+ @Override
+ public String toString() {
+ return "Tuple2(" + first + ", " + second + ")";
+ }
+
+ }
+
}
diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java
index 134274ff869..e2046fe17a6 100644
--- a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java
+++ b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/JsonReaderTest.java
@@ -12,7 +12,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
-import static com.yahoo.test.json.JsonTestHelper.inputJson;
+import static com.yahoo.vespa.http.client.JsonTestHelper.inputJson;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;