提交 c910f8b2 编写于 作者: oldratlee's avatar oldratlee 🔥

添加 UT DUBBO-475 ReferenceConfig支持按group+interface+version缓存

上级 3c5d9e33
......@@ -36,7 +36,7 @@ import java.util.concurrent.ConcurrentMap;
public class ReferenceConfigCache {
public static final String DEFAULT_NAME = "_DEFAULT_";
private static final ConcurrentMap<String, ReferenceConfigCache> cacheHolder = new ConcurrentHashMap<String, ReferenceConfigCache>();
static final ConcurrentMap<String, ReferenceConfigCache> cacheHolder = new ConcurrentHashMap<String, ReferenceConfigCache>();
/**
* 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 <b>Group</b>, <b>Interface</b> and <b>version</b> attribute of {@link ReferenceConfig}.
* <p>
* eg: <code>group1/com.alibaba.foo.FooService:1.0.0</code>.
* key example: <code>group1/com.alibaba.foo.FooService:1.0.0</code>.
*/
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<String, ReferenceConfig<?>> cache = new ConcurrentHashMap<String, ReferenceConfig<?>>();
ConcurrentMap<String, ReferenceConfig<?>> cache = new ConcurrentHashMap<String, ReferenceConfig<?>>();
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);
......
/*
* 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<String> {
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;
}
}
/*
* 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());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册