use of io.airlift.event.client.InMemoryEventModule in project airlift by airlift.
the class TestServer method setup.
@BeforeMethod
public void setup() {
Bootstrap app = new Bootstrap(new TestingNodeModule(), new InMemoryEventModule(), new TestingHttpServerModule(), new JsonModule(), new JaxrsModule(), new MainModule());
Injector injector = app.doNotInitializeLogging().initialize();
lifeCycleManager = injector.getInstance(LifeCycleManager.class);
server = injector.getInstance(TestingHttpServer.class);
store = injector.getInstance(PersonStore.class);
eventClient = injector.getInstance(InMemoryEventClient.class);
client = new JettyHttpClient();
}
use of io.airlift.event.client.InMemoryEventModule in project airlift by airlift.
the class TestHttpServerModule method testHttpRequestEvent.
@Test
public void testHttpRequestEvent() throws Exception {
Map<String, String> properties = new ImmutableMap.Builder<String, String>().put("http-server.http.port", "0").put("http-server.log.path", new File(tempDir, "http-request.log").getAbsolutePath()).build();
SingleUseEventClient eventClient = new SingleUseEventClient();
Bootstrap app = new Bootstrap(new HttpServerModule(), new TestingNodeModule(), new InMemoryEventModule(), new TraceTokenModule(), binder -> newSetBinder(binder, EventClient.class).addBinding().toInstance(eventClient), binder -> binder.bind(Servlet.class).annotatedWith(TheServlet.class).to(EchoServlet.class).in(Scopes.SINGLETON));
Injector injector = app.setRequiredConfigurationProperties(properties).doNotInitializeLogging().initialize();
HttpServerInfo httpServerInfo = injector.getInstance(HttpServerInfo.class);
EchoServlet echoServlet = (EchoServlet) injector.getInstance(Key.get(Servlet.class, TheServlet.class));
HttpServer server = injector.getInstance(HttpServer.class);
server.start();
URI requestUri = httpServerInfo.getHttpUri().resolve("/my/path");
String userAgent = "my-user-agent";
String referrer = "http://www.google.com";
String token = "this is a trace token";
String requestBody = Joiner.on(" ").join(nCopies(50, "request"));
String requestContentType = "request/type";
int responseCode = 555;
String responseBody = Joiner.on(" ").join(nCopies(100, "response"));
String responseContentType = "response/type";
echoServlet.responseBody = responseBody;
echoServlet.responseStatusCode = responseCode;
echoServlet.responseHeaders.put("Content-Type", responseContentType);
long beforeRequest = System.currentTimeMillis();
long afterRequest;
HttpRequestEvent event;
try (JettyHttpClient client = new JettyHttpClient()) {
// test servlet bound correctly
StringResponse response = client.execute(preparePost().setUri(requestUri).addHeader(USER_AGENT, userAgent).addHeader(CONTENT_TYPE, requestContentType).addHeader(REFERER, referrer).addHeader("X-Airlift-TraceToken", token).setBodyGenerator(createStaticBodyGenerator(requestBody, UTF_8)).build(), createStringResponseHandler());
afterRequest = System.currentTimeMillis();
assertEquals(response.getStatusCode(), responseCode);
assertEquals(response.getBody(), responseBody);
assertEquals(response.getHeader("Content-Type"), responseContentType);
event = (HttpRequestEvent) eventClient.getEvent().get(10, TimeUnit.SECONDS);
} finally {
server.stop();
}
assertEquals(event.getClientAddress(), echoServlet.remoteAddress);
assertEquals(event.getProtocol(), "http");
assertEquals(event.getMethod(), "POST");
assertEquals(event.getRequestUri(), requestUri.getPath());
assertNull(event.getUser());
assertEquals(event.getAgent(), userAgent);
assertEquals(event.getReferrer(), referrer);
assertEquals(event.getTraceToken(), token);
assertEquals(event.getRequestSize(), requestBody.length());
assertEquals(event.getRequestContentType(), requestContentType);
assertEquals(event.getResponseSize(), responseBody.length());
assertEquals(event.getResponseCode(), responseCode);
assertEquals(event.getResponseContentType(), responseContentType);
assertTrue(event.getTimeStamp().toEpochMilli() >= beforeRequest);
assertTrue(event.getTimeToLastByte() <= afterRequest - beforeRequest);
assertNotNull(event.getTimeToFirstByte());
assertTrue(event.getTimeToDispatch() <= event.getTimeToFirstByte());
assertTrue(event.getTimeToFirstByte() <= event.getTimeToLastByte());
}
Aggregations