diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/utils/ReferenceConfigCache.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/utils/ReferenceConfigCache.java index 456cd1d9b451d179dbc81916e8a3ced0420028a6..62238e1c72d7c1346e44222f3fac96738b1fdb77 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/utils/ReferenceConfigCache.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/utils/ReferenceConfigCache.java @@ -36,7 +36,7 @@ import java.util.concurrent.ConcurrentMap; public class ReferenceConfigCache { public static final String DEFAULT_NAME = "_DEFAULT_"; - private static final ConcurrentMap cacheHolder = new ConcurrentHashMap(); + static final ConcurrentMap cacheHolder = new ConcurrentHashMap(); /** * Get the cache use default name and {@link #DEFAULT_KEY_GENERATOR} to generate cache key. @@ -74,7 +74,7 @@ public class ReferenceConfigCache { /** * Create the key with the Group, Interface and version attribute of {@link ReferenceConfig}. *

- * eg: group1/com.alibaba.foo.FooService:1.0.0. + * key example: group1/com.alibaba.foo.FooService:1.0.0. */ public static final KeyGenerator DEFAULT_KEY_GENERATOR = new KeyGenerator() { public String generateKey(ReferenceConfig referenceConfig) { @@ -102,7 +102,7 @@ public class ReferenceConfigCache { private final String name; private final KeyGenerator generator; - private ConcurrentMap> cache = new ConcurrentHashMap>(); + ConcurrentMap> cache = new ConcurrentHashMap>(); private ReferenceConfigCache(String name, KeyGenerator generator) { this.name = name; @@ -114,7 +114,7 @@ public class ReferenceConfigCache { ReferenceConfig config = cache.get(key); if(config != null) { - return referenceConfig.get(); + return (T) config.get(); } cache.putIfAbsent(key, referenceConfig); diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/utils/MockReferenceConfig.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/utils/MockReferenceConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..b3ce67285e926ac6d8725e53beb694823683ee0a --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/utils/MockReferenceConfig.java @@ -0,0 +1,56 @@ +/* + * Copyright 1999-2011 Alibaba Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.dubbo.config.utils; + +import com.alibaba.dubbo.config.ReferenceConfig; + +import java.util.concurrent.atomic.AtomicLong; + +/** + * @author ding.lid + */ +public class MockReferenceConfig extends ReferenceConfig { + static AtomicLong counter = new AtomicLong(); + + String value; + + public boolean isGetMethodRun() { + return value != null; + } + + boolean destroyMethodRun = false; + + public boolean isDestroyMethodRun() { + return destroyMethodRun; + } + + public static void setCounter(long c) { + counter.set(c); + } + + @Override + public synchronized String get() { + if(value != null) return value; + + value = "" + counter.getAndIncrement(); + return value; + } + + @Override + public synchronized void destroy() { + destroyMethodRun = true; + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/utils/ReferenceConfigCacheTest.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/utils/ReferenceConfigCacheTest.java new file mode 100644 index 0000000000000000000000000000000000000000..3452c2ebc75e77ccf23a5c6cd7c1823f77096aa3 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/utils/ReferenceConfigCacheTest.java @@ -0,0 +1,162 @@ +/* + * Copyright 1999-2011 Alibaba Group. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.dubbo.config.utils; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * @author ding.lid + */ +public class ReferenceConfigCacheTest { + @Before + public void setUp() throws Exception { + MockReferenceConfig.setCounter(0); + ReferenceConfigCache.cacheHolder.clear(); + } + + @Test + public void testGetCache_SameReference() throws Exception { + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + + { + MockReferenceConfig config = new MockReferenceConfig(); + config.setInterface("FooService"); + config.setGroup("group1"); + config.setVersion("1.0.0"); + + String value = cache.get(config); + assertTrue(config.isGetMethodRun()); + assertEquals("0", value); + } + + { + MockReferenceConfig configCopy = new MockReferenceConfig(); + configCopy.setInterface("FooService"); + configCopy.setGroup("group1"); + configCopy.setVersion("1.0.0"); + + String value = cache.get(configCopy); + assertFalse(configCopy.isGetMethodRun()); + assertEquals("0", value); + } + } + + @Test + public void testGetCache_DiffReference() throws Exception { + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + + { + MockReferenceConfig config = new MockReferenceConfig(); + config.setInterface("FooService"); + config.setGroup("group1"); + config.setVersion("1.0.0"); + + String value = cache.get(config); + assertTrue(config.isGetMethodRun()); + assertEquals("0", value); + } + + { + MockReferenceConfig configCopy = new MockReferenceConfig(); + configCopy.setInterface("XxxService"); + configCopy.setGroup("group1"); + configCopy.setVersion("1.0.0"); + + String value = cache.get(configCopy); + assertTrue(configCopy.isGetMethodRun()); + assertEquals("1", value); + } + } + + @Test + public void testGetCache_DiffName() throws Exception { + { + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + + MockReferenceConfig config = new MockReferenceConfig(); + config.setInterface("FooService"); + config.setGroup("group1"); + config.setVersion("1.0.0"); + + String value = cache.get(config); + assertTrue(config.isGetMethodRun()); + assertEquals("0", value); + } + { + ReferenceConfigCache cache = ReferenceConfigCache.getCache("foo"); + + MockReferenceConfig config = new MockReferenceConfig(); + config.setInterface("FooService"); + config.setGroup("group1"); + config.setVersion("1.0.0"); + + String value = cache.get(config); + assertTrue(config.isGetMethodRun()); // 不同的Cache,相同的ReferenceConfig也会Init + assertEquals("1", value); + } + } + + @Test + public void testDestroy() throws Exception { + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + + MockReferenceConfig config = new MockReferenceConfig(); + config.setInterface("FooService"); + config.setGroup("group1"); + config.setVersion("1.0.0"); + cache.get(config); + MockReferenceConfig configCopy = new MockReferenceConfig(); + configCopy.setInterface("XxxService"); + configCopy.setGroup("group1"); + configCopy.setVersion("1.0.0"); + cache.get(configCopy); + + assertEquals(2, cache.cache.size()); + + cache.destroy(config); + assertTrue(config.isDestroyMethodRun()); + assertEquals(1, cache.cache.size()); + + cache.destroy(configCopy); + assertTrue(configCopy.isDestroyMethodRun()); + assertEquals(0, cache.cache.size()); + } + + @Test + public void testDestroyAll() throws Exception { + ReferenceConfigCache cache = ReferenceConfigCache.getCache(); + + MockReferenceConfig config = new MockReferenceConfig(); + config.setInterface("FooService"); + config.setGroup("group1"); + config.setVersion("1.0.0"); + cache.get(config); + MockReferenceConfig configCopy = new MockReferenceConfig(); + configCopy.setInterface("XxxService"); + configCopy.setGroup("group1"); + configCopy.setVersion("1.0.0"); + cache.get(configCopy); + + assertEquals(2, cache.cache.size()); + + cache.destroyAll(); + assertTrue(config.isDestroyMethodRun()); + assertTrue(configCopy.isDestroyMethodRun()); + assertEquals(0, cache.cache.size()); + } +}