Skip to content

Commit

Permalink
Merge pull request apolloconfig#1462 from nobodyiam/remove-change-lis…
Browse files Browse the repository at this point in the history
…tener

add removeChangeListener api
  • Loading branch information
nobodyiam authored Sep 9, 2018
2 parents ee4cfcc + 3f42821 commit 976b69d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ public interface Config {
*/
public void addChangeListener(ConfigChangeListener listener, Set<String> 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public void addChangeListener(ConfigChangeListener listener, Set<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ConfigChangeEvent> someListenerFuture1 = SettableFuture.create();
final SettableFuture<ConfigChangeEvent> 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<ConfigChangeEvent> anotherListenerFuture1 = SettableFuture.create();
final SettableFuture<ConfigChangeEvent> 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";
Expand Down

0 comments on commit 976b69d

Please sign in to comment.