Search in sources :

Example 1 with ExpectationHandler

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);
                }
            }
        });
    }
}
Also used : InputStream(java.io.InputStream) TransportManagerConfig(org.commonjava.maven.galley.config.TransportManagerConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) MemoryNotFoundCache(org.commonjava.maven.galley.nfc.MemoryNotFoundCache) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.slf4j.Logger) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TransferException(org.commonjava.maven.galley.TransferException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpectationHandler(org.commonjava.test.http.expect.ExpectationHandler) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleLocation(org.commonjava.maven.galley.model.SimpleLocation) MemoryNotFoundCache(org.commonjava.maven.galley.nfc.MemoryNotFoundCache) NotFoundCache(org.commonjava.maven.galley.spi.nfc.NotFoundCache) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 2 with ExpectationHandler

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));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TransferException(org.commonjava.maven.galley.TransferException) ExpectationHandler(org.commonjava.test.http.expect.ExpectationHandler) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter) BMRule(org.jboss.byteman.contrib.bmunit.BMRule) Test(org.junit.Test)

Example 3 with ExpectationHandler

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));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TransferException(org.commonjava.maven.galley.TransferException) ExpectationHandler(org.commonjava.test.http.expect.ExpectationHandler) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with ExpectationHandler

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));
}
Also used : SimpleHttpLocation(org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DownloadJob(org.commonjava.maven.galley.spi.transport.DownloadJob) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) TransferException(org.commonjava.maven.galley.TransferException) ExpectationHandler(org.commonjava.test.http.expect.ExpectationHandler) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)4 ServletException (javax.servlet.ServletException)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 TransferException (org.commonjava.maven.galley.TransferException)4 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)4 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)4 Transfer (org.commonjava.maven.galley.model.Transfer)4 ExpectationHandler (org.commonjava.test.http.expect.ExpectationHandler)4 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashMap (java.util.HashMap)3 DownloadJob (org.commonjava.maven.galley.spi.transport.DownloadJob)3 SimpleHttpLocation (org.commonjava.maven.galley.transport.htcli.model.SimpleHttpLocation)3 PrintWriter (java.io.PrintWriter)2 InputStream (java.io.InputStream)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 TransportManagerConfig (org.commonjava.maven.galley.config.TransportManagerConfig)1 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)1