diff --git a/apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/EmbeddedApollo.java b/apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/EmbeddedApollo.java index 7696b5363e2..1efb8a6f073 100644 --- a/apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/EmbeddedApollo.java +++ b/apollo-mockserver/src/main/java/com/ctrip/framework/apollo/mockserver/EmbeddedApollo.java @@ -5,14 +5,13 @@ import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification; import com.ctrip.framework.apollo.core.utils.ResourceUtils; import com.ctrip.framework.apollo.internals.ConfigServiceLocator; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; @@ -38,8 +37,8 @@ public class EmbeddedApollo extends ExternalResource { private static ConfigServiceLocator CONFIG_SERVICE_LOCATOR; private final Gson gson = new Gson(); - private final Map> addedOrModifiedPropertiesOfNamespace = new HashMap<>(); - private final Map> deletedKeysOfNamespace = new HashMap<>(); + private final Map> addedOrModifiedPropertiesOfNamespace = Maps.newConcurrentMap(); + private final Map> deletedKeysOfNamespace = Maps.newConcurrentMap(); private MockWebServer server; @@ -151,7 +150,7 @@ public void addOrModifyProperty(String namespace, String someKey, String someVal if (addedOrModifiedPropertiesOfNamespace.containsKey(namespace)) { addedOrModifiedPropertiesOfNamespace.get(namespace).put(someKey, someValue); } else { - Map m = new HashMap<>(); + Map m = Maps.newConcurrentMap(); m.put(someKey, someValue); addedOrModifiedPropertiesOfNamespace.put(namespace, m); } @@ -164,7 +163,9 @@ public void deleteProperty(String namespace, String someKey) { if (deletedKeysOfNamespace.containsKey(namespace)) { deletedKeysOfNamespace.get(namespace).add(someKey); } else { - deletedKeysOfNamespace.put(namespace, ImmutableSet.of(someKey)); + Set m = Sets.newConcurrentHashSet(); + m.add(someKey); + deletedKeysOfNamespace.put(namespace, m); } } diff --git a/apollo-mockserver/src/test/java/com/ctrip/framework/apollo/mockserver/ApolloMockServerApiTest.java b/apollo-mockserver/src/test/java/com/ctrip/framework/apollo/mockserver/ApolloMockServerApiTest.java index 8c0ffdcd54b..dc930f3e257 100644 --- a/apollo-mockserver/src/test/java/com/ctrip/framework/apollo/mockserver/ApolloMockServerApiTest.java +++ b/apollo-mockserver/src/test/java/com/ctrip/framework/apollo/mockserver/ApolloMockServerApiTest.java @@ -1,6 +1,7 @@ package com.ctrip.framework.apollo.mockserver; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.ctrip.framework.apollo.Config; @@ -8,7 +9,10 @@ import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.model.ConfigChangeEvent; import com.google.common.util.concurrent.SettableFuture; + +import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; + import org.junit.ClassRule; import org.junit.Test; @@ -51,7 +55,79 @@ public void onChange(ConfigChangeEvent changeEvent) { assertEquals(someNewValue, otherConfig.getProperty("key1", null)); assertEquals("otherValue2", otherConfig.getProperty("key2", null)); - assertTrue(changeEvent.isChanged("key1")); } + + @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()); + } + + @Test + public void testDeleteProperties() throws Exception { + Config otherConfig = ConfigService.getConfig(anotherNamespace); + + final SettableFuture 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")); + } + + @Test + public void testDeleteSamePropertyTwice() throws Exception { + Config otherConfig = ConfigService.getConfig(anotherNamespace); + + final Semaphore changes = new Semaphore(0); + + otherConfig.addChangeListener(new ConfigChangeListener() { + @Override + public void onChange(ConfigChangeEvent changeEvent) { + changes.release(); + } + }); + + assertEquals("otherValue6", otherConfig.getProperty("key6", null)); + + embeddedApollo.deleteProperty(anotherNamespace, "key6"); + embeddedApollo.deleteProperty(anotherNamespace, "key6"); + + assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS)); + assertNull(otherConfig.getProperty("key6", null)); + assertEquals(0, changes.availablePermits()); + } } diff --git a/apollo-mockserver/src/test/resources/mockdata-anotherNamespace.properties b/apollo-mockserver/src/test/resources/mockdata-anotherNamespace.properties index 9f6e4ac5dc1..e3c5790d1b5 100644 --- a/apollo-mockserver/src/test/resources/mockdata-anotherNamespace.properties +++ b/apollo-mockserver/src/test/resources/mockdata-anotherNamespace.properties @@ -1,2 +1,6 @@ key1=otherValue1 key2=otherValue2 +key3=otherValue3 +key4=otherValue4 +key5=otherValue5 +key6=otherValue6