Search in sources :

Example 36 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class KafkaIndexTaskClientTest method testGetCurrentOffsetsWithRetry.

@Test
public void testGetCurrentOffsetsWithRetry() throws Exception {
    client = new TestableKafkaIndexTaskClient(httpClient, objectMapper, taskInfoProvider, 3);
    Capture<Request> captured = Capture.newInstance(CaptureType.ALL);
    expect(responseHolder.getStatus()).andReturn(HttpResponseStatus.NOT_FOUND).times(6).andReturn(HttpResponseStatus.OK).times(1);
    expect(responseHolder.getContent()).andReturn("").times(2).andReturn("{\"0\":1, \"1\":10}");
    expect(responseHolder.getResponse()).andReturn(response).times(2);
    expect(response.headers()).andReturn(headers).times(2);
    expect(headers.get("X-Druid-Task-Id")).andReturn(TEST_ID).times(2);
    expect(httpClient.go(capture(captured), anyObject(FullResponseHandler.class), eq(TEST_HTTP_TIMEOUT))).andReturn(Futures.immediateFuture(responseHolder)).times(3);
    replayAll();
    Map<Integer, Long> results = client.getCurrentOffsets(TEST_ID, true);
    verifyAll();
    Assert.assertEquals(3, captured.getValues().size());
    for (Request request : captured.getValues()) {
        Assert.assertEquals(HttpMethod.GET, request.getMethod());
        Assert.assertEquals(new URL("http://test-host:1234/druid/worker/v1/chat/test-id/offsets/current"), request.getUrl());
        Assert.assertTrue(request.getHeaders().get("X-Druid-Task-Id").contains("test-id"));
    }
    Assert.assertEquals(2, results.size());
    Assert.assertEquals(1, (long) results.get(0));
    Assert.assertEquals(10, (long) results.get(1));
}
Also used : Request(com.metamx.http.client.Request) URL(java.net.URL) Test(org.junit.Test)

Example 37 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class KafkaIndexTaskClientTest method testSetEndOffsets.

@Test
public void testSetEndOffsets() throws Exception {
    Map<Integer, Long> endOffsets = ImmutableMap.of(0, 15L, 1, 120L);
    Capture<Request> captured = Capture.newInstance();
    expect(responseHolder.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
    expect(httpClient.go(capture(captured), anyObject(FullResponseHandler.class), eq(TEST_HTTP_TIMEOUT))).andReturn(Futures.immediateFuture(responseHolder));
    replayAll();
    client.setEndOffsets(TEST_ID, endOffsets);
    verifyAll();
    Request request = captured.getValue();
    Assert.assertEquals(HttpMethod.POST, request.getMethod());
    Assert.assertEquals(new URL("http://test-host:1234/druid/worker/v1/chat/test-id/offsets/end"), request.getUrl());
    Assert.assertTrue(request.getHeaders().get("X-Druid-Task-Id").contains("test-id"));
    Assert.assertEquals("{\"0\":15,\"1\":120}", new String(request.getContent().array()));
}
Also used : Request(com.metamx.http.client.Request) FullResponseHandler(com.metamx.http.client.response.FullResponseHandler) URL(java.net.URL) Test(org.junit.Test)

Example 38 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class KafkaIndexTaskClientTest method testStopAsync.

@Test
public void testStopAsync() throws Exception {
    final int numRequests = TEST_IDS.size();
    Capture<Request> captured = Capture.newInstance(CaptureType.ALL);
    expect(responseHolder.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
    expect(httpClient.go(capture(captured), anyObject(FullResponseHandler.class), eq(TEST_HTTP_TIMEOUT))).andReturn(Futures.immediateFuture(responseHolder)).times(numRequests);
    replayAll();
    List<URL> expectedUrls = Lists.newArrayList();
    List<ListenableFuture<Boolean>> futures = Lists.newArrayList();
    for (int i = 0; i < numRequests; i++) {
        expectedUrls.add(new URL(String.format(URL_FORMATTER, TEST_HOST, TEST_PORT, TEST_IDS.get(i), "stop")));
        futures.add(client.stopAsync(TEST_IDS.get(i), false));
    }
    List<Boolean> responses = Futures.allAsList(futures).get();
    verifyAll();
    List<Request> requests = captured.getValues();
    Assert.assertEquals(numRequests, requests.size());
    Assert.assertEquals(numRequests, responses.size());
    for (int i = 0; i < numRequests; i++) {
        Assert.assertEquals(HttpMethod.POST, requests.get(i).getMethod());
        Assert.assertTrue("unexpectedURL", expectedUrls.contains(requests.get(i).getUrl()));
        Assert.assertTrue(responses.get(i));
    }
}
Also used : Request(com.metamx.http.client.Request) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) URL(java.net.URL) Test(org.junit.Test)

Example 39 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class KafkaIndexTaskClientTest method testGetCurrentOffsetsAsync.

@Test
public void testGetCurrentOffsetsAsync() throws Exception {
    final int numRequests = TEST_IDS.size();
    Capture<Request> captured = Capture.newInstance(CaptureType.ALL);
    expect(responseHolder.getStatus()).andReturn(HttpResponseStatus.OK).anyTimes();
    expect(responseHolder.getContent()).andReturn("{\"0\":\"1\"}").anyTimes();
    expect(httpClient.go(capture(captured), anyObject(FullResponseHandler.class), eq(TEST_HTTP_TIMEOUT))).andReturn(Futures.immediateFuture(responseHolder)).times(numRequests);
    replayAll();
    List<URL> expectedUrls = Lists.newArrayList();
    List<ListenableFuture<Map<Integer, Long>>> futures = Lists.newArrayList();
    for (int i = 0; i < numRequests; i++) {
        expectedUrls.add(new URL(String.format(URL_FORMATTER, TEST_HOST, TEST_PORT, TEST_IDS.get(i), "offsets/current")));
        futures.add(client.getCurrentOffsetsAsync(TEST_IDS.get(i), false));
    }
    List<Map<Integer, Long>> responses = Futures.allAsList(futures).get();
    verifyAll();
    List<Request> requests = captured.getValues();
    Assert.assertEquals(numRequests, requests.size());
    Assert.assertEquals(numRequests, responses.size());
    for (int i = 0; i < numRequests; i++) {
        Assert.assertEquals(HttpMethod.GET, requests.get(i).getMethod());
        Assert.assertTrue("unexpectedURL", expectedUrls.contains(requests.get(i).getUrl()));
        Assert.assertEquals(Maps.newLinkedHashMap(ImmutableMap.of(0, 1L)), responses.get(i));
    }
}
Also used : Request(com.metamx.http.client.Request) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) URL(java.net.URL) Test(org.junit.Test)

Example 40 with Request

use of com.metamx.http.client.Request in project druid by druid-io.

the class JettyTest method testChunkNotFinalized.

// Tests that threads are not stuck when partial chunk is not finalized
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=424107
@Test
@Ignore
public // above bug is not fixed in jetty for gzip encoding, and the chunk is still finalized instead of throwing exception.
void testChunkNotFinalized() throws Exception {
    ListenableFuture<InputStream> go = client.go(new Request(HttpMethod.GET, new URL("http://localhost:" + port + "/exception/exception")), new InputStreamResponseHandler());
    try {
        StringWriter writer = new StringWriter();
        IOUtils.copy(go.get(), writer, "utf-8");
        Assert.fail("Should have thrown Exception");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : InputStreamResponseHandler(com.metamx.http.client.response.InputStreamResponseHandler) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) Request(com.metamx.http.client.Request) IOException(java.io.IOException) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Request (com.metamx.http.client.Request)47 URL (java.net.URL)38 Test (org.junit.Test)37 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)12 FullResponseHandler (com.metamx.http.client.response.FullResponseHandler)12 InputStream (java.io.InputStream)12 IOException (java.io.IOException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Duration (org.joda.time.Duration)9 StatusResponseHolder (com.metamx.http.client.response.StatusResponseHolder)8 Map (java.util.Map)8 SequenceInputStreamResponseHandler (com.metamx.http.client.response.SequenceInputStreamResponseHandler)7 DateTime (org.joda.time.DateTime)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 HttpClient (com.metamx.http.client.HttpClient)4 HttpResponseHandler (com.metamx.http.client.response.HttpResponseHandler)4 StatusResponseHandler (com.metamx.http.client.response.StatusResponseHandler)4 List (java.util.List)4