question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

adding async expiration listener outside of builder causes problems

See original GitHub issue

The behavior I’m seeing in my app is that the async expiration listener that I add after building the ExpiringMap doesn’t receive its callbacks and some of the items in the map don’t expire.

You can see a unit test for this on my fork that recreates the problem.

Adding in Builder and After works

ExpiringMap<String, String> map = ExpiringMap.builder()
                .expiration(100, TimeUnit.MILLISECONDS)
                .asyncExpirationListener((thekey, thevalue) -> {
                    System.out.println("builder one called");
                 })
                .build();
map.addAsyncExpirationListener((thekey, thevalue) -> {
     System.out.println("after builder called");
});

Adding only after the builder doesn’t work

ExpiringMap<String, String> map = ExpiringMap.builder()
                .expiration(100, TimeUnit.MILLISECONDS)
                .build();
map.addAsyncExpirationListener((thekey, thevalue) -> {
     System.out.println("after builder called");
});

In this case, the expiration listener isn’t called and I’m also seeing that some items don’t expire from the map. In all cases I’m adding the listener immediately after building the map and before putting any items in it.

Note that in my branch I added priority to the TestNG methods. If I run the whole suite and your existing tests run first then everything works. If I just run my test then it fails. If I sequence the tests such that mine run first then the failure is present. I haven’t dug into the ExpiringMap or its builder to see what might be sensitive to the ordering of the tests but I don’t see anything shared between them the way the existing tests were written.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jhaltermancommented, Jul 17, 2021

This was released in 0.5.10.

1reaction
massfordscommented, Jan 24, 2018

The source of the problem is in ExpiringMap’s private constructor where it only initializes the static LISTENER_SERVICE and THREAD_FACTORY if the builder has async listeners.

    if (LISTENER_SERVICE == null && builder.asyncExpirationListeners != null) {
      synchronized (ExpiringMap.class) {
        if (LISTENER_SERVICE == null) {
          LISTENER_SERVICE = (ThreadPoolExecutor) Executors.newCachedThreadPool(
              THREAD_FACTORY == null ? new NamedThreadFactory("ExpiringMap-Listener-%s") : THREAD_FACTORY);
        }
      }
    }

This explains why the tests need to be ordered they way they are.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Why is my variable unaltered after I modify it inside ...
One word answer: asynchronicity. Forewords. This topic has been iterated at least a couple of thousands of times, here, in Stack Overflow.
Read more >
Implementing an AsyncEventListener for Write-Behind Cache ...
Configuring an AsyncEventListener. To configure a write-behind cache listener, you first configure an asynchronous queue to dispatch the region events, and then ...
Read more >
Handling long Web Requests with Asynchronous Request ...
The problems with long requests run directly off a Web application are many: Long requests can time out the Web server Web servers...
Read more >
Asynchronous Programming in Java - Baeldung
In this tutorial, we'll look at a few ways to achieve asynchronous programming in Java. We'll also explore a few Java libraries that...
Read more >
How JavaScript works: Event loop and the rise of Async ...
The problem most developers new to JavaScript seem to have is understanding ... A Job can also cause more Jobs to be added...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found