use of org.commonjava.test.http.expect.ExpectationHandler in project galley by Commonjava.
the class DownloadHandlerConcurrencyTest method concurrentDownloadWaitsThenUsesFirstResult_SmallFile.
@BMRules(rules = { @BMRule(name = "init rendezvous", targetClass = "DownloadHandler", targetMethod = "<init>", targetLocation = "ENTRY", action = "debug(\"Creating rendezvous\"); createRendezvous(\"tsync\", 4);"), @BMRule(name = "rendezvous threads", targetClass = "DownloadHandler", targetMethod = "joinOrStart", targetLocation = "ENTRY", action = "debug(\"waiting for rendezvous\"); rendezvous(\"tsync\"); debug(\"proceeding\");") })
@Test
public void concurrentDownloadWaitsThenUsesFirstResult_SmallFile() throws Exception {
final NotFoundCache nfc = new MemoryNotFoundCache();
final TransportManagerConfig mgrConfig = new TransportManagerConfig();
handler = new DownloadHandler(nfc, mgrConfig, executor);
// NOTE: Coordinate with "init" @BMRule above!
final int threads = 4;
final int timeoutSeconds = 10;
final String content = "this is a test " + System.currentTimeMillis();
final String base = "repo";
final String path = "this/is/the/path.txt";
final String baseurl = server.formatUrl(base);
// Serve the content EXACTLY ONCE. It should be cached / cache should be used after that.
server.expect("GET", server.formatUrl(base, path), new ExpectationHandler() {
private boolean sent = false;
@Override
public void handle(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
if (!sent) {
resp.setStatus(HttpServletResponse.SC_OK);
resp.getWriter().write(content);
sent = true;
} else {
throw new ServletException("Cannot write content more than once.");
}
}
});
final ConcreteResource resource = new ConcreteResource(new SimpleLocation(base, baseurl), path);
final Transfer target = cacheProvider.getTransfer(resource);
final CountDownLatch latch = new CountDownLatch(threads);
final AtomicInteger successes = new AtomicInteger(0);
final Logger logger = LoggerFactory.getLogger(getClass());
// start threads, then wait for each to complete, and
for (int i = 0; i < threads; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
String oldName = Thread.currentThread().getName();
try {
Thread.currentThread().setName("Download - " + oldName);
Transfer result = handler.download(resource, target, timeoutSeconds, transport, false, new EventMetadata());
try (InputStream in = result.openInputStream()) {
String resultContent = IOUtils.toString(in);
if (resultContent.equals(content)) {
successes.incrementAndGet();
} else {
logger.error("Expected content: '{}'\nActual content: '{}'", content, resultContent);
}
} catch (IOException e) {
logger.error("Failed to read transfer: " + e.getMessage(), e);
}
} catch (TransferException e) {
logger.error("Failed to retrieve: " + e.getMessage(), e);
} finally {
latch.countDown();
Thread.currentThread().setName(oldName);
}
}
});
}
}
use of org.commonjava.test.http.expect.ExpectationHandler in project galley by Commonjava.
the class HttpDownloadTest method IOExceptionDuringDownloadTransferDeletesTargetFile.
@Test
@BMRule(name = "throw IOException during writeTarget copy operation", targetClass = "HttpDownload", targetMethod = "doCopy", targetLocation = "ENTRY", condition = "!flagged(\"firstCopy\")", action = "flag(\"firstCopy\"); throw new IOException(\"BMUnit exception\");")
public void IOExceptionDuringDownloadTransferDeletesTargetFile() throws Exception {
final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
final String path = "/path/to/file";
fixture.getServer().expect("GET", fixture.formatUrl(path), new ExpectationHandler() {
int count = 0;
@Override
public void handle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setStatus(200);
httpServletResponse.setHeader("Content-Length", Integer.toString(content.length()));
PrintWriter writer = httpServletResponse.getWriter();
if (count < 1) {
writer.write(content.substring(0, content.length() / 2));
} else {
writer.write(content);
}
count++;
}
});
final String baseUri = fixture.getBaseUri();
final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, path));
final String url = fixture.formatUrl(path);
assertThat(transfer.exists(), equalTo(false));
// first call, server should quit transferring halfway through the transfer
HttpDownload dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
DownloadJob resultJob = dl.call();
TransferException error = dl.getError();
assertThat(error, notNullValue());
error.printStackTrace();
assertThat(resultJob, notNullValue());
Transfer result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(false));
assertThat(transfer.exists(), equalTo(false));
// second call should hit upstream again and succeed.
dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
resultJob = dl.call();
error = dl.getError();
assertThat(error, nullValue());
assertThat(resultJob, notNullValue());
result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(true));
assertThat(transfer.exists(), equalTo(true));
final String urlPath = fixture.getUrlPath(url);
assertThat(fixture.getAccessesFor(urlPath), equalTo(2));
}
use of org.commonjava.test.http.expect.ExpectationHandler in project galley by Commonjava.
the class HttpDownloadTest method simpleRetriveOfRedirectUrl.
@Test
public void simpleRetriveOfRedirectUrl() throws Exception {
final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
final String redirectPath = "/path/to/file";
final String path = "/redirect/to/file";
fixture.getServer().expect("GET", fixture.formatUrl(path), new ExpectationHandler() {
@Override
public void handle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setStatus(302);
httpServletResponse.setHeader("Location", fixture.formatUrl(redirectPath));
}
});
fixture.getServer().expect("GET", fixture.formatUrl(redirectPath), 200, content);
final String baseUri = fixture.getBaseUri();
final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, path));
final String url = fixture.formatUrl(path);
Map<Transfer, Long> transferSizes = new HashMap<Transfer, Long>();
assertThat(transfer.exists(), equalTo(false));
final HttpDownload dl = new HttpDownload(url, location, transfer, transferSizes, new EventMetadata(), fixture.getHttp(), new ObjectMapper());
final DownloadJob resultJob = dl.call();
final TransferException error = dl.getError();
assertThat(error, nullValue());
assertThat(resultJob, notNullValue());
final Transfer result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(true));
assertThat(transfer.exists(), equalTo(true));
final String postPath = fixture.getUrlPath(url);
assertThat(fixture.getAccessesFor(postPath), equalTo(1));
}
use of org.commonjava.test.http.expect.ExpectationHandler in project galley by Commonjava.
the class HttpDownloadTest method partialDownloadDeletesTargetFile.
@Test
public void partialDownloadDeletesTargetFile() throws Exception {
final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
final String path = "/path/to/file";
fixture.getServer().expect("GET", fixture.formatUrl(path), new ExpectationHandler() {
int count = 0;
@Override
public void handle(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws ServletException, IOException {
httpServletResponse.setStatus(200);
httpServletResponse.setHeader("Content-Length", Integer.toString(content.length()));
PrintWriter writer = httpServletResponse.getWriter();
if (count < 1) {
writer.write(content.substring(0, content.length() / 2));
} else {
writer.write(content);
}
count++;
}
});
final String baseUri = fixture.getBaseUri();
final SimpleHttpLocation location = new SimpleHttpLocation("test", baseUri, true, true, true, true, null);
final Transfer transfer = fixture.getTransfer(new ConcreteResource(location, path));
final String url = fixture.formatUrl(path);
assertThat(transfer.exists(), equalTo(false));
// first call, server should quit transferring halfway through the transfer
HttpDownload dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
DownloadJob resultJob = dl.call();
TransferException error = dl.getError();
assertThat(error, notNullValue());
error.printStackTrace();
assertThat(resultJob, notNullValue());
Transfer result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(false));
assertThat(transfer.exists(), equalTo(false));
// second call should hit upstream again and succeed.
dl = new HttpDownload(url, location, transfer, new HashMap<Transfer, Long>(), new EventMetadata(), fixture.getHttp(), new ObjectMapper());
resultJob = dl.call();
error = dl.getError();
assertThat(error, nullValue());
assertThat(resultJob, notNullValue());
result = resultJob.getTransfer();
assertThat(result, notNullValue());
assertThat(result.exists(), equalTo(true));
assertThat(transfer.exists(), equalTo(true));
final String urlPath = fixture.getUrlPath(url);
assertThat(fixture.getAccessesFor(urlPath), equalTo(2));
}
Aggregations