Merge pull request #1017 from jclouds/cleanup-basehttptest

clean up style on base http test class
This commit is contained in:
Adrian Cole 2012-12-01 15:14:55 -08:00
commit ac8be50461
4 changed files with 153 additions and 360 deletions

View File

@ -18,7 +18,16 @@
*/
package org.jclouds.http;
import static com.google.common.io.Closeables.closeQuietly;
import static java.lang.String.format;
import static org.jclouds.crypto.CryptoStreams.base64;
import static org.jclouds.crypto.CryptoStreams.md5Base64;
import static org.jclouds.http.options.GetOptions.Builder.tail;
import static org.jclouds.io.Payloads.newFilePayload;
import static org.jclouds.io.Payloads.newStringPayload;
import static org.jclouds.util.Throwables2.getFirstThrowableOfType;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
@ -27,31 +36,23 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.security.MessageDigest;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.http.options.GetOptions;
import org.jclouds.io.InputSuppliers;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.util.Strings2;
import org.jclouds.util.Throwables2;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.common.io.Closeables;
/**
* Tests for functionality all HttpCommandExecutorServices must express. These tests will operate
* against an in-memory http engine, so as to ensure end-to-end functionality works.
* Tests for functionality all {@link HttpCommandExecutorService http executor
* services} must express. These tests will operate against an in-memory http
* engine, so as to ensure end-to-end functionality works.
*
* @author Adrian Cole
*/
@ -59,38 +60,30 @@ import com.google.common.io.Closeables;
public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends BaseJettyTest {
@Test(invocationCount = 25, timeOut = 5000)
public void testRequestFilter() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testRequestFilter() {
assertEquals(client.downloadFilter("", "filterme").trim(), "test");
}
// TODO: filtering redirect test
@Test(invocationCount = 5, timeOut = 5000)
public void testGetStringWithHeader() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetStringWithHeader() {
assertEquals(client.download("", "test").trim(), "test");
}
@Test(invocationCount = 1, timeOut = 5000)
public void testAlternateMethod() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testAlternateMethod() {
assertEquals(client.rowdy("").trim(), XML);
}
@Test(invocationCount = 5, timeOut = 5000)
public void testGetString() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
public void testGetString() {
assertEquals(client.download("").trim(), XML);
}
@Test(invocationCount = 5, timeOut = 5000)
public void testGetStringViaRequest() throws ExecutionException, InterruptedException, TimeoutException, IOException {
assertEquals(
Strings2.toStringAndClose(
client.invoke(
HttpRequest.builder().method("GET")
.endpoint("http://localhost:" + testPort + "/objects/").build()).getPayload()
.getInput()).trim(), XML);
public void testGetStringViaRequest() throws IOException {
HttpResponse getStringResponse = client.invoke(HttpRequest.builder().method("GET")
.endpoint(format("http://localhost:%d/objects/", testPort)).build());
assertEquals(Strings2.toString(getStringResponse.getPayload()).trim(), XML);
}
@DataProvider(name = "gets")
@ -99,71 +92,52 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
}
@Test(invocationCount = 5, timeOut = 5000, dataProvider = "gets")
public void testGetStringSynch(String uri) throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
// TODO why need trim?
public void testGetStringSynch(String uri) {
assertEquals(client.synch(uri).trim(), XML);
}
@Test(invocationCount = 5, timeOut = 5000)
public void testGetException() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
assertEquals(client.downloadException("", GetOptions.Builder.tail(1)).trim(), "foo");
public void testGetException() {
assertEquals(client.downloadException("", tail(1)).trim(), "foo");
}
@Test(invocationCount = 5, timeOut = 5000)
public void testGetSynchException() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetSynchException() {
assertEquals(client.synchException("", "").trim(), "foo");
}
@Test(invocationCount = 5, timeOut = 5000)
public void testGetStringRedirect() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetStringRedirect() {
assertEquals(client.download("redirect").trim(), XML2);
}
@Test(invocationCount = 100, timeOut = 5000)
public void testGetBigFile() throws ExecutionException, InterruptedException, TimeoutException, IOException {
public void testGetBigFile() throws IOException {
InputStream input = getConsitution();
try {
assertEquals(CryptoStreams.md5Base64(InputSuppliers.of(input)), md5);
assertEquals(md5Base64(InputSuppliers.of(input)), md5);
} catch (RuntimeException e) {
Closeables.closeQuietly(input);
closeQuietly(input);
// since we are parsing client side, and not through a response
// handler, the user
// must retry directly. In this case, we are assuming lightning doesn't
// strike
// twice in the same spot.
if (Throwables2.getFirstThrowableOfType(e, IOException.class) != null) {
// handler, the user must retry directly. In this case, we are assuming
// lightning doesn't strike twice in the same spot.
if (getFirstThrowableOfType(e, IOException.class) != null) {
input = getConsitution();
assertEquals(CryptoStreams.md5Base64(InputSuppliers.of(input)), md5);
assertEquals(md5Base64(InputSuppliers.of(input)), md5);
}
} finally {
closeQuietly(input);
}
}
private InputStream getConsitution() {
InputStream input = context.utils().http()
.get(URI.create(String.format("http://localhost:%d/%s", testPort, "101constitutions")));
return input;
}
@Test(enabled = false, invocationCount = 5, timeOut = 5000)
public void testGetStringPermanentRedirect() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
// GetString get = factory.createGetString("permanentredirect");
// assert get != null;
// client.submit(get);
// assertEquals(get.get(10, TimeUnit.SECONDS).trim(), XML2);
// TODO assert misses are only one, as permanent redirects paths should be
// remembered.
URI constitutionUri = URI.create(format("http://localhost:%d/101constitutions", testPort));
return context.utils().http().get(constitutionUri);
}
/**
* Tests sending a big file to the server. Note: this is a heavy test, takes several minutes to
* finish.
*
* @throws java.io.IOException
* Tests sending a big file to the server. Note: this is a heavy test, takes
* several minutes to finish.
*/
@Test(invocationCount = 1)
public void testUploadBigFile() throws IOException {
@ -185,35 +159,30 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
os.write((byte) 'a');
}
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
Closeables.closeQuietly(out);
closeQuietly(out);
}
Payload payload = Payloads.newFilePayload(f);
Payload payload = newFilePayload(f);
byte[] digest = digester.digest();
payload.getContentMetadata().setContentMD5(digest);
Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
assertEquals(headers.get("x-Content-MD5"),
ImmutableList.of(CryptoStreams.base64(digest)));
assertEquals(headers.get("x-Content-MD5"), ImmutableList.of(base64(digest)));
payload.release();
} finally {
if (os != null)
os.close();
closeQuietly(os);
if (f != null && f.exists())
f.delete();
}
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPost() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
public void testPost() {
assertEquals(client.post("", "foo").trim(), "fooPOST");
}
@Test(invocationCount = 1, timeOut = 5000)
public void testPostAsInputStream() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testPostAsInputStream() {
AtomicInteger postFailures = new AtomicInteger();
for (int i = 0; i < 5; i++)
try {
@ -221,19 +190,17 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
} catch (Exception e) {
postFailures.incrementAndGet();
}
assert postFailures.get() > 0;
assertTrue(postFailures.get() > 0, "expected failures");
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPostBinder() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testPostBinder() {
assertEquals(client.postJson("", "foo").trim(), "{\"key\":\"foo\"}POST");
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPostContentDisposition() throws ExecutionException, InterruptedException, TimeoutException,
IOException {
Payload payload = Payloads.newStringPayload("foo");
public void testPostContentDisposition() {
Payload payload = newStringPayload("foo");
payload.getContentMetadata().setContentDisposition("attachment; filename=photo.jpg");
Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
assertEquals(headers.get("x-Content-Disposition"), ImmutableList.of("attachment; filename=photo.jpg"));
@ -241,8 +208,8 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPostContentEncoding() throws ExecutionException, InterruptedException, TimeoutException, IOException {
Payload payload = Payloads.newStringPayload("foo");
public void testPostContentEncoding() {
Payload payload = newStringPayload("foo");
payload.getContentMetadata().setContentEncoding("gzip");
Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
assertEquals(headers.get("x-Content-Encoding"), ImmutableList.of("gzip"));
@ -250,8 +217,8 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPostContentLanguage() throws ExecutionException, InterruptedException, TimeoutException, IOException {
Payload payload = Payloads.newStringPayload("foo");
public void testPostContentLanguage() {
Payload payload = newStringPayload("foo");
payload.getContentMetadata().setContentLanguage("mi, en");
Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
assertEquals(headers.get("x-Content-Language"), ImmutableList.of("mi, en"));
@ -259,24 +226,22 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPut() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
public void testPut() {
assertEquals(client.upload("", "foo").trim(), "fooPUT");
}
@Test(invocationCount = 5, timeOut = 5000)
public void testPutRedirect() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testPutRedirect() {
assertEquals(client.upload("redirect", "foo").trim(), "fooPUTREDIRECT");
}
@Test(invocationCount = 5, timeOut = 5000)
public void testHead() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
assert client.exists("");
public void testHead() {
assertTrue(client.exists(""), "head returned false");
}
@Test(invocationCount = 5, timeOut = 5000)
public void testGetAndParseSax() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetAndParseSax() {
assertEquals(client.downloadAndParse(""), "whoppers");
}
}

View File

@ -18,21 +18,28 @@
*/
package org.jclouds.http;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.base.Throwables.getStackTraceAsString;
import static com.google.common.io.ByteStreams.copy;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Closeables.closeQuietly;
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
import static javax.servlet.http.HttpServletResponse.SC_OK;
import static javax.ws.rs.core.HttpHeaders.CONTENT_ENCODING;
import static javax.ws.rs.core.HttpHeaders.CONTENT_LANGUAGE;
import static javax.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME;
import static org.jclouds.Constants.PROPERTY_TRUST_ALL_CERTS;
import static org.jclouds.crypto.CryptoStreams.md5Base64;
import static org.jclouds.util.Strings2.toStringAndClose;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -41,7 +48,6 @@ import java.util.zip.GZIPInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.HttpHeaders;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
@ -50,19 +56,18 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.jclouds.Constants;
import org.jclouds.ContextBuilder;
import org.jclouds.crypto.CryptoStreams;
import org.jclouds.io.InputSuppliers;
import org.jclouds.providers.AnonymousProviderMetadata;
import org.jclouds.rest.RestContext;
import org.jclouds.util.Strings2;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import com.google.common.base.Throwables;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
@ -86,40 +91,36 @@ public abstract class BaseJettyTest {
static final Pattern actionPattern = Pattern.compile("/objects/(.*)/action/([a-z]*);?(.*)");
@BeforeTest
@Parameters( { "test-jetty-port" })
@Parameters({ "test-jetty-port" })
public void setUpJetty(@Optional("8123") final int testPort) throws Exception {
this.testPort = testPort;
final InputSupplier<InputStream> oneHundredOneConstitutions = getTestDataSupplier();
md5 = CryptoStreams.md5Base64(oneHundredOneConstitutions);
md5 = md5Base64(oneHundredOneConstitutions);
Handler server1Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
throws IOException, ServletException {
InputStream body = request.getInputStream();
try {
if (failIfNoContentLength(request, response)) {
return;
} else if (target.indexOf("sleep") > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
propagate(e);
}
sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
} else if (target.indexOf("redirect") > 0) {
response.sendRedirect("https://localhost:" + (testPort + 1) + "/");
} else if (target.indexOf("101constitutions") > 0) {
response.setContentType("text/plain");
response.setHeader("Content-MD5", md5);
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
copy(oneHundredOneConstitutions, response.getOutputStream());
} else if (request.getMethod().equals("PUT")) {
if (request.getContentLength() > 0) {
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(Strings2.toStringAndClose(body) + "PUT");
response.setStatus(SC_OK);
response.getWriter().println(toStringAndClose(body) + "PUT");
} else {
response.sendError(500, "no content");
}
@ -138,26 +139,24 @@ public abstract class BaseJettyTest {
response.sendError(404, "no content");
} else if (request.getHeader("test") != null) {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
response.getWriter().println("test");
} else if (request.getMethod().equals("HEAD")) {
/*
* NOTE: by HTML specification, HEAD response MUST NOT include a body
*/
// by HTML specification, HEAD response MUST NOT include a
// body
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
} else {
if (failEveryTenRequests(request, response))
return;
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
response.getWriter().println(XML);
}
((Request) request).setHandled(true);
} catch (IOException e) {
if (body != null)
closeQuietly(body);
response.sendError(500, Throwables.getStackTraceAsString(e));
closeQuietly(body);
response.sendError(500, getStackTraceAsString(e));
}
}
@ -184,44 +183,41 @@ public abstract class BaseJettyTest {
if (request.getHeader("Content-MD5") != null) {
String expectedMd5 = request.getHeader("Content-MD5");
String realMd5FromRequest;
realMd5FromRequest = CryptoStreams.md5Base64(InputSuppliers.of(body));
realMd5FromRequest = md5Base64(InputSuppliers.of(body));
boolean matched = expectedMd5.equals(realMd5FromRequest);
if (matched) {
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
response.addHeader("x-Content-MD5", realMd5FromRequest);
} else {
response.sendError(500, "didn't match");
}
} else {
String responseString = (request.getContentLength() < 10240) ? Strings2.toStringAndClose(body) + "POST"
: "POST";
String responseString = (request.getContentLength() < 10240) ? toStringAndClose(body) + "POST" : "POST";
body = null;
for (String header : new String[] { "Content-Disposition", HttpHeaders.CONTENT_LANGUAGE,
HttpHeaders.CONTENT_ENCODING })
for (String header : new String[] { "Content-Disposition", CONTENT_LANGUAGE, CONTENT_ENCODING })
if (request.getHeader(header) != null) {
response.addHeader("x-" + header, request.getHeader(header));
}
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
response.getWriter().println(responseString);
}
} catch (IOException e) {
if (body != null)
closeQuietly(body);
response.sendError(500, Throwables.getStackTraceAsString(e));
closeQuietly(body);
response.sendError(500, getStackTraceAsString(e));
}
}
protected void setupAndStartSSLServer(final int testPort) throws Exception {
Handler server2Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
throws IOException, ServletException {
InputStream body = request.getInputStream();
try {
if (request.getMethod().equals("PUT")) {
String text = Strings2.toStringAndClose(body);
String text = toStringAndClose(body);
body = null;
if (request.getContentLength() > 0) {
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
response.getWriter().println(text + "PUTREDIRECT");
}
} else if (request.getMethod().equals("POST")) {
@ -231,21 +227,19 @@ public abstract class BaseJettyTest {
handleAction(request, response);
}
} else if (request.getMethod().equals("HEAD")) {
/*
* NOTE: by HTML specification, HEAD response MUST NOT include a body
*/
// by HTML specification, HEAD response MUST NOT include a
// body
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
} else {
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.setStatus(SC_OK);
response.getWriter().println(XML2);
}
((Request) request).setHandled(true);
} catch (IOException e) {
if (body != null)
closeQuietly(body);
response.sendError(500, Throwables.getStackTraceAsString(e));
closeQuietly(body);
response.sendError(500, getStackTraceAsString(e));
}
}
};
@ -262,8 +256,8 @@ public abstract class BaseJettyTest {
ssl.setTrustStore("src/test/resources/test.jks");
ssl.setTrustStorePassword("jclouds");
server2.setConnectors(new Connector[]{ ssl_connector });
server2.setConnectors(new Connector[] { ssl_connector });
server2.start();
}
@ -281,18 +275,18 @@ public abstract class BaseJettyTest {
}
public static ContextBuilder newBuilder(int testPort, Properties properties, Module... connectionModules) {
properties.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
properties.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
return ContextBuilder.newBuilder(
AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(IntegrationTestClient.class, IntegrationTestAsyncClient.class,
"http://localhost:" + testPort))
.modules(ImmutableSet.<Module> copyOf(connectionModules))
.overrides(properties);
properties.setProperty(PROPERTY_TRUST_ALL_CERTS, "true");
properties.setProperty(PROPERTY_RELAX_HOSTNAME, "true");
return ContextBuilder
.newBuilder(
AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint(IntegrationTestClient.class,
IntegrationTestAsyncClient.class, "http://localhost:" + testPort))
.modules(ImmutableSet.<Module> copyOf(connectionModules)).overrides(properties);
}
@AfterTest
public void tearDownJetty() throws Exception {
context.close();
closeQuietly(context);
if (server2 != null)
server2.stop();
server.stop();
@ -320,7 +314,7 @@ public abstract class BaseJettyTest {
}
protected boolean redirectEveryTwentyRequests(HttpServletRequest request, HttpServletResponse response)
throws IOException {
throws IOException {
if (cycle.incrementAndGet() % 20 == 0) {
response.sendRedirect("http://localhost:" + (testPort + 1) + "/");
((Request) request).setHandled(true);
@ -355,18 +349,12 @@ public abstract class BaseJettyTest {
if (matchFound) {
String objectId = matcher.group(1);
String action = matcher.group(2);
Map<String, String> options = newHashMap();
Builder<String, String> options = ImmutableMap.<String, String> builder();
if (matcher.groupCount() == 3) {
String optionsGroup = matcher.group(3);
for (String entry : optionsGroup.split(";")) {
if (entry.indexOf('=') >= 0) {
String[] keyValue = entry.split("=");
options.put(keyValue[0], keyValue[1]);
}
}
options.putAll(Splitter.on(';').withKeyValueSeparator("=").split(matcher.group(3)));
}
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(objectId + "->" + action + ":" + options);
response.setStatus(SC_OK);
response.getWriter().println(objectId + "->" + action + ":" + options.build());
} else {
response.sendError(500, "no content");
}

View File

@ -25,14 +25,11 @@ import static org.jclouds.Constants.PROPERTY_MAX_CONNECTIONS_PER_HOST;
import static org.jclouds.Constants.PROPERTY_SO_TIMEOUT;
import static org.jclouds.Constants.PROPERTY_USER_THREADS;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import org.jclouds.http.BaseHttpCommandExecutorServiceIntegrationTest;
import org.jclouds.http.apachehc.config.ApacheHCHttpCommandExecutorServiceModule;
import org.testng.annotations.Test;
import org.testng.SkipException;
import com.google.inject.Module;
@ -61,25 +58,18 @@ public class ApacheHCHttpCommandExecutorServiceTestDisabled extends BaseHttpComm
}
@Override
@Test(enabled = false)
public void testPostContentDisposition() throws ExecutionException, InterruptedException, TimeoutException,
IOException {
// TODO: currently times out, see issue
// http://code.google.com/p/jclouds/issues/detail?id=353
public void testPostContentDisposition() {
throw new SkipException("http://code.google.com/p/jclouds/issues/detail?id=353");
}
@Override
@Test(enabled = false)
public void testPostContentEncoding() throws ExecutionException, InterruptedException, TimeoutException, IOException {
// TODO: currently times out, see issue
// http://code.google.com/p/jclouds/issues/detail?id=353
public void testPostContentEncoding() {
throw new SkipException("http://code.google.com/p/jclouds/issues/detail?id=353");
}
@Override
@Test(enabled = false)
public void testPostContentLanguage() throws ExecutionException, InterruptedException, TimeoutException, IOException {
// TODO: currently times out, see issue
// http://code.google.com/p/jclouds/issues/detail?id=353
public void testPostContentLanguage() {
throw new SkipException("http://code.google.com/p/jclouds/issues/detail?id=353");
}
}

View File

@ -18,22 +18,12 @@
*/
package org.jclouds.gae;
import static org.jclouds.concurrent.FutureIterables.awaitCompletion;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jclouds.concurrent.Futures;
import org.jclouds.concurrent.MoreExecutors;
import org.jclouds.concurrent.SingleThreaded;
import org.jclouds.concurrent.config.ConfiguresExecutorService;
import org.jclouds.gae.config.GoogleAppEngineConfigurationModule;
@ -41,18 +31,12 @@ import org.jclouds.http.BaseHttpCommandExecutorServiceIntegrationTest;
import org.jclouds.http.HttpCommandExecutorService;
import org.jclouds.http.config.ConfiguresHttpCommandExecutorService;
import org.jclouds.logging.Logger;
import org.jclouds.util.Strings2;
import org.testng.SkipException;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Injector;
import com.google.inject.Module;
@ -69,129 +53,21 @@ public class AsyncGaeHttpCommandExecutorServiceIntegrationTest extends BaseHttpC
@Override
protected void setupAndStartSSLServer(final int testPort) throws Exception {
}
@Override
protected boolean redirectEveryTwentyRequests(HttpServletRequest request, HttpServletResponse response)
throws IOException {
return false;
}
@Test(enabled = false)
public void testPerformanceVsNothing() throws TimeoutException {
setupApiProxy();
int count = 5;
final URI fetch = URI.create("http://www.google.com");
final URLFetchService service = URLFetchServiceFactory.getURLFetchService();
Results gae = getTest(count, "gae", new Supplier<ListenableFuture<?>>() {
@Override
public ListenableFuture<?> get() {
try {
return Futures.makeListenable(service.fetchAsync(fetch.toURL()), MoreExecutors.sameThreadExecutor());
} catch (MalformedURLException e) {
throw Throwables.propagate(e);
}
}
}, new Consumer() {
@SuppressWarnings("unchecked")
@Override
public void consume(Object in) {
try {
new String(((ListenableFuture<com.google.appengine.api.urlfetch.HTTPResponse>) in).get().getContent());
} catch (InterruptedException e) {
Throwables.propagate(e);
} catch (ExecutionException e) {
Throwables.propagate(e);
}
}
});
Results jclouds = getTest(count, "jclouds", new Supplier<ListenableFuture<?>>() {
@Override
public ListenableFuture<?> get() {
return AsyncGaeHttpCommandExecutorServiceIntegrationTest.this.context.utils().asyncHttp().get(fetch);
}
}, new Consumer() {
@SuppressWarnings("unchecked")
@Override
public void consume(Object in) {
try {
Strings2.toStringAndClose(((ListenableFuture<InputStream>) in).get());
} catch (InterruptedException e) {
Throwables.propagate(e);
} catch (ExecutionException e) {
Throwables.propagate(e);
} catch (IOException e) {
Throwables.propagate(e);
}
}
});
System.err.println(jclouds + " " + gae);
long overhead = 10;
assert jclouds.createFutures <= gae.createFutures + overhead : jclouds + " " + gae;
assert jclouds.futuresReady <= gae.futuresReady + overhead : jclouds + " " + gae;
assert jclouds.futuresConsumed <= gae.futuresConsumed + overhead : jclouds + " " + gae;
}
interface Consumer {
void consume(Object in);
}
class Results {
public long createFutures;
public long futuresReady;
public long futuresConsumed;
public int count;
public String who;
@Override
public String toString() {
return "[count=" + count + ", createFutures=" + createFutures + ", futuresConsumed=" + futuresConsumed
+ ", futuresReady=" + futuresReady + ", who=" + who + "]";
}
}
private Results getTest(int count, String who, Supplier<ListenableFuture<?>> getSupplier, Consumer consumer)
throws TimeoutException {
Results results = new Results();
results.count = count;
results.who = who;
Map<String, ListenableFuture<?>> responses = Maps.newConcurrentMap();
long start = System.currentTimeMillis();
for (int i = 0; i < count; i++)
responses.put(i + "", getSupplier.get());
results.createFutures = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
Map<String, Exception> exceptions = awaitCompletion(responses, MoreExecutors.sameThreadExecutor(), null, logger,
who);
results.futuresReady = System.currentTimeMillis() - start;
assert exceptions.size() == 0 : exceptions;
start = System.currentTimeMillis();
for (ListenableFuture<?> value : responses.values())
consumer.consume(value);
results.futuresConsumed = System.currentTimeMillis() - start;
return results;
}
@Override
@Test(enabled = false)
public void testPostAsInputStream() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
// streams aren't supported
public void testPostAsInputStream() {
throw new SkipException("streams aren't supported");
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testPostBinder() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testPostBinder() {
setupApiProxy();
super.testPostBinder();
}
@ -204,107 +80,87 @@ public class AsyncGaeHttpCommandExecutorServiceIntegrationTest extends BaseHttpC
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testGetAndParseSax() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetAndParseSax() {
setupApiProxy();
super.testGetAndParseSax();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testGetString() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
public void testGetString() {
setupApiProxy();
super.testGetString();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000, dataProvider = "gets")
public void testGetStringSynch(String path) throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetStringSynch(String path) {
setupApiProxy();
super.testGetStringSynch(path);
}
// TODO: determine how to get redirects to operate
@Override
@Test(enabled = false)
public void testGetStringRedirect() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
setupApiProxy();
super.testGetStringRedirect();
public void testGetStringRedirect() {
throw new SkipException("need to get redirects to operate");
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testGetException() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetException() {
setupApiProxy();
super.testGetException();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testGetStringPermanentRedirect() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
setupApiProxy();
super.testGetStringPermanentRedirect();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testGetSynchException() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetSynchException() {
setupApiProxy();
super.testGetSynchException();
}
@Override
@Test(enabled = false)
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testPost() {
setupApiProxy();
super.testPost();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testPut() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
public void testPut() {
setupApiProxy();
super.testPut();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testGetStringViaRequest() throws ExecutionException, InterruptedException, TimeoutException, IOException {
public void testGetStringViaRequest() throws IOException {
setupApiProxy();
super.testGetStringViaRequest();
}
// TODO: determine how to get redirects to operate
@Override
@Test(enabled = false)
public void testPutRedirect() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
setupApiProxy();
super.testPutRedirect();
public void testPutRedirect() {
throw new SkipException("need to get redirects to operate");
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testGetStringWithHeader() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testGetStringWithHeader() {
setupApiProxy();
super.testGetStringWithHeader();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testHead() throws MalformedURLException, ExecutionException, InterruptedException, TimeoutException {
public void testHead() {
setupApiProxy();
super.testHead();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testRequestFilter() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testRequestFilter() {
setupApiProxy();
super.testRequestFilter();
}
@ -334,36 +190,31 @@ public class AsyncGaeHttpCommandExecutorServiceIntegrationTest extends BaseHttpC
}
@Override
@Test(enabled = true)
public void testGetBigFile() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
// disabled since test data is too big
public void testGetBigFile() {
throw new SkipException("test data is too big for GAE");
}
@Override
@Test(enabled = true)
public void testUploadBigFile() throws IOException {
// disabled since test data is too big
throw new SkipException("test data is too big for GAE");
}
@Override
@Test(enabled = false)
public void testPostContentDisposition() throws ExecutionException, InterruptedException, TimeoutException,
IOException {
public void testPostContentDisposition() {
setupApiProxy();
super.testPostContentDisposition();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testPostContentEncoding() throws ExecutionException, InterruptedException, TimeoutException, IOException {
public void testPostContentEncoding() {
setupApiProxy();
super.testPostContentEncoding();
}
@Override
@Test(enabled = true, invocationCount = 5, timeOut = 3000)
public void testPostContentLanguage() throws ExecutionException, InterruptedException, TimeoutException, IOException {
public void testPostContentLanguage() {
setupApiProxy();
super.testPostContentLanguage();
}
@ -371,8 +222,7 @@ public class AsyncGaeHttpCommandExecutorServiceIntegrationTest extends BaseHttpC
// http://code.google.com/p/googleappengine/issues/detail?id=3599
@Override
@Test(enabled = true, expectedExceptions = IllegalArgumentException.class)
public void testAlternateMethod() throws MalformedURLException, ExecutionException, InterruptedException,
TimeoutException {
public void testAlternateMethod() {
setupApiProxy();
super.testAlternateMethod();
}