Search in sources :

Example 11 with ConfigChangeEvent

use of com.ctrip.framework.apollo.model.ConfigChangeEvent 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 12 with ConfigChangeEvent

use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.

the class ConfigIntegrationTest method testLongPollRefreshWithMultipleNamespacesAndMultipleNamespaceNotified.

@Test
public void testLongPollRefreshWithMultipleNamespacesAndMultipleNamespaceNotified() 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), new ApolloConfigNotification(someOtherNamespace, someNotificationId)), false);
    startServerWithHandlers(configHandler, pollHandler);
    Config config = ConfigService.getAppConfig();
    Config someOtherConfig = ConfigService.getConfig(someOtherNamespace);
    assertEquals(someValue, config.getProperty(someKey, null));
    assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
    final SettableFuture<Boolean> longPollFinished = SettableFuture.create();
    final SettableFuture<Boolean> someOtherNamespacelongPollFinished = SettableFuture.create();
    config.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            longPollFinished.set(true);
        }
    });
    someOtherConfig.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            someOtherNamespacelongPollFinished.set(true);
        }
    });
    apolloConfig.getConfigurations().put(someKey, anotherValue);
    longPollFinished.get(5000, TimeUnit.MILLISECONDS);
    someOtherNamespacelongPollFinished.get(5000, TimeUnit.MILLISECONDS);
    assertEquals(anotherValue, config.getProperty(someKey, null));
    assertEquals(anotherValue, 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 13 with ConfigChangeEvent

use of com.ctrip.framework.apollo.model.ConfigChangeEvent 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)

Example 14 with ConfigChangeEvent

use of com.ctrip.framework.apollo.model.ConfigChangeEvent 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 15 with ConfigChangeEvent

use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.

the class DefaultConfigTest method testRemoveChangeListener.

@Test
public void testRemoveChangeListener() throws Exception {
    String someNamespace = "someNamespace";
    final ConfigChangeEvent someConfigChangeEvent = mock(ConfigChangeEvent.class);
    ConfigChangeEvent anotherConfigChangeEvent = mock(ConfigChangeEvent.class);
    final SettableFuture<ConfigChangeEvent> someListenerFuture1 = SettableFuture.create();
    final SettableFuture<ConfigChangeEvent> someListenerFuture2 = SettableFuture.create();
    ConfigChangeListener someListener = new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            if (someConfigChangeEvent == changeEvent) {
                someListenerFuture1.set(changeEvent);
            } else {
                someListenerFuture2.set(changeEvent);
            }
        }
    };
    final SettableFuture<ConfigChangeEvent> anotherListenerFuture1 = SettableFuture.create();
    final SettableFuture<ConfigChangeEvent> anotherListenerFuture2 = SettableFuture.create();
    ConfigChangeListener anotherListener = new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            if (someConfigChangeEvent == changeEvent) {
                anotherListenerFuture1.set(changeEvent);
            } else {
                anotherListenerFuture2.set(changeEvent);
            }
        }
    };
    ConfigChangeListener yetAnotherListener = mock(ConfigChangeListener.class);
    DefaultConfig config = new DefaultConfig(someNamespace, mock(ConfigRepository.class));
    config.addChangeListener(someListener);
    config.addChangeListener(anotherListener);
    config.fireConfigChange(someConfigChangeEvent);
    assertEquals(someConfigChangeEvent, someListenerFuture1.get(500, TimeUnit.MILLISECONDS));
    assertEquals(someConfigChangeEvent, anotherListenerFuture1.get(500, TimeUnit.MILLISECONDS));
    assertFalse(config.removeChangeListener(yetAnotherListener));
    assertTrue(config.removeChangeListener(someListener));
    config.fireConfigChange(anotherConfigChangeEvent);
    assertEquals(anotherConfigChangeEvent, anotherListenerFuture2.get(500, TimeUnit.MILLISECONDS));
    TimeUnit.MILLISECONDS.sleep(100);
    assertFalse(someListenerFuture2.isDone());
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Aggregations

ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)28 Test (org.junit.Test)25 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)23 Config (com.ctrip.framework.apollo.Config)16 ConfigChange (com.ctrip.framework.apollo.model.ConfigChange)9 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)5 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)4 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)4 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)4 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)4 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)4 HashMap (java.util.HashMap)4 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3 Properties (java.util.Properties)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 DirtiesContext (org.springframework.test.annotation.DirtiesContext)3 ConfigSourceType (com.ctrip.framework.apollo.enums.ConfigSourceType)2