use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class ApolloMockServerApiTest method testDeleteProperties.
@Test
public void testDeleteProperties() throws Exception {
Config otherConfig = ConfigService.getConfig(anotherNamespace);
final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
otherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
future.set(changeEvent);
}
});
assertEquals("otherValue4", otherConfig.getProperty("key4", null));
assertEquals("otherValue5", otherConfig.getProperty("key5", null));
embeddedApollo.deleteProperty(anotherNamespace, "key4");
ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
assertNull(otherConfig.getProperty("key4", null));
assertEquals("otherValue5", otherConfig.getProperty("key5", null));
assertTrue(changeEvent.isChanged("key4"));
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class ApolloMockServerApiTest method testUpdateSamePropertyTwice.
@Test
public void testUpdateSamePropertyTwice() throws Exception {
String someNewValue = "someNewValue";
Config otherConfig = ConfigService.getConfig(anotherNamespace);
final Semaphore changes = new Semaphore(0);
otherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
changes.release();
}
});
assertEquals("otherValue3", otherConfig.getProperty("key3", null));
embeddedApollo.addOrModifyProperty(anotherNamespace, "key3", someNewValue);
embeddedApollo.addOrModifyProperty(anotherNamespace, "key3", someNewValue);
assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS));
assertEquals(someNewValue, otherConfig.getProperty("key3", null));
assertEquals(0, changes.availablePermits());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class DefaultConfigTest method testFireConfigChangeWithInterestedKeys.
@Test
public void testFireConfigChangeWithInterestedKeys() throws Exception {
String someKeyChanged = "someKeyChanged";
String anotherKeyChanged = "anotherKeyChanged";
String someKeyNotChanged = "someKeyNotChanged";
String someNamespace = "someNamespace";
Map<String, ConfigChange> changes = Maps.newHashMap();
changes.put(someKeyChanged, mock(ConfigChange.class));
changes.put(anotherKeyChanged, mock(ConfigChange.class));
ConfigChangeEvent someChangeEvent = new ConfigChangeEvent(someNamespace, changes);
final SettableFuture<ConfigChangeEvent> interestedInAllKeysFuture = SettableFuture.create();
ConfigChangeListener interestedInAllKeys = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
interestedInAllKeysFuture.set(changeEvent);
}
};
final SettableFuture<ConfigChangeEvent> interestedInSomeKeyFuture = SettableFuture.create();
ConfigChangeListener interestedInSomeKey = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
interestedInSomeKeyFuture.set(changeEvent);
}
};
final SettableFuture<ConfigChangeEvent> interestedInSomeKeyNotChangedFuture = SettableFuture.create();
ConfigChangeListener interestedInSomeKeyNotChanged = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
interestedInSomeKeyNotChangedFuture.set(changeEvent);
}
};
DefaultConfig config = new DefaultConfig(someNamespace, mock(ConfigRepository.class));
config.addChangeListener(interestedInAllKeys);
config.addChangeListener(interestedInSomeKey, Sets.newHashSet(someKeyChanged));
config.addChangeListener(interestedInSomeKeyNotChanged, Sets.newHashSet(someKeyNotChanged));
config.fireConfigChange(someChangeEvent);
ConfigChangeEvent changeEvent = interestedInAllKeysFuture.get(500, TimeUnit.MILLISECONDS);
assertEquals(someChangeEvent, changeEvent);
{
// hidden variables in scope
ConfigChangeEvent actualConfigChangeEvent = interestedInSomeKeyFuture.get(500, TimeUnit.MILLISECONDS);
assertEquals(someChangeEvent.changedKeys(), actualConfigChangeEvent.changedKeys());
for (String changedKey : someChangeEvent.changedKeys()) {
ConfigChange expectConfigChange = someChangeEvent.getChange(changedKey);
ConfigChange actualConfigChange = actualConfigChangeEvent.getChange(changedKey);
assertEquals(expectConfigChange, actualConfigChange);
}
}
assertFalse(interestedInSomeKeyNotChangedFuture.isDone());
}
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);
someSourceType = ConfigSourceType.LOCAL;
when(configRepository.getSourceType()).thenReturn(someSourceType);
// 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);
assertEquals(someSourceType, defaultConfig.getSourceType());
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));
ConfigSourceType anotherSourceType = ConfigSourceType.REMOTE;
when(configRepository.getSourceType()).thenReturn(anotherSourceType);
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());
assertEquals(anotherSourceType, defaultConfig.getSourceType());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class InterestedConfigChangeEventTest method TestInterestedChangedKeys.
@Test
public void TestInterestedChangedKeys() throws ExecutionException, InterruptedException, TimeoutException {
final String namespace = "app";
final String keyPrefix = "key-abc.";
final String key = keyPrefix + UUID.randomUUID();
final String anotherKey = UUID.randomUUID().toString();
final SettableFuture<ConfigChangeEvent> onChangeFuture = SettableFuture.create();
ConfigChangeListener configChangeListener = spy(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
assertEquals(namespace, changeEvent.getNamespace());
assertEquals(2, changeEvent.changedKeys().size());
assertTrue(changeEvent.changedKeys().containsAll(Sets.newHashSet(key, anotherKey)));
assertEquals(1, changeEvent.interestedChangedKeys().size());
assertTrue(changeEvent.interestedChangedKeys().contains(key));
onChangeFuture.set(changeEvent);
}
});
UnsupportedOperationConfig config = new UnsupportedOperationConfig();
config.addChangeListener(configChangeListener, Collections.singleton("key-nothing"), Collections.singleton(keyPrefix));
Map<String, ConfigChange> changes = new HashMap<>();
changes.put(key, new ConfigChange(namespace, key, "123", "456", PropertyChangeType.MODIFIED));
changes.put(anotherKey, new ConfigChange(namespace, anotherKey, null, "someValue", PropertyChangeType.ADDED));
config.fireConfigChange(namespace, changes);
onChangeFuture.get(500, TimeUnit.MILLISECONDS);
verify(configChangeListener, atLeastOnce()).onChange(Matchers.<ConfigChangeEvent>any());
}
Aggregations