Skip to content

Stacktrace Missing after adding Listener for a Cache instance #171

@dawnwords

Description

@dawnwords

Version of Cache2k:2.0.0.Final

How to reproduce:

  1. Run the following two unit test cases.
  2. testNpeWithoutListener passes but testNpeWithListener fails
package org.cache2k.test;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.cache2k.event.CacheEntryRemovedListener;
import org.hamcrest.Matchers;
import org.junit.Test;

public class TestCache2k {
  @Test
  public void testNpeWithoutListener() {
    testAndVerifyStacktrace(Cache2kBuilder.of(String.class, String.class)
        .expireAfterWrite(10, TimeUnit.SECONDS)
        .entryCapacity(10)
        // no listener added
        .build());
  }

  @Test
  public void testNpeWithListener() {
    testAndVerifyStacktrace(Cache2kBuilder.of(String.class, String.class)
        .expireAfterWrite(10, TimeUnit.SECONDS)
        .entryCapacity(10)
        // listener added
        .addListener((CacheEntryRemovedListener<String, String>) (cache1, cacheEntry) -> {
        })
        .build());
  }


  private static void testAndVerifyStacktrace(Cache<String, String> cache) {
    try {
      final String value = null;
      cache.computeIfAbsent("npeKey", s -> value.substring(10));
      fail("this shall not pass");
    } catch (NullPointerException e) {
      assertThat(e.getStackTrace()[0].getClassName(), Matchers.is(TestCache2k.class.getName()));
    }
  }
}

Expectations: Both two cases pass, i.e. stacktrace within the callback function of computeIfAbsent should not be replaced by cache

Possible Reason: Stacktrace of the Exception thrown by the client callback function is replaced here

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions