Search in sources :

Example 6 with ConfigChangeEvent

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"));
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 7 with ConfigChangeEvent

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());
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 8 with ConfigChangeEvent

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());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 9 with ConfigChangeEvent

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());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigSourceType(com.ctrip.framework.apollo.enums.ConfigSourceType) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Example 10 with ConfigChangeEvent

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());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) HashMap(java.util.HashMap) 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