From 3f428211f9dd384de7f2caa7cedc5980dc161e89 Mon Sep 17 00:00:00 2001 From: nobodyiam Date: Sun, 9 Sep 2018 21:30:35 +0800 Subject: [PATCH] add removeChangeListener api --- .../com/ctrip/framework/apollo/Config.java | 8 +++ .../apollo/internals/AbstractConfig.java | 6 ++ .../apollo/internals/DefaultConfigTest.java | 57 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java index d80cf8b722d..1ad6a68d9d4 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/Config.java @@ -178,6 +178,14 @@ public interface Config { */ public void addChangeListener(ConfigChangeListener listener, Set interestedKeys); + /** + * Remove the change listener + * + * @param listener the specific config change listener to remove + * @return true if the specific config change listener is found and removed + */ + public boolean removeChangeListener(ConfigChangeListener listener); + /** * Return a set of the property names * diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java index 8bfd61720b5..2bc0dd18f10 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/AbstractConfig.java @@ -85,6 +85,12 @@ public void addChangeListener(ConfigChangeListener listener, Set interes } } + @Override + public boolean removeChangeListener(ConfigChangeListener listener) { + m_interestedKeys.remove(listener); + return m_listeners.remove(listener); + } + @Override public Integer getIntProperty(String key, Integer defaultValue) { try { diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java index fd3b77930f7..43f9fca2363 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/DefaultConfigTest.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -705,6 +706,62 @@ public void onChange(ConfigChangeEvent changeEvent) { assertFalse(interestedInSomeKeyNotChangedFuture.isDone()); } + @Test + public void testRemoveChangeListener() throws Exception { + String someNamespace = "someNamespace"; + final ConfigChangeEvent someConfigChangEvent = mock(ConfigChangeEvent.class); + ConfigChangeEvent anotherConfigChangEvent = mock(ConfigChangeEvent.class); + + final SettableFuture someListenerFuture1 = SettableFuture.create(); + final SettableFuture someListenerFuture2 = SettableFuture.create(); + ConfigChangeListener someListener = new ConfigChangeListener() { + @Override + public void onChange(ConfigChangeEvent changeEvent) { + if (someConfigChangEvent == changeEvent) { + someListenerFuture1.set(changeEvent); + } else { + someListenerFuture2.set(changeEvent); + } + } + }; + + final SettableFuture anotherListenerFuture1 = SettableFuture.create(); + final SettableFuture anotherListenerFuture2 = SettableFuture.create(); + ConfigChangeListener anotherListener = new ConfigChangeListener() { + @Override + public void onChange(ConfigChangeEvent changeEvent) { + if (someConfigChangEvent == 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(someConfigChangEvent); + + assertEquals(someConfigChangEvent, someListenerFuture1.get(500, TimeUnit.MILLISECONDS)); + assertEquals(someConfigChangEvent, anotherListenerFuture1.get(500, TimeUnit.MILLISECONDS)); + + assertFalse(config.removeChangeListener(yetAnotherListener)); + assertTrue(config.removeChangeListener(someListener)); + + config.fireConfigChange(anotherConfigChangEvent); + + assertEquals(anotherConfigChangEvent, anotherListenerFuture2.get(500, TimeUnit.MILLISECONDS)); + + TimeUnit.MILLISECONDS.sleep(100); + + assertFalse(someListenerFuture2.isDone()); + } + @Test public void testGetPropertyNames() { String someKeyPrefix = "someKey";