Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using foreach, the keyset method loops infinitely #70

Open
bigzibo opened this issue Aug 26, 2020 · 5 comments
Open

When using foreach, the keyset method loops infinitely #70

bigzibo opened this issue Aug 26, 2020 · 5 comments
Labels

Comments

@bigzibo
Copy link

bigzibo commented Aug 26, 2020

code
public static void main(String[] args) { ExpiringMap<String, String> map = ExpiringMap.builder() .maxSize(100) .expiration(20L, TimeUnit.MINUTES) .expirationPolicy(ExpirationPolicy.ACCESSED) .variableExpiration() .build(); map.put("1", "1"); map.put("2", "2"); map.keySet().forEach((key) -> { System.out.println(map.get(key)); }); }

and result
1 2 1 2 1 2 1 2 .....

@bigzibo
Copy link
Author

bigzibo commented Aug 26, 2020

    <dependency>
        <groupId>net.jodah</groupId>
        <artifactId>expiringmap</artifactId>
        <version>0.5.9</version>
    </dependency>

@jhalterman
Copy link
Owner

Happy to take a PR for this.

@r0b0ji
Copy link
Contributor

r0b0ji commented Oct 9, 2020

There is something wrong when ExpirationPolicy is ACCESSED. For ex:

This works.

 @Test
  public void testLoopTerminates() {
    ExpiringMap<String, String> map = ExpiringMap.builder()
            .maxSize(100)
            .expiration(20L, TimeUnit.MINUTES)
            .expirationPolicy(ExpirationPolicy.CREATED)
            .variableExpiration()
            .build();

    map.put("1", "1");
    map.put("2", "2");

    map.keySet().forEach(key -> System.out.println(map.get(key)));
  }

But this doesn't.

@Test
  public void testLoop() {
    ExpiringMap<String, String> map = ExpiringMap.builder()
            .maxSize(100)
            .expiration(20L, TimeUnit.MINUTES)
            .expirationPolicy(ExpirationPolicy.ACCESSED)
            .variableExpiration()
            .build();

    map.put("1", "1");
    map.put("2", "2");

    map.keySet().forEach(key -> System.out.println(map.get(key)));
  }

@r0b0ji
Copy link
Contributor

r0b0ji commented Oct 9, 2020

This is a bug.

@r0b0ji
Copy link
Contributor

r0b0ji commented Oct 14, 2020

The bug is that get > resetEntry (if policy is accessed) > reorder (removes and add)

This looks like a bug, that it adds again, so when I removed the reorder line, it worked fine but then I am not sure if that is correct fix.

@jhalterman jhalterman added the Bug label Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants