Skip to content

Commit

Permalink
Merge pull request #1093 from caarlos0/request-cache
Browse files Browse the repository at this point in the history
Fixed NPE on request cache when HystrixRequestContext is not initialized
  • Loading branch information
mattrjacobs committed Feb 8, 2016
2 parents 1b4c676 + 6363051 commit ec642b0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import rx.Observable;

import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableHolder;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
Expand Down Expand Up @@ -100,6 +101,9 @@ private static HystrixRequestCache getInstance(RequestCacheKey rcKey, HystrixCon
/* package */<T> Observable<T> get(String cacheKey) {
ValueCacheKey key = getRequestCacheKey(cacheKey);
if (key != null) {
if (!HystrixRequestContext.isCurrentThreadInitialized()) {
throw new IllegalStateException("Request caching is not available. Maybe you need to initialize the HystrixRequestContext?");
}
/* look for the stored value */
return (Observable<T>) requestVariableForCache.get(concurrencyStrategy).get(key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public void testCache() {
}
}

@Test(expected = IllegalStateException.class)
public void testCacheWithoutContext() {
HystrixRequestCache.getInstance(
HystrixCommandKey.Factory.asKey("command1"),
HystrixConcurrencyStrategyDefault.getInstance()
).get("any");
}

@Test
public void testClearCache() {
HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance();
Expand Down

0 comments on commit ec642b0

Please sign in to comment.