jhalterman / expiringmap

A high performance thread-safe map that expires entries
Apache License 2.0
1k stars 142 forks source link

Variable expiration not working correctly with CREATED policy #68

Closed carloshenrique153 closed 4 years ago

carloshenrique153 commented 4 years ago

Hi, thanks for sharing this library, it's very handy.

Can I get help with the test below? I don't understand why it's failing, it's either a bug or I misinterpreted the documentation, because both keys are expiring at the same time, even though they have different TTL:

import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class VariableExpirationInMemoryCacheTest {

    @Test
    public void shouldSupportVariableExpiration() throws InterruptedException {
        ExpiringMap<String, Object> cache = ExpiringMap.builder()
                .variableExpiration()
                .build();

        cache.put("hello", "world", ExpirationPolicy.CREATED, 1000L, TimeUnit.MILLISECONDS);
        cache.put("foo", "bar", ExpirationPolicy.CREATED, 2000L, TimeUnit.MILLISECONDS);

        // both keys are available in the first second
        assertEquals("world", cache.get("hello"));
        assertEquals("bar", cache.get("foo"));

        // after waiting 1 second, only "hello" should have expired
        Thread.sleep(1001L);
        assertNull(cache.get("hello"));
        assertEquals("foo", cache.get("bar"));   <============   this fails

        // after waiting a total of 2 seconds, both keys should have expired
        Thread.sleep(1000L);
        assertNull(cache.get("hello"));
        assertNull(cache.get("bar"));
    }
}
jfeign commented 4 years ago

Well~, I found something wrong with your test case ^ ^ assertEquals("foo", cache.get("bar")); <============ this fails There is no key "bar" in the your cache, only the key "foo"; I guess you may write like this

`

@Test
public void shouldSupportVariableExpiration() throws InterruptedException {
    ExpiringMap<String, Object> cache = ExpiringMap.builder()
            .variableExpiration()
            .build();

    cache.put("hello", "world", ExpirationPolicy.CREATED, 1000L, TimeUnit.MILLISECONDS);
    cache.put("foo", "bar", ExpirationPolicy.CREATED, 2000L, TimeUnit.MILLISECONDS);

    // both keys are available in the first second
    assertEquals("world", cache.get("hello"));
    assertEquals("bar", cache.get("foo"));

    // after waiting 1 second, only "hello" should have expired
    Thread.sleep(1001L);
    assertNull(cache.get("hello"));
    assertEquals("bar", cache.get("foo"));   

    // after waiting a total of 2 seconds, both keys should have expired
    Thread.sleep(1000L);
    assertNull(cache.get("hello"));
    assertNull(cache.get("bar"));
}

`

carloshenrique153 commented 4 years ago

Omg haha, sorry, thanks for looking into this!