Search in sources :

Example 6 with RequestHandler

use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.

the class ApolloEnvironmentModuleTest method shouldUseEndpointRunnableFactoryDecorator.

@Test
public void shouldUseEndpointRunnableFactoryDecorator() throws Exception {
    AtomicReference<String> lastResponseRef = new AtomicReference<>();
    final Service service = this.service.withModule(new LastResponseModule(lastResponseRef)).build();
    try (Service.Instance i = service.start()) {
        final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
        final RequestHandler handler = environment.initialize(env -> {
            env.routingEngine().registerAutoRoute(Route.sync("GET", "/", ctx -> "hello"));
        });
        assertNotNull(handler);
        final FakeOngoingRequest ongoingRequest = ongoingRequest("http://foo");
        handler.handle(ongoingRequest);
        assertThat(ongoingRequest.getReply(), hasStatus(Status.GONE));
        assertEquals("hello", lastResponseRef.get());
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : Response(com.spotify.apollo.Response) Request(com.spotify.apollo.Request) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) OngoingRequest(com.spotify.apollo.request.OngoingRequest) RequestHandler(com.spotify.apollo.request.RequestHandler) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ByteString(okio.ByteString) Assert.fail(org.junit.Assert.fail) RequestMetadata(com.spotify.apollo.RequestMetadata) Service(com.spotify.apollo.core.Service) Environment(com.spotify.apollo.Environment) Status(com.spotify.apollo.Status) Before(org.junit.Before) Description(org.hamcrest.Description) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Route(com.spotify.apollo.route.Route) Instant(java.time.Instant) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Consumer(java.util.function.Consumer) MetaModule(com.spotify.apollo.meta.MetaModule) Services(com.spotify.apollo.core.Services) Matcher(org.hamcrest.Matcher) RequestMetadataImpl(com.spotify.apollo.request.RequestMetadataImpl) Optional(java.util.Optional) AppInit(com.spotify.apollo.AppInit) Assert.assertEquals(org.junit.Assert.assertEquals) RequestHandler(com.spotify.apollo.request.RequestHandler) Service(com.spotify.apollo.core.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteString(okio.ByteString) IOException(java.io.IOException) Test(org.junit.Test)

Example 7 with RequestHandler

use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.

the class App method main.

public static void main(String... args) throws IOException, InterruptedException {
    Service service = Services.usingName("test").withModule(HttpServerModule.create()).withModule(HttpClientModule.create()).build();
    try (Service.Instance instance = service.start(args)) {
        final RequestHandler handler = new Handler();
        HttpServerModule.server(instance).start(handler);
        LOG.info("Started service '{}'", service.getServiceName());
        final String string = instance.getConfig().getString("config.key");
        LOG.info("Value of config.key: {}", string);
        instance.waitForShutdown();
        LOG.info("Stopping service '{}'", service.getServiceName());
    }
}
Also used : RequestHandler(com.spotify.apollo.request.RequestHandler) Service(com.spotify.apollo.core.Service) RequestHandler(com.spotify.apollo.request.RequestHandler)

Example 8 with RequestHandler

use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.

the class ApolloEnvironmentModuleTest method shouldUseRequestRunnableFactoryDecorator.

@Test
public void shouldUseRequestRunnableFactoryDecorator() throws Exception {
    final AtomicInteger counter = new AtomicInteger();
    final Service service = this.service.withModule(new RequestInspectingModule("http://foo", counter)).build();
    try (Service.Instance i = service.start()) {
        final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
        final RequestHandler handler = environment.initialize(env -> {
        });
        assertNotNull(handler);
        final OngoingRequest ongoingRequest = ongoingRequest("http://foo");
        handler.handle(ongoingRequest);
        assertEquals(1, counter.get());
        handler.handle(ongoingRequest);
        assertEquals(2, counter.get());
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : RequestHandler(com.spotify.apollo.request.RequestHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(com.spotify.apollo.core.Service) IOException(java.io.IOException) OngoingRequest(com.spotify.apollo.request.OngoingRequest) Test(org.junit.Test)

Example 9 with RequestHandler

use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.

the class ApolloEnvironmentModuleTest method shouldGetFunctioningEnvironmentInAppInit.

@Test
public void shouldGetFunctioningEnvironmentInAppInit() throws Exception {
    final AtomicReference<Environment> envReference = new AtomicReference<>();
    try (Service.Instance i = service.build().start()) {
        final ApolloEnvironment environment = ApolloEnvironmentModule.environment(i);
        final RequestHandler handler = environment.initialize(envReference::set);
        assertNotNull(handler);
        final Environment e = envReference.get();
        validateEnv(e);
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : RequestHandler(com.spotify.apollo.request.RequestHandler) Environment(com.spotify.apollo.Environment) Service(com.spotify.apollo.core.Service) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) Test(org.junit.Test)

Example 10 with RequestHandler

use of com.spotify.apollo.request.RequestHandler in project apollo by spotify.

the class HttpServiceModuleTest method shouldInitAndDestroyApplication.

@Test
public void shouldInitAndDestroyApplication() throws Exception {
    final AtomicBoolean init = new AtomicBoolean();
    final AtomicBoolean destroy = new AtomicBoolean();
    final App app = new App(init, destroy);
    try (Service.Instance i = service(app).start()) {
        final RequestHandler handler = HttpServiceModule.requestHandler(i);
        assertNotNull(handler);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    assertTrue(init.get());
    assertTrue(destroy.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RequestHandler(com.spotify.apollo.request.RequestHandler) Service(com.spotify.apollo.core.Service) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Service (com.spotify.apollo.core.Service)10 RequestHandler (com.spotify.apollo.request.RequestHandler)10 IOException (java.io.IOException)9 Test (org.junit.Test)8 Environment (com.spotify.apollo.Environment)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 OngoingRequest (com.spotify.apollo.request.OngoingRequest)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ByteString (okio.ByteString)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 AppInit (com.spotify.apollo.AppInit)1 Request (com.spotify.apollo.Request)1 RequestMetadata (com.spotify.apollo.RequestMetadata)1 Response (com.spotify.apollo.Response)1 Status (com.spotify.apollo.Status)1 Services (com.spotify.apollo.core.Services)1 ApolloConfig (com.spotify.apollo.environment.ApolloConfig)1 MetaDescriptor (com.spotify.apollo.meta.MetaDescriptor)1 MetaModule (com.spotify.apollo.meta.MetaModule)1