Search in sources :

Example 11 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestPersonStore method testTtl.

@Test
public void testTtl() throws InterruptedException {
    StoreConfig config = new StoreConfig();
    config.setTtl(new Duration(1, TimeUnit.MILLISECONDS));
    PersonStore store = new PersonStore(config, new InMemoryEventClient());
    store.put("foo", new Person("foo@example.com", "Mr Foo"));
    Thread.sleep(2);
    assertNull(store.get("foo"));
}
Also used : InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Duration(io.airlift.units.Duration) Test(org.testng.annotations.Test)

Example 12 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestDelimitedRequestLog method testNoTimeToFirstByte.

@Test
public void testNoTimeToFirstByte() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    when(request.getHttpVersion()).thenReturn(HTTP_2);
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, null, eventClient, false);
    logger.log(request, response, 0, 0, 0, new DoubleSummaryStats(new DoubleSummaryStatistics()));
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertNull(event.getTimeToFirstByte());
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Test(org.testng.annotations.Test)

Example 13 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestDelimitedRequestLog method testNoXForwardedProto.

@Test
public void testNoXForwardedProto() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    String protocol = "protocol";
    when(request.getScheme()).thenReturn("protocol");
    when(request.getHttpVersion()).thenReturn(HTTP_2);
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, null, eventClient, false);
    logger.log(request, response, 0, 0, 0, new DoubleSummaryStats(new DoubleSummaryStatistics()));
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertEquals(event.getProtocol(), protocol);
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) Test(org.testng.annotations.Test)

Example 14 with InMemoryEventClient

use of io.airlift.event.client.InMemoryEventClient in project airlift by airlift.

the class TestDelimitedRequestLog method testWriteLog.

@Test
public void testWriteLog() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Principal principal = mock(Principal.class);
    long timeToFirstByte = 456;
    long timeToLastByte = 3453;
    long now = System.currentTimeMillis();
    long timestamp = now - timeToLastByte;
    String user = "martin";
    String agent = "HttpClient 4.0";
    String referrer = "http://www.google.com";
    String ip = "4.4.4.4";
    String protocol = "protocol";
    String method = "GET";
    long requestSize = 5432;
    String requestContentType = "request/type";
    long responseSize = 32311;
    int responseCode = 200;
    String responseContentType = "response/type";
    HttpURI uri = new HttpURI("http://www.example.com/aaa+bbb/ccc?param=hello%20there&other=true");
    long beginToDispatchMillis = 333;
    long firstToLastContentTimeInMillis = 444;
    long beginToEndMillis = 555;
    DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
    stats.accept(1);
    stats.accept(3);
    DoubleSummaryStats responseContentInterarrivalStats = new DoubleSummaryStats(stats);
    TraceTokenManager tokenManager = new TraceTokenManager();
    InMemoryEventClient eventClient = new InMemoryEventClient();
    MockCurrentTimeMillisProvider currentTimeMillisProvider = new MockCurrentTimeMillisProvider(timestamp + timeToLastByte);
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, tokenManager, eventClient, currentTimeMillisProvider, false);
    when(principal.getName()).thenReturn(user);
    when(request.getTimeStamp()).thenReturn(timestamp);
    when(request.getHeader("User-Agent")).thenReturn(agent);
    when(request.getHeader("Referer")).thenReturn(referrer);
    when(request.getRemoteAddr()).thenReturn("9.9.9.9");
    when(request.getHeaders("X-FORWARDED-FOR")).thenReturn(Collections.enumeration(ImmutableList.of("1.1.1.1, 2.2.2.2", "3.3.3.3, " + ip)));
    when(request.getProtocol()).thenReturn("unknown");
    when(request.getHeader("X-FORWARDED-PROTO")).thenReturn(protocol);
    when(request.getAttribute(TimingFilter.FIRST_BYTE_TIME)).thenReturn(timestamp + timeToFirstByte);
    when(request.getRequestURI()).thenReturn(uri.toString());
    when(request.getUserPrincipal()).thenReturn(principal);
    when(request.getMethod()).thenReturn(method);
    when(request.getContentRead()).thenReturn(requestSize);
    when(request.getHttpVersion()).thenReturn(HTTP_2);
    when(request.getHeader("Content-Type")).thenReturn(requestContentType);
    when(response.getStatus()).thenReturn(responseCode);
    when(response.getContentCount()).thenReturn(responseSize);
    when(response.getHeader("Content-Type")).thenReturn(responseContentType);
    tokenManager.createAndRegisterNewRequestToken();
    logger.log(request, response, beginToDispatchMillis, beginToEndMillis, firstToLastContentTimeInMillis, responseContentInterarrivalStats);
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertEquals(event.getTimeStamp().toEpochMilli(), timestamp);
    assertEquals(event.getClientAddress(), ip);
    assertEquals(event.getProtocol(), protocol);
    assertEquals(event.getMethod(), method);
    assertEquals(event.getRequestUri(), uri.toString());
    assertEquals(event.getUser(), user);
    assertEquals(event.getAgent(), agent);
    assertEquals(event.getReferrer(), referrer);
    assertEquals(event.getRequestSize(), requestSize);
    assertEquals(event.getRequestContentType(), requestContentType);
    assertEquals(event.getResponseSize(), responseSize);
    assertEquals(event.getResponseCode(), responseCode);
    assertEquals(event.getResponseContentType(), responseContentType);
    assertEquals(event.getTimeToFirstByte(), (Long) timeToFirstByte);
    assertEquals(event.getTimeToLastByte(), timeToLastByte);
    assertEquals(event.getTraceToken(), tokenManager.getCurrentRequestToken());
    assertEquals(event.getBeginToDispatchMillis(), beginToDispatchMillis);
    assertEquals(event.getFirstToLastContentTimeInMillis(), firstToLastContentTimeInMillis);
    assertEquals(event.getResponseContentInterarrivalStats(), responseContentInterarrivalStats);
    String actual = asCharSource(file, UTF_8).read();
    String expected = String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", ISO_FORMATTER.format(Instant.ofEpochMilli(timestamp)), ip, method, uri, user, agent, responseCode, requestSize, responseSize, event.getTimeToLastByte(), tokenManager.getCurrentRequestToken(), HTTP_2.toString(), beginToDispatchMillis, beginToEndMillis, firstToLastContentTimeInMillis, format("%.2f, %.2f, %.2f, %d", stats.getMin(), stats.getAverage(), stats.getMax(), stats.getCount()));
    assertEquals(actual, expected);
}
Also used : InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) HttpURI(org.eclipse.jetty.http.HttpURI) TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) Response(org.eclipse.jetty.server.Response) Principal(java.security.Principal) Test(org.testng.annotations.Test)

Aggregations

InMemoryEventClient (io.airlift.event.client.InMemoryEventClient)14 Test (org.testng.annotations.Test)13 DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)6 Request (org.eclipse.jetty.server.Request)6 Response (org.eclipse.jetty.server.Response)6 TraceTokenManager (io.airlift.tracetoken.TraceTokenManager)2 Duration (io.airlift.units.Duration)1 Principal (java.security.Principal)1 HttpURI (org.eclipse.jetty.http.HttpURI)1 BeforeMethod (org.testng.annotations.BeforeMethod)1