Skip to content

Commit

Permalink
Merge 380e5ce into 700bddb
Browse files Browse the repository at this point in the history
  • Loading branch information
r0b0ji committed Oct 8, 2020
2 parents 700bddb + 380e5ce commit 9a93f85
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/jodah/expiringmap/ExpiringMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,15 @@ public synchronized void addAsyncExpirationListener(ExpirationListener<K, V> lis
if (asyncExpirationListeners == null)
asyncExpirationListeners = new CopyOnWriteArrayList<ExpirationListener<K, V>>();
asyncExpirationListeners.add(listener);
// If asyncListener was not added on Builder, LISTENER_SERVICE was not initialized and remain null
if (LISTENER_SERVICE == null) {
synchronized (ExpiringMap.class) {
if (LISTENER_SERVICE == null) {
LISTENER_SERVICE = (ThreadPoolExecutor) Executors.newCachedThreadPool(
THREAD_FACTORY == null ? new NamedThreadFactory("ExpiringMap-Listener-%s") : THREAD_FACTORY);
}
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package net.jodah.expiringmap.functional;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.BeforeMethod;
Expand All @@ -9,7 +12,8 @@
import net.jodah.concurrentunit.Waiter;
import net.jodah.expiringmap.ExpiringMap;

@Test
import static org.testng.Assert.assertEquals;

public class ExpirationListenerTest {
private Waiter waiter;

Expand All @@ -21,6 +25,7 @@ protected void beforeMethod() {
/**
* Tests that an expiration listener is called as expected.
*/
@Test(priority = 100)
public void shouldCallExpirationListener() throws Throwable {
final String key = "a";
final String value = "v";
Expand All @@ -42,13 +47,14 @@ public void shouldCallExpirationListener() throws Throwable {
/**
* Tests that an async expiration listener is called as expected.
*/
@Test(priority = 100)
public void shouldCallAsyncExpirationListener() throws Throwable {
final String key = "a";
final String value = "v";

Map<String, String> map = ExpiringMap.builder()
.expiration(100, TimeUnit.MILLISECONDS)
.expirationListener((thekey, thevalue) -> {
.asyncExpirationListener((thekey, thevalue) -> {
waiter.assertEquals(key, thekey);
waiter.assertEquals(value, thevalue);
waiter.resume();
Expand All @@ -59,4 +65,88 @@ public void shouldCallAsyncExpirationListener() throws Throwable {

waiter.await(5000);
}

@Test(priority = 10)
public void shouldCallListenerWhenAddedOnMap() throws Throwable {
final String key = "a";
final String value = "v";

ExpiringMap<String, String> map = ExpiringMap.builder()
.expiration(100, TimeUnit.MILLISECONDS)
.build();

map.addExpirationListener((thekey, thevalue) -> {
waiter.assertEquals(key, thekey);
waiter.assertEquals(value, thevalue);
waiter.resume();
});

map.put(key, value);

waiter.await(5000);
}

@Test(priority = 10)
public void shouldCallAsyncListenerWhenAddedOnMap() throws Throwable {
final String key = "a";
final String value = "v";

ExpiringMap<String, String> map = ExpiringMap.builder()
.expiration(100, TimeUnit.MILLISECONDS)
.build();

map.addAsyncExpirationListener((thekey, thevalue) -> {
waiter.assertEquals(key, thekey);
waiter.assertEquals(value, thevalue);
waiter.resume();
});

map.put(key, value);

waiter.await(5000);
}

@Test(priority = 1)
public void shouldExpireAllWhenListenerAddedOnMap() throws Throwable {
CountDownLatch latch = new CountDownLatch(10);
Set<Integer> expiredKeys = new HashSet<>();

ExpiringMap<Integer, Integer> map = ExpiringMap.builder()
.expiration(100, TimeUnit.MILLISECONDS)
.build();

map.addExpirationListener((thekey, thevalue) -> {
expiredKeys.add(thekey);
latch.countDown();
});

for (int i = 0; i < 10; i++) {
map.put(i, i);
}

latch.await(5, TimeUnit.SECONDS);
assertEquals(expiredKeys.size(), 10);
}

@Test(priority = 1)
public void shouldExpireAllWhenAsyncListenerAddedOnMap() throws Throwable {
CountDownLatch latch = new CountDownLatch(10);
Set<Integer> expiredKeys = new HashSet<>();

ExpiringMap<Integer, Integer> map = ExpiringMap.builder()
.expiration(100, TimeUnit.MILLISECONDS)
.build();

map.addAsyncExpirationListener((thekey, thevalue) -> {
expiredKeys.add(thekey);
latch.countDown();
});

for (int i = 0; i < 10; i++) {
map.put(i, i);
}

latch.await(5, TimeUnit.SECONDS);
assertEquals(expiredKeys.size(), 10);
}
}

0 comments on commit 9a93f85

Please sign in to comment.