Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-2233] fix reference config cache issue #2233 #2347

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ public <T> T get(ReferenceConfig<T> referenceConfig) {
return (T) config.get();
}

cache.putIfAbsent(key, referenceConfig);
config = cache.get(key);
return (T) config.get();
T t = referenceConfig.get();
if (null == t){
return null;
}else{
cache.putIfAbsent(key, referenceConfig);
return t;
}
}

void destroyKey(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class MockReferenceConfig extends ReferenceConfig<String> {

String value;
boolean destroyMethodRun = false;
boolean shouldReturnNull = false;

public static void setCounter(long c) {
counter.set(c);
Expand All @@ -40,12 +41,17 @@ public boolean isDestroyMethodRun() {

@Override
public synchronized String get() {
if (shouldReturnNull) return null;
if (value != null) return value;

value = "" + counter.getAndIncrement();
return value;
}

public void setShouldReturnNull(boolean shouldReturnNull){
this.shouldReturnNull = shouldReturnNull;
}

@Override
public synchronized void destroy() {
destroyMethodRun = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class ReferenceConfigCacheTest {
Expand Down Expand Up @@ -111,4 +112,37 @@ private MockReferenceConfig buildMockReferenceConfig(String service, String grou
config.setVersion(version);
return config;
}

@Test
public void testGetCache_NullReference() throws Exception {
ReferenceConfigCache cache = ReferenceConfigCache.getCache();

{
MockReferenceConfig config = buildMockReferenceConfig("FooService", "group1", "1.0.0");
config.setShouldReturnNull(true);

// first time should get null
String value = cache.get(config);
assertFalse(config.isGetMethodRun());
assertNull(value);
}

{
MockReferenceConfig configCopy = buildMockReferenceConfig("FooService", "group1", "1.0.0");

// second time should get 0
String value = cache.get(configCopy);
assertTrue(configCopy.isGetMethodRun());
assertEquals("0", value);
}

{
MockReferenceConfig configCopy = buildMockReferenceConfig("FooService", "group1", "1.0.0");

// third time should get 0 and this is cache data
String value = cache.get(configCopy);
assertFalse(configCopy.isGetMethodRun());
assertEquals("0", value);
}
}
}