Merge branch 'master' of github.com:jclouds/jclouds

* 'master' of github.com:jclouds/jclouds:
  update to latest surefire
  cleaned exception handling
  attempt to stabalize tests on build server
  [issue 535] Moved LICENSE.txt to META-INF in the resource bundle
This commit is contained in:
Adrian Cole 2011-04-21 09:33:39 -07:00
commit 1322a85962
6 changed files with 140 additions and 112 deletions

View File

@ -24,6 +24,7 @@ import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Iterables.getLast;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Closeables.closeQuietly;
import static org.jclouds.io.Payloads.newInputStreamPayload;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@ -60,12 +61,10 @@ import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.io.MutableContentMetadata;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.logging.Logger;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.io.CountingOutputStream;
@ -130,7 +129,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
headerBuilder.putAll(header, connection.getHeaderFields().get(header));
}
ImmutableMultimap<String, String> headers = headerBuilder.build();
Payload payload = in != null ? Payloads.newInputStreamPayload(in) : null;
Payload payload = in != null ? newInputStreamPayload(in) : null;
if (payload != null) {
payload.getContentMetadata().setPropertiesFromHttpHeaders(headers);
builder.payload(payload);
@ -202,7 +201,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
methodField.set(connection, request.getMethod());
} catch (Exception e1) {
logger.error(e, "could not set request method: ", request.getMethod());
Throwables.propagate(e1);
propagate(e1);
}
}
@ -236,7 +235,7 @@ public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorSe
"JDK 1.6 does not support >2GB chunks. Use chunked encoding, if possible.");
connection.setFixedLengthStreamingMode(length.intValue());
if (length.intValue() > 0) {
connection.setRequestProperty("Expect", "100-continue");
connection.setRequestProperty("Expect", "100-continue");
}
}
CountingOutputStream out = new CountingOutputStream(connection.getOutputStream());

View File

@ -63,6 +63,7 @@ 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.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
@ -86,7 +87,7 @@ 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;
@ -96,62 +97,69 @@ public abstract class BaseJettyTest {
Handler server1Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
if (failIfNoContentLength(request, response)) {
return;
} else if (target.indexOf("sleep") > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
propagate(e);
}
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.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);
copy(oneHundredOneConstitutions.getInput(), response.getOutputStream());
} else if (request.getMethod().equals("PUT")) {
if (request.getContentLength() > 0) {
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);
}
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.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);
copy(oneHundredOneConstitutions.getInput(), response.getOutputStream());
} else if (request.getMethod().equals("PUT")) {
if (request.getContentLength() > 0) {
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(Strings2.toStringAndClose(body) + "PUT");
} else {
response.sendError(500, "no content");
}
} else if (request.getMethod().equals("POST")) {
// don't redirect large objects
if (request.getContentLength() < 10240 && redirectEveryTwentyRequests(request, response))
return;
if (failEveryTenRequests(request, response))
return;
if (request.getContentLength() > 0) {
handlePost(request, response);
} else {
handleAction(request, response);
}
} else if (request.getHeader("range") != null) {
response.sendError(404, "no content");
} else if (request.getHeader("test") != null) {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("test");
} else if (request.getMethod().equals("HEAD")) {
/*
* NOTE: by HTML specification, HEAD response MUST NOT include a body
*/
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(Strings2.toStringAndClose(request.getInputStream()) + "PUT");
} else {
response.sendError(500, "no content");
if (failEveryTenRequests(request, response))
return;
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(XML);
}
} else if (request.getMethod().equals("POST")) {
// don't redirect large objects
if (request.getContentLength() < 10240 && redirectEveryTwentyRequests(request, response))
return;
if (failEveryTenRequests(request, response))
return;
if (request.getContentLength() > 0) {
handlePost(request, response);
} else {
handleAction(request, response);
}
} else if (request.getHeader("range") != null) {
response.sendError(404, "no content");
} else if (request.getHeader("test") != null) {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("test");
} else if (request.getMethod().equals("HEAD")) {
/*
* NOTE: by HTML specification, HEAD response MUST NOT include a body
*/
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
} else {
if (failEveryTenRequests(request, response))
return;
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(XML);
((Request) request).setHandled(true);
} catch (IOException e) {
if (body != null)
closeQuietly(body);
response.sendError(500, Throwables.getStackTraceAsString(e));
}
((Request) request).setHandled(true);
}
};
@ -172,11 +180,12 @@ public abstract class BaseJettyTest {
}
private static void handlePost(HttpServletRequest request, HttpServletResponse response) throws IOException {
InputStream body = request.getInputStream();
try {
if (request.getHeader("Content-MD5") != null) {
String expectedMd5 = request.getHeader("Content-MD5");
String realMd5FromRequest;
realMd5FromRequest = CryptoStreams.md5Base64(InputSuppliers.of(request.getInputStream()));
realMd5FromRequest = CryptoStreams.md5Base64(InputSuppliers.of(body));
boolean matched = expectedMd5.equals(realMd5FromRequest);
if (matched) {
response.setStatus(HttpServletResponse.SC_OK);
@ -185,52 +194,60 @@ public abstract class BaseJettyTest {
response.sendError(500, "didn't match");
}
} else {
String responseString = (request.getContentLength() < 10240) ? Strings2.toStringAndClose(body) + "POST"
: "POST";
body = null;
for (String header : new String[] { "Content-Disposition", HttpHeaders.CONTENT_LANGUAGE,
HttpHeaders.CONTENT_ENCODING })
HttpHeaders.CONTENT_ENCODING })
if (request.getHeader(header) != null) {
response.addHeader("x-" + header, request.getHeader(header));
}
response.setStatus(HttpServletResponse.SC_OK);
String responseString = "POST";
if (request.getContentLength() < 10240) {
responseString = Strings2.toStringAndClose(request.getInputStream()) + "POST";
} else {
closeQuietly(request.getInputStream());
}
response.getWriter().println(responseString);
}
} catch (IOException e) {
response.sendError(500, e.toString());
if (body != null)
closeQuietly(body);
response.sendError(500, Throwables.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 {
if (request.getMethod().equals("PUT")) {
if (request.getContentLength() > 0) {
throws IOException, ServletException {
InputStream body = request.getInputStream();
try {
if (request.getMethod().equals("PUT")) {
String text = Strings2.toStringAndClose(body);
body = null;
if (request.getContentLength() > 0) {
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(text + "PUTREDIRECT");
}
} else if (request.getMethod().equals("POST")) {
if (request.getContentLength() > 0) {
handlePost(request, response);
} else {
handleAction(request, response);
}
} else if (request.getMethod().equals("HEAD")) {
/*
* NOTE: by HTML specification, HEAD response MUST NOT include a body
*/
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(Strings2.toStringAndClose(request.getInputStream()) + "PUTREDIRECT");
}
} else if (request.getMethod().equals("POST")) {
if (request.getContentLength() > 0) {
handlePost(request, response);
} else {
handleAction(request, response);
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(XML2);
}
} else if (request.getMethod().equals("HEAD")) {
/*
* NOTE: by HTML specification, HEAD response MUST NOT include a body
*/
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
} else {
response.setContentType("text/xml");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println(XML2);
((Request) request).setHandled(true);
} catch (IOException e) {
if (body != null)
closeQuietly(body);
response.sendError(500, Throwables.getStackTraceAsString(e));
}
((Request) request).setHandled(true);
}
};
@ -261,12 +278,12 @@ public abstract class BaseJettyTest {
}
public static RestContextBuilder<IntegrationTestClient, IntegrationTestAsyncClient> newBuilder(int testPort,
Properties properties, Module... connectionModules) {
Properties properties, Module... connectionModules) {
properties.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
properties.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
RestContextSpec<IntegrationTestClient, IntegrationTestAsyncClient> contextSpec = contextSpec("test",
"http://localhost:" + testPort, "1", "", "identity", null, IntegrationTestClient.class,
IntegrationTestAsyncClient.class, ImmutableSet.<Module> copyOf(connectionModules));
"http://localhost:" + testPort, "1", "", "identity", null, IntegrationTestClient.class,
IntegrationTestAsyncClient.class, ImmutableSet.<Module> copyOf(connectionModules));
return createContextBuilder(contextSpec, properties);
}
@ -300,7 +317,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);

View File

@ -61,37 +61,39 @@ public class BackoffLimitedRetryHandlerTest {
@Test
void testExponentialBackoffDelayDefaultMaxInterval500() throws InterruptedException {
long acceptableDelay = 25; // Delay to forgive if tests run long.
long period = 100;
long acceptableDelay = period - 1;
long startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(1, "TEST FAILURE: 1");
handler.imposeBackoffExponentialDelay(period, 2, 1, 5, "TEST FAILURE: 1");
long elapsedTime = (System.nanoTime() - startTime) / 1000000;
assert (elapsedTime >= 49) : elapsedTime;
assertTrue(elapsedTime < 50 + acceptableDelay);
assert (elapsedTime >= period - 1) : elapsedTime;
assertTrue(elapsedTime < period + acceptableDelay);
startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(2, "TEST FAILURE: 2");
handler.imposeBackoffExponentialDelay(period, 2, 2, 5, "TEST FAILURE: 2");
elapsedTime = (System.nanoTime() - startTime) / 1000000;
assert (elapsedTime >= 199) : elapsedTime;
assertTrue(elapsedTime < 200 + acceptableDelay);
assert (elapsedTime >= period * 4 - 1) : elapsedTime;
assertTrue(elapsedTime < period * 9);
startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(3, "TEST FAILURE: 3");
handler.imposeBackoffExponentialDelay(period, 2, 3, 5, "TEST FAILURE: 3");
elapsedTime = (System.nanoTime() - startTime) / 1000000;
assert (elapsedTime >= 449) : elapsedTime;
assertTrue(elapsedTime < 450 + acceptableDelay);
assert (elapsedTime >= period * 9 - 1) : elapsedTime;
assertTrue(elapsedTime < period * 10);
startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(4, "TEST FAILURE: 4");
handler.imposeBackoffExponentialDelay(period, 2, 4, 5, "TEST FAILURE: 4");
elapsedTime = (System.nanoTime() - startTime) / 1000000;
assert (elapsedTime >= 499) : elapsedTime;
assertTrue(elapsedTime < 550 + acceptableDelay * 2);
assert (elapsedTime >= period * 10 - 1) : elapsedTime;
assertTrue(elapsedTime < period * 11);
startTime = System.nanoTime();
handler.imposeBackoffExponentialDelay(5, "TEST FAILURE: 5");
handler.imposeBackoffExponentialDelay(period, 2, 5, 5, "TEST FAILURE: 5");
elapsedTime = (System.nanoTime() - startTime) / 1000000;
assert (elapsedTime >= 499) : elapsedTime;
assertTrue(elapsedTime < 550 + acceptableDelay * 2);
assert (elapsedTime >= period * 10 - 1) : elapsedTime;
assertTrue(elapsedTime < period * 11);
}
TransformingHttpCommandExecutorServiceImpl executorService;

View File

@ -36,7 +36,7 @@ import com.google.common.base.Supplier;
*/
@Test(groups = "unit", sequential = true)
public class RetryablePredicateTest {
public static int SLOW_BUILD_SERVER_GRACE = 50;
public static int SLOW_BUILD_SERVER_GRACE = 100;
@Test
void testFalseOnIllegalStateExeception() {

View File

@ -283,7 +283,14 @@
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.8</version>
<version>2.8.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>integration</id>

View File

@ -36,6 +36,7 @@
<build>
<resources>
<resource>
<targetPath>META-INF</targetPath>
<directory>${project.basedir}</directory>
<includes>
<include>LICENSE.txt</include>
@ -46,16 +47,18 @@
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.2</version>
<executions>
<executions>
<execution>
<id>bundle-remote-resources</id>
<goals>
<goal>bundle</goal>
</goals>
<!-- run *after* copying resources to the output directory -->
<phase>process-resources</phase>
<configuration>
<resourcesDirectory>${project.basedir}</resourcesDirectory>
<resourcesDirectory>${project.build.outputDirectory}</resourcesDirectory>
<includes>
<include>LICENSE.txt</include>
<include>META-INF/LICENSE.txt</include>
</includes>
</configuration>
</execution>