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));
}
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));
}
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));
}
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));
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class DefaultConfigTest method testOnRepositoryChange.
@Test
public void testOnRepositoryChange() throws Exception {
String someKey = "someKey";
String someSystemPropertyValue = "system-property-value";
String anotherKey = "anotherKey";
String someLocalFileValue = "local-file-value";
String keyToBeDeleted = "keyToBeDeleted";
String keyToBeDeletedValue = "keyToBeDeletedValue";
String yetAnotherKey = "yetAnotherKey";
String yetAnotherValue = "yetAnotherValue";
String yetAnotherResourceValue = "yetAnotherResourceValue";
// set up system property
System.setProperty(someKey, someSystemPropertyValue);
// set up config repo
someProperties = new Properties();
someProperties.putAll(ImmutableMap.of(someKey, someLocalFileValue, anotherKey, someLocalFileValue, keyToBeDeleted, keyToBeDeletedValue, yetAnotherKey, yetAnotherValue));
when(configRepository.getConfig()).thenReturn(someProperties);
// set up resource file
File resourceFile = new File(someResourceDir, someNamespace + ".properties");
Files.append(yetAnotherKey + "=" + yetAnotherResourceValue, resourceFile, Charsets.UTF_8);
DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository);
final SettableFuture<ConfigChangeEvent> configChangeFuture = SettableFuture.create();
ConfigChangeListener someListener = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
configChangeFuture.set(changeEvent);
}
};
defaultConfig.addChangeListener(someListener);
Properties newProperties = new Properties();
String someKeyNewValue = "new-some-value";
String anotherKeyNewValue = "another-new-value";
String newKey = "newKey";
String newValue = "newValue";
newProperties.putAll(ImmutableMap.of(someKey, someKeyNewValue, anotherKey, anotherKeyNewValue, newKey, newValue));
defaultConfig.onRepositoryChange(someNamespace, newProperties);
ConfigChangeEvent changeEvent = configChangeFuture.get(500, TimeUnit.MILLISECONDS);
// clean up
System.clearProperty(someKey);
assertEquals(someNamespace, changeEvent.getNamespace());
assertEquals(4, changeEvent.changedKeys().size());
ConfigChange anotherKeyChange = changeEvent.getChange(anotherKey);
assertEquals(someLocalFileValue, anotherKeyChange.getOldValue());
assertEquals(anotherKeyNewValue, anotherKeyChange.getNewValue());
assertEquals(PropertyChangeType.MODIFIED, anotherKeyChange.getChangeType());
ConfigChange yetAnotherKeyChange = changeEvent.getChange(yetAnotherKey);
assertEquals(yetAnotherValue, yetAnotherKeyChange.getOldValue());
assertEquals(yetAnotherResourceValue, yetAnotherKeyChange.getNewValue());
assertEquals(PropertyChangeType.MODIFIED, yetAnotherKeyChange.getChangeType());
ConfigChange keyToBeDeletedChange = changeEvent.getChange(keyToBeDeleted);
assertEquals(keyToBeDeletedValue, keyToBeDeletedChange.getOldValue());
assertEquals(null, keyToBeDeletedChange.getNewValue());
assertEquals(PropertyChangeType.DELETED, keyToBeDeletedChange.getChangeType());
ConfigChange newKeyChange = changeEvent.getChange(newKey);
assertEquals(null, newKeyChange.getOldValue());
assertEquals(newValue, newKeyChange.getNewValue());
assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType());
}
Aggregations