Ehcache在测试中挂起

发布于 2024-09-02 06:39:48 字数 3083 浏览 3 评论 0原文

我正在重写我所在项目代码中的瓶颈,在此过程中,我正在创建一个包含自填充 Ehcache 的顶级项目。我试图编写一个测试来确保建立基本的调用链,但是当测试执行时,它会从缓存中检索项目。

以下是设置和测试,以供参考使用 Mockito 进行模拟:

@Before
public void SetUp()
{
    testCache = new Cache(getTestCacheConfiguration());
    recordingFactory = new EntryCreationRecordingCache();
    service = new Service<Request, Response>(testCache, recordingFactory); 
}

@Test
public void retrievesResultsFromSuppliedCache()
{
    ResultType resultType = mock(ResultType.class);
    Response expectedResponse = mock(Response.class);
    addToExpectedResults(resultType, expectedResponse);
    Request request = mock(Request.class);
    when(request.getResultType()).thenReturn(resultType);

    assertThat(service.getResponse(request), sameInstance(expectedResponse));
    assertTrue(recordingFactory.requestList.contains(request));
}

private void addToExpectedResults(ResultType resultType,
        Response response) {
    recordingFactory.responseMap.put(resultType, response);

}

private CacheConfiguration getTestCacheConfiguration() {
    CacheConfiguration cacheConfiguration = new CacheConfiguration("TEST_CACHE", 10);
    cacheConfiguration.setLoggingEnabled(false);
    return cacheConfiguration;
}

private class EntryCreationRecordingCache extends ResponseFactory{

    public final Map<ResultType, Response> responseMap = new ConcurrentHashMap<ResultType, Response>();
    public final List<Request> requestList = new ArrayList<Request>();

    @Override
    protected Map<ResultType, Response> generateResponse(Request request) {
        requestList.add(request);
        return responseMap;
    }
}

这是 ServiceClass

public class Service<K extends Request, V extends Response> {

    private Ehcache cache;

    public Service(Ehcache cache, ResponseFactory factory) {
        this.cache = new SelfPopulatingCache(cache, factory);
    }

    @SuppressWarnings("unchecked")
    public V getResponse(K request)
    {
        ResultType resultType = request.getResultType();
        Element cacheEntry = cache.get(request);
        V response = null;
        if(cacheEntry != null){
            Map<ResultType, Response> resultTypeMap = (Map<ResultType, Response>) cacheEntry.getValue();
            try{
                response = (V) resultTypeMap.get(resultType);
            }catch(NullPointerException e){
                throw new RuntimeException("Result type not found for Result Type: " + resultType);
            }catch(ClassCastException e){
                throw new RuntimeException("Incorrect Response Type for Result Type: " + resultType);
            }
        }
        return response;
    }
}

这是 ResponseFactory:

public abstract class ResponseFactory implements CacheEntryFactory{

    @Override
    public final Object createEntry(Object request) throws Exception {
        return generateResponse((Request)request);
    }

    protected abstract Map<ResultType,Response> generateResponse(Request request);
}

I am in the process of rewriting a bottle neck in the code of the project I am on, and in doing so I am creating a top level item that contains a self populating Ehcache. I am attempting to write a test to make sure that the basic call chain is established, but when the test executes it hands when retrieving the item from the cache.

Here are the Setup and the test, for reference mocking is being done with Mockito:

@Before
public void SetUp()
{
    testCache = new Cache(getTestCacheConfiguration());
    recordingFactory = new EntryCreationRecordingCache();
    service = new Service<Request, Response>(testCache, recordingFactory); 
}

@Test
public void retrievesResultsFromSuppliedCache()
{
    ResultType resultType = mock(ResultType.class);
    Response expectedResponse = mock(Response.class);
    addToExpectedResults(resultType, expectedResponse);
    Request request = mock(Request.class);
    when(request.getResultType()).thenReturn(resultType);

    assertThat(service.getResponse(request), sameInstance(expectedResponse));
    assertTrue(recordingFactory.requestList.contains(request));
}

private void addToExpectedResults(ResultType resultType,
        Response response) {
    recordingFactory.responseMap.put(resultType, response);

}

private CacheConfiguration getTestCacheConfiguration() {
    CacheConfiguration cacheConfiguration = new CacheConfiguration("TEST_CACHE", 10);
    cacheConfiguration.setLoggingEnabled(false);
    return cacheConfiguration;
}

private class EntryCreationRecordingCache extends ResponseFactory{

    public final Map<ResultType, Response> responseMap = new ConcurrentHashMap<ResultType, Response>();
    public final List<Request> requestList = new ArrayList<Request>();

    @Override
    protected Map<ResultType, Response> generateResponse(Request request) {
        requestList.add(request);
        return responseMap;
    }
}

Here is the ServiceClass

public class Service<K extends Request, V extends Response> {

    private Ehcache cache;

    public Service(Ehcache cache, ResponseFactory factory) {
        this.cache = new SelfPopulatingCache(cache, factory);
    }

    @SuppressWarnings("unchecked")
    public V getResponse(K request)
    {
        ResultType resultType = request.getResultType();
        Element cacheEntry = cache.get(request);
        V response = null;
        if(cacheEntry != null){
            Map<ResultType, Response> resultTypeMap = (Map<ResultType, Response>) cacheEntry.getValue();
            try{
                response = (V) resultTypeMap.get(resultType);
            }catch(NullPointerException e){
                throw new RuntimeException("Result type not found for Result Type: " + resultType);
            }catch(ClassCastException e){
                throw new RuntimeException("Incorrect Response Type for Result Type: " + resultType);
            }
        }
        return response;
    }
}

And here is the ResponseFactory:

public abstract class ResponseFactory implements CacheEntryFactory{

    @Override
    public final Object createEntry(Object request) throws Exception {
        return generateResponse((Request)request);
    }

    protected abstract Map<ResultType,Response> generateResponse(Request request);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懵少女 2024-09-09 06:39:48

经过一番研究后,我发现缓存没有被初始化。创建 CacheManager 并向其添加缓存解决了该问题。

After wrestling with it for a while, I discovered that the cache wasn't being initialized. Creating a CacheManager and adding the cache to it resolved the problem.

彩虹直至黑白 2024-09-09 06:39:48

我也遇到了 EHCache 挂起的问题,尽管只是在 hello-world 示例中。将其添加到末尾修复了它(应用程序正常结束)。

CacheManager.getInstance().removeAllCaches();

https://stackoverflow.com/a/20731502/2736496

I also had a problem with EHCache hanging, although only in a hello-world example. Adding this to the end fixed it (the application ends normally).

CacheManager.getInstance().removeAllCaches();

https://stackoverflow.com/a/20731502/2736496

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文