diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2019-10-11 16:17:58 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2019-10-11 16:32:55 +0200 |
commit | d31f043b3cca1a97dc5fb8b81b85c485667c28cf (patch) | |
tree | 5e10bb7169e9b2c554bc9a4bba3dc28735ec065f /clustercontroller-utils/src | |
parent | a879e07cbf4885da58ac51844f42176db5604695 (diff) |
Remove dead code
Diffstat (limited to 'clustercontroller-utils/src')
19 files changed, 3 insertions, 1059 deletions
diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClientWithBase.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClientWithBase.java deleted file mode 100644 index d824303df57..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClientWithBase.java +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; - -public class AsyncHttpClientWithBase<V extends HttpResult> implements AsyncHttpClient<V> { - - protected final AsyncHttpClient<V> client; - private HttpRequest baseRequest = new HttpRequest(); - - public AsyncHttpClientWithBase(AsyncHttpClient<V> client) { - if (client == null) throw new IllegalArgumentException("HTTP client must be set."); - this.client = client; - } - - /** - * If all your http requests have common features you want to set once, you can provide those values in a base - * request. For instance, if you specify a host and a port using this function, all your requests will use that - * host and port unless specified in the request you execute. - */ - public void setHttpRequestBase(HttpRequest r) { - this.baseRequest = (r == null ? new HttpRequest() : r.clone()); - } - - public HttpRequest getHttpRequestBase() { - return baseRequest; - } - - @Override - public AsyncOperation<V> execute(HttpRequest r) { - return client.execute(baseRequest.merge(r)); - } - - @Override - public void close() { - client.close(); - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/JsonAsyncHttpClient.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/JsonAsyncHttpClient.java deleted file mode 100644 index dac4decc1ee..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/JsonAsyncHttpClient.java +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.RedirectedAsyncOperation; -import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; - -/** - * Wrapped for the HTTP client, converting requests to/from JSON. - */ -public class JsonAsyncHttpClient implements AsyncHttpClient<JsonHttpResult> { - - private AsyncHttpClient<HttpResult> client; - private boolean verifyRequestContentAsJson = true; - private boolean addJsonContentType = true; - - public JsonAsyncHttpClient(AsyncHttpClient<HttpResult> client) { - this.client = client; - } - - public JsonAsyncHttpClient verifyRequestContentAsJson(boolean doIt) { - verifyRequestContentAsJson = doIt; - return this; - } - - public JsonAsyncHttpClient addJsonContentType(boolean doIt) { - addJsonContentType = doIt; - return this; - } - - public AsyncOperation<JsonHttpResult> execute(HttpRequest r) { - if (verifyRequestContentAsJson) { - if (r.getPostContent() != null && !(r.getPostContent() instanceof JSONObject)) { - try{ - r = r.clone().setPostContent(new JSONObject(r.getPostContent().toString())); - } catch (JSONException e) { - throw new IllegalArgumentException(e); - } - } - } - if (addJsonContentType && r.getPostContent() != null) { - r = r.clone().addHttpHeader("Content-Type", "application/json"); - } - final AsyncOperation<HttpResult> op = client.execute(r); - return new RedirectedAsyncOperation<HttpResult, JsonHttpResult>(op) { - @Override - public JsonHttpResult getResult() { - return (op.getResult() == null ? null : new JsonHttpResult(op.getResult())); - } - }; - } - - @Override - public void close() { - client.close(); - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClient.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClient.java deleted file mode 100644 index 2d967223f9e..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClient.java +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.PipedAsyncOperation; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class LoggingAsyncHttpClient<T extends HttpResult> extends AsyncHttpClientWithBase<T> { - - private static final Logger log = Logger.getLogger(LoggingAsyncHttpClient.class.getName()); - private int requestCounter = 0; - - public LoggingAsyncHttpClient(AsyncHttpClient<T> client) { - super(client); - log.info("Logging HTTP requests if fine logging level is added"); - } - - public AsyncOperation<T> execute(HttpRequest r) { - final int requestCount = ++requestCounter; - log.fine("Issuing HTTP request " + requestCount + ": " + r.toString(true)); - final AsyncOperation<T> op = client.execute(r); - return new PipedAsyncOperation<T, T>(op) { - @Override - public T convertResult(T result) { - if (log.isLoggable(Level.FINE)) { - if (op.isSuccess()) { - log.fine("HTTP request " + requestCount + " completed: " + result.toString(true)); - } else { - StringWriter sw = new StringWriter(); - op.getCause().printStackTrace(new PrintWriter(sw)); - log.fine("HTTP request " + requestCount + " failed: " + sw); - } - } - return result; - } - }; - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/ProxyAsyncHttpClient.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/ProxyAsyncHttpClient.java deleted file mode 100644 index 3d6658ae366..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/ProxyAsyncHttpClient.java +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; - -public class ProxyAsyncHttpClient<V extends HttpResult> extends AsyncHttpClientWithBase<V> { - - private final String proxyHost; - private final int proxyPort; - - public ProxyAsyncHttpClient(AsyncHttpClient<V> client, String proxyHost, int proxyPort) { - super(client); - this.proxyHost = proxyHost; - this.proxyPort = proxyPort; - } - - @Override - public AsyncOperation<V> execute(HttpRequest r) { - r = getHttpRequestBase().merge(r); - if (r.getHost() == null || r.getPath() == null) { - throw new IllegalStateException("Host and path must be set prior to being able to proxy an HTTP request"); - } - StringBuilder path = new StringBuilder().append(r.getHost()); - if (r.getPort() != 0) path.append(':').append(r.getPort()); - if (r.getPath().isEmpty() || r.getPath().charAt(0) != '/') path.append('/'); - path.append(r.getPath()); - return client.execute(r.setHost(proxyHost).setPort(proxyPort).setPath(path.toString())); - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/RequestQueue.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/RequestQueue.java deleted file mode 100644 index 2a3af5724ab..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/RequestQueue.java +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncCallback; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; - -import java.util.LinkedList; -import java.util.logging.Logger; - -/** - * Utility class to schedule HTTP requests and keeping a maximum amount of them pending at a time. - */ -public class RequestQueue<V extends HttpResult> { - - private static final Logger log = Logger.getLogger(RequestQueue.class.getName()); - private final AsyncHttpClient<V> httpClient; - private final LinkedList<Request<V>> requestQueue = new LinkedList<>(); - private final int maxPendingRequests; - private int pendingRequests = 0; - - public RequestQueue(AsyncHttpClient<V> httpClient, int maxPendingRequests) { - this.httpClient = httpClient; - this.maxPendingRequests = maxPendingRequests; - } - - public boolean empty() { - synchronized (requestQueue) { - return (requestQueue.isEmpty() && pendingRequests == 0); - } - } - - public void waitUntilEmpty() throws InterruptedException { - synchronized (requestQueue) { - while (!empty()) { - requestQueue.wait(); - } - } - } - - public void schedule(HttpRequest request, AsyncCallback<V> callback) { - log.fine("Scheduling " + request + " call"); - synchronized (requestQueue) { - requestQueue.addLast(new Request<>(request, callback)); - sendMore(); - } - } - - private void sendMore() { - while (pendingRequests < maxPendingRequests && !requestQueue.isEmpty()) { - Request<V> call = requestQueue.removeFirst(); - log.fine("Sending " + call.getRequest() + "."); - ++pendingRequests; - AsyncOperation<V> op = httpClient.execute(call.getRequest()); - op.register(call); - } - } - - private class Request<V extends HttpResult> implements AsyncCallback<V> { - private final HttpRequest request; - private final AsyncCallback<V> callback; - - Request(HttpRequest request, AsyncCallback<V> callback) { - this.request = request; - this.callback = callback; - } - - public HttpRequest getRequest() { return request; } - - @Override - public void done(AsyncOperation<V> op) { - if (op.isSuccess()) { - log.fine("Operation " + op.getName() + " completed successfully"); - } else { - log.fine("Operation " + op.getName() + " failed: " + op.getCause()); - } - synchronized (requestQueue) { - --pendingRequests; - } - callback.done(op); - synchronized (requestQueue) { - requestQueue.notifyAll(); - sendMore(); - } - } - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/SyncHttpClient.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/SyncHttpClient.java deleted file mode 100644 index b8890550dc6..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/SyncHttpClient.java +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -public interface SyncHttpClient { - - HttpResult execute(HttpRequest r); - - /** Attempt to cancel all pending operations and shut down the client. */ - void close(); - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/TimeoutHandler.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/TimeoutHandler.java deleted file mode 100644 index eae7e15541b..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/TimeoutHandler.java +++ /dev/null @@ -1,151 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncCallback; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import com.yahoo.vespa.clustercontroller.utils.util.Clock; - -import java.util.*; -import java.util.concurrent.Executor; -import java.util.concurrent.TimeoutException; -import java.util.logging.Logger; - -public class TimeoutHandler<V extends HttpResult> extends AsyncHttpClientWithBase<V> { - - public static class InternalRequest<V extends HttpResult> extends AsyncOperationImpl<V> { - final AsyncOperation<V> operation; - long startTime; - long timeout; - - public InternalRequest(AsyncOperation<V> op, long startTime, long timeout) { - super(op.getName(), op.getDescription()); - this.operation = op; - this.startTime = startTime; - this.timeout = timeout; - op.register(new AsyncCallback<V>() { - @Override - public void done(AsyncOperation<V> op) { - if (!isDone()) { - if (op.isSuccess()) { - setResult(op.getResult()); - } else { - setFailure(op.getCause(), op.getResult()); - } - } - } - }); - } - - public long getTimeoutTime() { return startTime + timeout; } - - public void handleTimeout(long currentTime) { - long timePassed = currentTime - startTime; - this.setFailure(new TimeoutException("Operation timeout. " + timePassed + " ms since operation was issued. Timeout was " + timeout + " ms.")); - operation.cancel(); - } - - @Override - public boolean cancel() { return operation.cancel(); } - @Override - public boolean isCanceled() { return operation.isCanceled(); } - @Override - public Double getProgress() { return (isDone() ? Double.valueOf(1.0) : operation.getProgress()); } - } - - public static class ChangeLogger { - private InternalRequest lastTimeoutLogged = null; - private boolean emptyLogged = true; - - public void logChanges(TreeMap<Long, InternalRequest> requests) { - if (requests.isEmpty()) { - if (!emptyLogged) { - log.finest("No more pending requests currently."); - emptyLogged = true; - } - } else { - emptyLogged = false; - InternalRequest r = requests.firstEntry().getValue(); - if (lastTimeoutLogged == null || !lastTimeoutLogged.equals(r)) { - lastTimeoutLogged = r; - log.finest("Next operation to possibly timeout will do so at " + r.getTimeoutTime()); - } - } - } - } - - private final static Logger log = Logger.getLogger(TimeoutHandler.class.getName()); - private final TreeMap<Long, InternalRequest> requests = new TreeMap<>(); - private final ChangeLogger changeLogger = new ChangeLogger(); - private final Clock clock; - private boolean run = true; - private Runnable timeoutHandler = new Runnable() { - @Override - public void run() { - log.fine("Starting timeout monitor thread"); - while (true) { - performTimeoutHandlerTick(); - synchronized (clock) { - try{ clock.wait(100); } catch (InterruptedException e) {} - if (!run) break; - } - } - log.fine("Stopped timeout monitor thread"); - } - }; - - public TimeoutHandler(Executor executor, Clock clock, AsyncHttpClient<V> client) { - super(client); - this.clock = clock; - executor.execute(timeoutHandler); - } - - @Override - public void close() { - synchronized (clock) { - run = false; - clock.notifyAll(); - } - synchronized (requests) { - for (InternalRequest r : requests.values()) { - r.operation.cancel(); - r.setFailure(new TimeoutException("Timeout handler shutting down. Shutting down all requests monitored.")); - } - requests.clear(); - } - } - - @Override - public AsyncOperation<V> execute(HttpRequest r) { - AsyncOperation<V> op = super.execute(r); - InternalRequest<V> request = new InternalRequest<>(op, clock.getTimeInMillis(), r.getTimeoutMillis()); - synchronized (requests) { - requests.put(request.getTimeoutTime(), request); - } - return request; - } - - void performTimeoutHandlerTick() { - synchronized (requests) { - removeCompletedRequestsFromTimeoutList(); - handleTimeoutsAtTime(clock.getTimeInMillis()); - changeLogger.logChanges(requests); - } - } - - private void removeCompletedRequestsFromTimeoutList() { - while (!requests.isEmpty() && requests.firstEntry().getValue().operation.isDone()) { - requests.remove(requests.firstEntry().getKey()); - log.finest("Removed completed request from operation timeout list."); - } - } - - private void handleTimeoutsAtTime(long currentTime) { - Map<Long, InternalRequest> timeouts = requests.subMap(0l, currentTime + 1); - for (InternalRequest r : timeouts.values()) { - r.handleTimeout(currentTime); - requests.values().remove(r); - } - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/FakeClock.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/FakeClock.java deleted file mode 100644 index 80f06b62675..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/FakeClock.java +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.test; - -import java.util.logging.Logger; - -/** - * Unit tests want to fast forward time to avoid waiting for time to pass - */ -public class FakeClock extends SettableClock { - - private static final Logger logger = Logger.getLogger(FakeClock.class.getName()); - protected long currentTime = 1; - - @Override - public long getTimeInMillis() { - return currentTime; - } - - @Override - public void adjust(long adjustment) { - synchronized (this) { - logger.fine("Adjusting clock, adding " + adjustment + " ms to it."); - currentTime += adjustment; - notifyAll(); - } - } - - @Override - public void set(long newTime) { - synchronized (this) { - if (newTime < currentTime) { - // throw new IllegalArgumentException("Clock attempted to be set to go backwards"); - } - currentTime = newTime; - } - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/SettableClock.java b/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/SettableClock.java deleted file mode 100644 index 12b398709f0..00000000000 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/SettableClock.java +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.test; - -import com.yahoo.vespa.clustercontroller.utils.util.Clock; - -public abstract class SettableClock extends Clock { - - public abstract void set(long newTime); - public abstract void adjust(long adjustment); - -} diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClientWithBaseTest.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClientWithBaseTest.java deleted file mode 100644 index 0175eab84c2..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClientWithBaseTest.java +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.fail; - -public class AsyncHttpClientWithBaseTest { - - @Test - public void testOverride() { - class HttpClient implements AsyncHttpClient<HttpResult> { - HttpRequest lastRequest; - @Override - public AsyncOperation<HttpResult> execute(HttpRequest r) { - lastRequest = r; - return new AsyncOperationImpl<>("test"); - } - @Override - public void close() { - } - } - - HttpClient client = new HttpClient(); - AsyncHttpClientWithBase<HttpResult> base = new AsyncHttpClientWithBase<>(client); - // No override by default - HttpRequest r = new HttpRequest().setPath("/foo").setHost("bar").setPort(50); - base.execute(r); - assertEquals(client.lastRequest, r); - // Base request always set - base.setHttpRequestBase(null); - base.execute(r); - assertEquals(client.lastRequest, r); - // Set an override - base.setHttpRequestBase(new HttpRequest().setHttpOperation(HttpRequest.HttpOp.DELETE)); - base.execute(r); - assertNotSame(client.lastRequest, r); - assertEquals(HttpRequest.HttpOp.DELETE, client.lastRequest.getHttpOperation()); - - base.close(); - } - - @Test - public void testClientMustBeSet() { - try{ - new AsyncHttpClientWithBase<>(null); - fail(); - } catch (IllegalArgumentException e) { - } - } - -} diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/DummyAsyncHttpClient.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/DummyAsyncHttpClient.java deleted file mode 100644 index 7070fb8570e..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/DummyAsyncHttpClient.java +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; - -public class DummyAsyncHttpClient implements AsyncHttpClient<HttpResult> { - HttpResult result; - HttpRequest lastRequest; - - public DummyAsyncHttpClient(HttpResult result) { - this.result = result; - } - - @Override - public AsyncOperation<HttpResult> execute(HttpRequest r) { - lastRequest = r; - AsyncOperationImpl<HttpResult> op = new AsyncOperationImpl<>(r.toString()); - op.setResult(result); - return op; - } - - @Override - public void close() { - } -} diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/JsonAsyncHttpClientTest.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/JsonAsyncHttpClientTest.java deleted file mode 100644 index 3d3cd517020..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/JsonAsyncHttpClientTest.java +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import org.codehaus.jettison.json.JSONObject; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -public class JsonAsyncHttpClientTest { - - @Test - public void testJSONInJSONOut() throws Exception { - DummyAsyncHttpClient dummy = new DummyAsyncHttpClient( - new HttpResult().setContent(new JSONObject().put("bar", 42))); - JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy); - client.addJsonContentType(true); - client.verifyRequestContentAsJson(true); - - HttpRequest r = new HttpRequest(); - r.setPostContent(new JSONObject().put("foo", 34)); - - AsyncOperation<JsonHttpResult> result = client.execute(r); - - assertEquals(new JSONObject().put("bar", 42).toString(), result.getResult().getJson().toString()); - assertTrue(result.isSuccess()); - - result.toString(); - client.close(); - } - - @Test - public void testStringInJSONOut() throws Exception { - DummyAsyncHttpClient dummy = new DummyAsyncHttpClient( - new HttpResult().setContent(new JSONObject().put("bar", 42).toString())); - JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy); - - HttpRequest r = new HttpRequest(); - r.setPostContent(new JSONObject().put("foo", 34).toString()); - - AsyncOperation<JsonHttpResult> result = client.execute(r); - - assertEquals(new JSONObject().put("bar", 42).toString(), result.getResult().getJson().toString()); - } - - @Test - public void testIllegalJsonIn() throws Exception { - DummyAsyncHttpClient dummy = new DummyAsyncHttpClient( - new HttpResult().setContent(new JSONObject().put("bar", 42))); - JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy); - - try { - HttpRequest r = new HttpRequest(); - r.setPostContent("my illegal json"); - - client.execute(r); - assertTrue(false); - } catch (Exception e) { - - } - } - - @Test - public void testIllegalJSONOut() throws Exception { - DummyAsyncHttpClient dummy = new DummyAsyncHttpClient( - new HttpResult().setContent("my illegal json")); - JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy); - - HttpRequest r = new HttpRequest(); - r.setPostContent(new JSONObject().put("foo", 34).toString()); - - AsyncOperation<JsonHttpResult> result = client.execute(r); - - assertEquals("{\"error\":\"Invalid JSON in output: A JSONObject text must begin with '{' at character 1 of my illegal json\",\"output\":\"my illegal json\"}", result.getResult().getJson().toString()); - } - - @Test - public void testEmptyReply() { - class Client implements AsyncHttpClient<HttpResult> { - AsyncOperationImpl<HttpResult> lastOp; - @Override - public AsyncOperation<HttpResult> execute(HttpRequest r) { - return lastOp = new AsyncOperationImpl<>(r.toString()); - } - @Override - public void close() { - } - }; - Client client = new Client(); - JsonAsyncHttpClient jsonClient = new JsonAsyncHttpClient(client); - AsyncOperation<JsonHttpResult> op = jsonClient.execute(new HttpRequest()); - client.lastOp.setResult(null); - assertNull(op.getResult()); - } - - @Test - public void testNotVerifyingJson() throws Exception { - DummyAsyncHttpClient dummy = new DummyAsyncHttpClient( - new HttpResult().setContent(new JSONObject().put("bar", 42))); - JsonAsyncHttpClient client = new JsonAsyncHttpClient(dummy); - client.addJsonContentType(true); - client.verifyRequestContentAsJson(false); - - HttpRequest r = new HttpRequest(); - r.setPostContent(new JSONObject().put("foo", 34)); - - AsyncOperation<JsonHttpResult> result = client.execute(r); - - assertEquals(new JSONObject().put("bar", 42).toString(), result.getResult().getJson().toString()); - assertTrue(result.isSuccess()); - - result.toString(); - client.close(); - } - -} diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClientTest.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClientTest.java deleted file mode 100644 index 37841f7ca29..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/LoggingAsyncHttpClientTest.java +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import org.junit.Test; - -import java.util.logging.Level; -import java.util.logging.Logger; - -import static org.junit.Assert.assertEquals; - -public class LoggingAsyncHttpClientTest { - - class HttpClient implements AsyncHttpClient<HttpResult> { - AsyncOperationImpl<HttpResult> lastOp; - @Override - public AsyncOperation<HttpResult> execute(HttpRequest r) { - return lastOp = new AsyncOperationImpl<>("test"); - } - @Override - public void close() { - } - } - - @Test - public void testWithoutDebugLog() { - doRequests(); - } - - @Test - public void testWithDebugLog() { - Logger log = Logger.getLogger(LoggingAsyncHttpClient.class.getName()); - log.setLevel(Level.FINE); - doRequests(); - } - - private void doRequests() { - { - HttpClient client = new HttpClient(); - LoggingAsyncHttpClient<HttpResult> loggingClient = new LoggingAsyncHttpClient<>(client); - AsyncOperation<HttpResult> op = loggingClient.execute(new HttpRequest()); - client.lastOp.setResult(new HttpResult().setContent("foo")); - assertEquals("foo", op.getResult().getContent()); - } - { - HttpClient client = new HttpClient(); - LoggingAsyncHttpClient<HttpResult> loggingClient = new LoggingAsyncHttpClient<>(client); - AsyncOperation<HttpResult> op = loggingClient.execute(new HttpRequest()); - client.lastOp.setFailure(new Exception("foo")); - assertEquals("foo", op.getCause().getMessage()); - } - } - -} diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/ProxyAsyncHttpClientTest.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/ProxyAsyncHttpClientTest.java deleted file mode 100644 index 062fd4aaa32..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/ProxyAsyncHttpClientTest.java +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import org.codehaus.jettison.json.JSONObject; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class ProxyAsyncHttpClientTest { - - @Test - public void testSimple() throws Exception { - // Can't really test much here, but verifies that the code runs. - DummyAsyncHttpClient dummy = new DummyAsyncHttpClient( - new HttpResult().setContent(new JSONObject().put("bar", 42))); - ProxyAsyncHttpClient client = new ProxyAsyncHttpClient<>(dummy, "myproxyhost", 1234); - - HttpRequest r = new HttpRequest(); - r.setPath("/foo"); - r.setHost("myhost"); - r.setPort(4567); - - r.setPostContent(new JSONObject().put("foo", 34)); - - client.execute(r); - - assertEquals(new HttpRequest().setPath("/myhost:4567/foo") - .setHost("myproxyhost") - .setPort(1234) - .setPostContent(new JSONObject().put("foo", 34)), - dummy.lastRequest); - } - - @Test - public void testNoAndEmptyPath() throws Exception { - DummyAsyncHttpClient dummy = new DummyAsyncHttpClient( - new HttpResult().setContent(new JSONObject().put("bar", 42))); - ProxyAsyncHttpClient client = new ProxyAsyncHttpClient<>(dummy, "myproxyhost", 1234); - try{ - client.execute(new HttpRequest()); - assertTrue(false); - } catch (IllegalStateException e) { - assertTrue(e.getMessage().contains("Host and path must be set prior")); - } - client.execute(new HttpRequest().setHost("local").setPath("")); - } - -} diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/RequestQueueTest.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/RequestQueueTest.java deleted file mode 100644 index 230920df53f..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/RequestQueueTest.java +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncCallback; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import org.junit.Test; - -import java.util.LinkedList; - -import static org.junit.Assert.assertEquals; - -public class RequestQueueTest { - - public static class Request { - public final HttpRequest request; - public final AsyncOperationImpl<HttpResult> result; - - public Request(HttpRequest r, AsyncOperationImpl<HttpResult> rr) { - this.request = r; - this.result = rr; - } - } - - public class TestClient implements AsyncHttpClient<HttpResult> { - LinkedList<Request> requests = new LinkedList<>(); - @Override - public AsyncOperation<HttpResult> execute(HttpRequest r) { - Request p = new Request(r, new AsyncOperationImpl<HttpResult>(r.toString())); - synchronized (requests) { - requests.addLast(p); - } - return p.result; - } - @Override - public void close() {} - }; - - @Test - public void testNormalUsage() { - TestClient client = new TestClient(); - RequestQueue<HttpResult> queue = new RequestQueue<>(client, 4); - final LinkedList<HttpResult> results = new LinkedList<>(); - for (int i=0; i<10; ++i) { - queue.schedule(new HttpRequest().setPath("/" + i), new AsyncCallback<HttpResult>() { - @Override - public void done(AsyncOperation<HttpResult> op) { - if (op.isSuccess()) { - results.add(op.getResult()); - } else { - results.add(new HttpResult().setHttpCode(500, op.getCause().getMessage())); - } - } - }); - } - assertEquals(4, client.requests.size()); - for (int i=0; i<3; ++i) { - Request p = client.requests.removeFirst(); - p.result.setResult(new HttpResult()); - assertEquals(true, results.getLast().isSuccess()); - } - assertEquals(4, client.requests.size()); - for (int i=0; i<7; ++i) { - Request p = client.requests.removeFirst(); - p.result.setFailure(new Exception("Fail")); - assertEquals(false, results.getLast().isSuccess()); - } - assertEquals(0, client.requests.size()); - assertEquals(true, queue.empty()); - assertEquals(10, results.size()); - } - - public class Waiter implements Runnable { - boolean waiting = false; - boolean completed = false; - RequestQueue<HttpResult> queue; - Waiter(RequestQueue<HttpResult> queue) { - this.queue = queue; - } - public void run() { - try{ - waiting = true; - queue.waitUntilEmpty(); - } catch (InterruptedException e) { throw new Error(e); } - completed = true; - } - } - - @Test - public void testWaitUntilEmpty() throws Exception { - TestClient client = new TestClient(); - RequestQueue<HttpResult> queue = new RequestQueue<>(client, 4); - final LinkedList<HttpResult> result = new LinkedList<>(); - queue.schedule(new HttpRequest().setPath("/foo"), new AsyncCallback<HttpResult>() { - @Override - public void done(AsyncOperation<HttpResult> op) { - result.add(op.getResult()); - } - }); - Waiter waiter = new Waiter(queue); - Thread thread = new Thread(waiter); - thread.start(); - while (!waiter.waiting) { - Thread.sleep(1); - } - assertEquals(0, result.size()); - client.requests.getFirst().result.setResult(new HttpResult()); - while (!waiter.completed) { - Thread.sleep(1); - } - assertEquals(1, result.size()); - } - -} diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/TimeoutHandlerTest.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/TimeoutHandlerTest.java deleted file mode 100644 index 72a2a4eab8a..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/communication/http/TimeoutHandlerTest.java +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; - -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncUtils; -import com.yahoo.vespa.clustercontroller.utils.test.FakeClock; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.concurrent.ArrayBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class TimeoutHandlerTest { - - public class TestClient implements AsyncHttpClient<HttpResult> { - AsyncOperationImpl<HttpResult> lastOp; - @Override - public AsyncOperation<HttpResult> execute(HttpRequest r) { - return lastOp = new AsyncOperationImpl<>("test"); - } - @Override - public void close() {} - }; - - private ThreadPoolExecutor executor; - private TestClient client; - private FakeClock clock; - private TimeoutHandler<HttpResult> handler; - - @Before - public void setUp() { - executor = new ThreadPoolExecutor(10, 100, 100, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1000)); - clock = new FakeClock(); - client = new TestClient(); - handler = new TimeoutHandler<>(executor, clock, client); - } - - @After - public void tearDown() { - handler.close(); - executor.shutdown(); - } - - @Test - public void testTimeout() { - AsyncOperation<HttpResult> op = handler.execute(new HttpRequest().setTimeout(1000)); - assertFalse(op.isDone()); - clock.adjust(999); - // Give it a bit of time for timeout handler to have a chance of timout out prematurely - try{ Thread.sleep(1); } catch (InterruptedException e) {} - assertFalse(op.isDone()); - clock.adjust(1); - AsyncUtils.waitFor(op); - assertTrue(op.isDone()); - assertFalse(op.isSuccess()); - assertTrue(op.getCause().getMessage(), op.getCause().getMessage().contains("Operation timeout")); - // After timeout, finishing the original request no longer matter - client.lastOp.setResult(new HttpResult()); - assertFalse(op.isSuccess()); - assertTrue(op.getCause().getMessage(), op.getCause().getMessage().contains("Operation timeout")); - } - - @Test - public void testNoTimeout() { - AsyncOperation<HttpResult> op = handler.execute(new HttpRequest().setTimeout(1000)); - clock.adjust(999); - assertFalse(op.isDone()); - client.lastOp.setResult(new HttpResult().setContent("foo")); - AsyncUtils.waitFor(op); - assertTrue(op.isDone()); - assertTrue(op.isSuccess()); - assertEquals("foo", op.getResult().getContent()); - } - - @Test - public void testNoTimeoutFailing() { - AsyncOperation<HttpResult> op = handler.execute(new HttpRequest().setTimeout(1000)); - clock.adjust(999); - assertFalse(op.isDone()); - client.lastOp.setFailure(new Exception("foo")); - AsyncUtils.waitFor(op); - assertTrue(op.isDone()); - assertFalse(op.isSuccess()); - assertEquals("foo", op.getCause().getMessage()); - } - - @Test - public void testProvokeCompletedOpPurgeInTimeoutList() { - AsyncOperation<HttpResult> op1 = handler.execute(new HttpRequest().setTimeout(1000)); - AsyncOperationImpl<HttpResult> op1Internal = client.lastOp; - clock.adjust(300); - AsyncOperation<HttpResult> op2 = handler.execute(new HttpRequest().setTimeout(1000)); - clock.adjust(300); - op1Internal.setResult(new HttpResult().setContent("foo")); - AsyncUtils.waitFor(op1); - clock.adjust(800); - AsyncUtils.waitFor(op2); - assertEquals(true, op1.isDone()); - assertEquals(true, op2.isDone()); - assertEquals(true, op1.isSuccess()); - assertEquals(false, op2.isSuccess()); - } - - @Test - public void testNothingButGetCoverage() { - AsyncOperation<HttpResult> op = handler.execute(new HttpRequest().setTimeout(1000)); - op.getProgress(); - op.cancel(); - assertFalse(op.isCanceled()); // Cancel not currently supported - client.lastOp.setResult(new HttpResult().setContent("foo")); - AsyncUtils.waitFor(op); - op.getProgress(); - op = handler.execute(new HttpRequest().setTimeout(1000)); - handler.performTimeoutHandlerTick(); - handler.performTimeoutHandlerTick(); - client.lastOp.setResult(new HttpResult().setContent("foo")); - AsyncUtils.waitFor(op); - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClient.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/AsyncHttpClient.java index 6af3e4de759..29f86842b34 100644 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/communication/http/AsyncHttpClient.java +++ b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/AsyncHttpClient.java @@ -1,7 +1,9 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.communication.http; +package com.yahoo.vespa.clustercontroller.utils.test; import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; +import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest; +import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult; /** * Abstraction of an asynchronous HTTP client, such that applications don't need to depend directly on an HTTP client. diff --git a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/FakeClockTest.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/FakeClockTest.java deleted file mode 100644 index 47edd7ac55c..00000000000 --- a/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/FakeClockTest.java +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.clustercontroller.utils.test; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class FakeClockTest { - - @Test - public void testSimple() { - FakeClock clock = new FakeClock(); - // Should not start at 0, as that is common not initialized yet value - assertTrue(clock.getTimeInMillis() > 0); - long start = clock.getTimeInMillis(); - - clock.adjust(5); - assertEquals(start + 5, clock.getTimeInMillis()); - - clock.set(start + 10); - assertEquals(start + 10, clock.getTimeInMillis()); - - clock.adjust(5); - assertEquals(start + 15, clock.getTimeInMillis()); - } - - // TODO: This should probably throw exceptions.. However, that doesn't seem to be current behavior. - // I suspect some tests misuse the clock to reset things to run another test. Should probably be fixed. - @Test - public void testTurnTimeBack() { - FakeClock clock = new FakeClock(); - clock.set(1000); - - clock.set(500); - assertEquals(500, clock.getTimeInMillis()); - - clock.adjust(-100); - assertEquals(400, clock.getTimeInMillis()); - } - -} diff --git a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/TestTransport.java b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/TestTransport.java index 87ecd127e44..d2e76cd77f9 100644 --- a/clustercontroller-utils/src/main/java/com/yahoo/vespa/clustercontroller/utils/test/TestTransport.java +++ b/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/TestTransport.java @@ -3,7 +3,6 @@ package com.yahoo.vespa.clustercontroller.utils.test; import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation; import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl; -import com.yahoo.vespa.clustercontroller.utils.communication.http.AsyncHttpClient; import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest; import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequestHandler; import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult; |