Search in sources :

Example 31 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.

the class ConfigIntegrationTest method mockPollNotificationHandler.

private ContextHandler mockPollNotificationHandler(final long pollResultTimeOutInMS, final int statusCode, final List<ApolloConfigNotification> result, final boolean failedAtFirstTime) {
    ContextHandler context = new ContextHandler("/notifications/v2");
    context.setHandler(new AbstractHandler() {

        AtomicInteger counter = new AtomicInteger(0);

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            if (failedAtFirstTime && counter.incrementAndGet() == 1) {
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                baseRequest.setHandled(true);
                return;
            }
            try {
                TimeUnit.MILLISECONDS.sleep(pollResultTimeOutInMS);
            } catch (InterruptedException e) {
            }
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(statusCode);
            response.getWriter().println(gson.toJson(result));
            baseRequest.setHandled(true);
        }
    });
    return context;
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler)

Example 32 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.

the class ConfigIntegrationTest method testLongPollRefresh.

@Test
public void testLongPollRefresh() throws Exception {
    final String someKey = "someKey";
    final String someValue = "someValue";
    final String anotherValue = "anotherValue";
    long someNotificationId = 1;
    long pollTimeoutInMS = 50;
    Map<String, String> configurations = Maps.newHashMap();
    configurations.put(someKey, someValue);
    ApolloConfig apolloConfig = assembleApolloConfig(configurations);
    ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
    ContextHandler pollHandler = mockPollNotificationHandler(pollTimeoutInMS, HttpServletResponse.SC_OK, Lists.newArrayList(new ApolloConfigNotification(apolloConfig.getNamespaceName(), someNotificationId)), false);
    startServerWithHandlers(configHandler, pollHandler);
    Config config = ConfigService.getAppConfig();
    assertEquals(someValue, config.getProperty(someKey, null));
    final SettableFuture<Boolean> longPollFinished = SettableFuture.create();
    config.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            longPollFinished.set(true);
        }
    });
    apolloConfig.getConfigurations().put(someKey, anotherValue);
    longPollFinished.get(pollTimeoutInMS * 20, TimeUnit.MILLISECONDS);
    assertEquals(anotherValue, config.getProperty(someKey, null));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 33 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.

the class ConfigIntegrationTest method testLongPollRefreshWithMultipleNamespacesAndOnlyOneNamespaceNotified.

@Test
public void testLongPollRefreshWithMultipleNamespacesAndOnlyOneNamespaceNotified() throws Exception {
    final String someKey = "someKey";
    final String someValue = "someValue";
    final String anotherValue = "anotherValue";
    long someNotificationId = 1;
    long pollTimeoutInMS = 50;
    Map<String, String> configurations = Maps.newHashMap();
    configurations.put(someKey, someValue);
    ApolloConfig apolloConfig = assembleApolloConfig(configurations);
    ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
    ContextHandler pollHandler = mockPollNotificationHandler(pollTimeoutInMS, HttpServletResponse.SC_OK, Lists.newArrayList(new ApolloConfigNotification(apolloConfig.getNamespaceName(), someNotificationId)), false);
    startServerWithHandlers(configHandler, pollHandler);
    Config someOtherConfig = ConfigService.getConfig(someOtherNamespace);
    Config config = ConfigService.getAppConfig();
    assertEquals(someValue, config.getProperty(someKey, null));
    assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
    final SettableFuture<Boolean> longPollFinished = SettableFuture.create();
    config.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            longPollFinished.set(true);
        }
    });
    apolloConfig.getConfigurations().put(someKey, anotherValue);
    longPollFinished.get(5000, TimeUnit.MILLISECONDS);
    assertEquals(anotherValue, config.getProperty(someKey, null));
    TimeUnit.MILLISECONDS.sleep(pollTimeoutInMS * 10);
    assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 34 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.

the class ConfigIntegrationTest method testGetConfigWithLocalFileAndRemoteConfigError.

@Test
public void testGetConfigWithLocalFileAndRemoteConfigError() throws Exception {
    String someKey = "someKey";
    String someValue = "someValue";
    Properties properties = new Properties();
    properties.put(someKey, someValue);
    createLocalCachePropertyFile(properties);
    ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
    startServerWithHandlers(handler);
    Config config = ConfigService.getAppConfig();
    assertEquals(someValue, config.getProperty(someKey, null));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) Properties(java.util.Properties) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 35 with ContextHandler

use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.

the class ConfigIntegrationTest method testRefreshConfig.

@Test
public void testRefreshConfig() throws Exception {
    final String someKey = "someKey";
    final String someValue = "someValue";
    final String anotherValue = "anotherValue";
    int someRefreshInterval = 500;
    TimeUnit someRefreshTimeUnit = TimeUnit.MILLISECONDS;
    setRefreshInterval(someRefreshInterval);
    setRefreshTimeUnit(someRefreshTimeUnit);
    Map<String, String> configurations = Maps.newHashMap();
    configurations.put(someKey, someValue);
    ApolloConfig apolloConfig = assembleApolloConfig(configurations);
    ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
    startServerWithHandlers(handler);
    Config config = ConfigService.getAppConfig();
    final List<ConfigChangeEvent> changeEvents = Lists.newArrayList();
    final SettableFuture<Boolean> refreshFinished = SettableFuture.create();
    config.addChangeListener(new ConfigChangeListener() {

        AtomicInteger counter = new AtomicInteger(0);

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            // only need to assert once
            if (counter.incrementAndGet() > 1) {
                return;
            }
            assertEquals(1, changeEvent.changedKeys().size());
            assertTrue(changeEvent.isChanged(someKey));
            assertEquals(someValue, changeEvent.getChange(someKey).getOldValue());
            assertEquals(anotherValue, changeEvent.getChange(someKey).getNewValue());
            // if there is any assertion failed above, this line won't be executed
            changeEvents.add(changeEvent);
            refreshFinished.set(true);
        }
    });
    apolloConfig.getConfigurations().put(someKey, anotherValue);
    refreshFinished.get(someRefreshInterval * 5, someRefreshTimeUnit);
    assertThat("Change event's size should equal to one or there must be some assertion failed in change listener", 1, equalTo(changeEvents.size()));
    assertEquals(anotherValue, config.getProperty(someKey, null));
}
Also used : ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) TimeUnit(java.util.concurrent.TimeUnit) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Aggregations

ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)127 Server (org.eclipse.jetty.server.Server)47 ServerConnector (org.eclipse.jetty.server.ServerConnector)27 URI (java.net.URI)21 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)20 Test (org.junit.Test)20 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)19 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)19 IOException (java.io.IOException)18 Handler (org.eclipse.jetty.server.Handler)14 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)14 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)14 File (java.io.File)13 ServletException (javax.servlet.ServletException)13 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)13 BeforeClass (org.junit.BeforeClass)11 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)10 Config (com.ctrip.framework.apollo.Config)10 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)10 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)10