Search in sources :

Example 1 with AsyncHandler

use of com.spotify.apollo.route.AsyncHandler in project apollo by spotify.

the class EnvironmentFactoryTest method shouldThrowIfUsedAfterInit.

@Test
public void shouldThrowIfUsedAfterInit() throws Exception {
    final EnvironmentFactory factory = sut.build();
    final RoutingContext routingContext = factory.createRoutingContext();
    final Environment environment = factory.create(SERVICE_NAME, routingContext);
    final Route<AsyncHandler<Response<ByteString>>> route = Route.sync("GET", "/f1", handler());
    environment.routingEngine().registerRoute(route);
    final Iterable<Object> objects = routingContext.endpointObjects();
    assertTrue(Iterables.contains(objects, route));
    try {
        environment.routingEngine().registerRoute(route);
        fail("should throw");
    } catch (Exception e) {
        assertThat(e.getMessage(), containsString("already been initialized"));
    }
}
Also used : RoutingContext(com.spotify.apollo.environment.EnvironmentFactory.RoutingContext) AsyncHandler(com.spotify.apollo.route.AsyncHandler) ByteString(okio.ByteString) Environment(com.spotify.apollo.Environment) Test(org.junit.Test)

Example 2 with AsyncHandler

use of com.spotify.apollo.route.AsyncHandler in project apollo by spotify.

the class EnvironmentFactoryTest method shouldCollectRegisteredRoutes.

@Test
public void shouldCollectRegisteredRoutes() throws Exception {
    final EnvironmentFactory factory = sut.build();
    final RoutingContext routingContext = factory.createRoutingContext();
    final Environment environment = factory.create(SERVICE_NAME, routingContext);
    final Route<AsyncHandler<Response<ByteString>>> route1 = Route.sync("GET", "/f1", handler());
    final Route<AsyncHandler<Response<ByteString>>> route2 = Route.sync("GET", "/2", handler());
    final Route<AsyncHandler<Response<ByteString>>> route3 = Route.sync("GET", "/3", handler());
    environment.routingEngine().registerRoute(route1).registerRoute(route2);
    environment.routingEngine().registerRoute(route3);
    final Iterable<Object> objects = routingContext.endpointObjects();
    assertTrue(Iterables.contains(objects, route1));
    assertTrue(Iterables.contains(objects, route2));
    assertTrue(Iterables.contains(objects, route3));
}
Also used : RoutingContext(com.spotify.apollo.environment.EnvironmentFactory.RoutingContext) AsyncHandler(com.spotify.apollo.route.AsyncHandler) ByteString(okio.ByteString) Environment(com.spotify.apollo.Environment) Test(org.junit.Test)

Example 3 with AsyncHandler

use of com.spotify.apollo.route.AsyncHandler in project apollo by spotify.

the class RouteTransformMetricsExampleTest method shouldTrackResponsePayloadSize.

@Test
public void shouldTrackResponsePayloadSize() throws Exception {
    Route<AsyncHandler<Response<ByteString>>> testRoute = Route.sync("GET", "/foo/<name>", context -> Response.forPayload(ByteString.encodeUtf8(context.pathArgs().get("name"))));
    Route<AsyncHandler<Response<ByteString>>> trackedRoute = withResponsePayloadSizeHistogram(testRoute);
    RequestContext context = mock(RequestContext.class);
    when(context.pathArgs()).thenReturn(Collections.singletonMap("name", "bar"));
    trackedRoute.handler().invoke(context).toCompletableFuture().get();
    assertThat(registry.getHistograms().keySet(), containsInAnyOrder(hasProperty("tags", allOf(hasEntry("service", "example"), hasEntry("what", "endpoint-response-size"), hasEntry("endpoint", "GET:/foo/<name>")))));
}
Also used : AsyncHandler(com.spotify.apollo.route.AsyncHandler) ByteString(okio.ByteString) RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test)

Example 4 with AsyncHandler

use of com.spotify.apollo.route.AsyncHandler in project apollo by spotify.

the class RouteTransformMetricsExampleTest method responsePayloadSizeHistogram.

/**
 * Middleware to track response payload size in a Histogram,
 * tagged with an endpoint tag set to the given endpoint name.
 */
public Middleware<AsyncHandler<Response<ByteString>>, AsyncHandler<Response<ByteString>>> responsePayloadSizeHistogram(String endpointName) {
    final MetricId histogramId = MetricId.build().tagged("service", serviceName).tagged("endpoint", endpointName).tagged("what", "endpoint-response-size");
    final Histogram histogram = registry.histogram(histogramId);
    return (inner) -> (requestContext) -> inner.invoke(requestContext).whenComplete((response, t) -> {
        if (response != null) {
            histogram.update(response.payload().map(ByteString::size).orElse(0));
        }
    });
}
Also used : Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Histogram(com.codahale.metrics.Histogram) Response(com.spotify.apollo.Response) AsyncHandler(com.spotify.apollo.route.AsyncHandler) RequestContext(com.spotify.apollo.RequestContext) Matchers.allOf(org.hamcrest.Matchers.allOf) MetricId(com.spotify.metrics.core.MetricId) SemanticMetricRegistry(com.spotify.metrics.core.SemanticMetricRegistry) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Middleware(com.spotify.apollo.route.Middleware) Route(com.spotify.apollo.route.Route) Matchers.hasProperty(org.hamcrest.Matchers.hasProperty) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) ByteString(okio.ByteString) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) MetricId(com.spotify.metrics.core.MetricId) Histogram(com.codahale.metrics.Histogram) ByteString(okio.ByteString)

Aggregations

AsyncHandler (com.spotify.apollo.route.AsyncHandler)4 ByteString (okio.ByteString)4 Test (org.junit.Test)4 Environment (com.spotify.apollo.Environment)2 RequestContext (com.spotify.apollo.RequestContext)2 RoutingContext (com.spotify.apollo.environment.EnvironmentFactory.RoutingContext)2 Histogram (com.codahale.metrics.Histogram)1 Response (com.spotify.apollo.Response)1 Middleware (com.spotify.apollo.route.Middleware)1 Route (com.spotify.apollo.route.Route)1 MetricId (com.spotify.metrics.core.MetricId)1 SemanticMetricRegistry (com.spotify.metrics.core.SemanticMetricRegistry)1 Collections (java.util.Collections)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1 Matchers.allOf (org.hamcrest.Matchers.allOf)1 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)1 Matchers.hasEntry (org.hamcrest.Matchers.hasEntry)1 Matchers.hasProperty (org.hamcrest.Matchers.hasProperty)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1