package es.moki.ratelimitj.hazelcast;


import com.google.common.collect.ImmutableSet;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import es.moki.ratelimitj.api.LimitRule;
import es.moki.ratelimitj.api.RateLimiter;
import es.moki.ratelimitj.core.time.time.TimeBanditSupplier;
import es.moki.ratelimitj.core.time.time.TimeSupplier;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import static org.assertj.core.api.Assertions.assertThat;

public class HazelcastRateLimiterInternalTest {

    private static HazelcastInstance hz;

    private TimeBanditSupplier timeBandit = new TimeBanditSupplier();

    @BeforeClass
    public static void before() {
        hz = Hazelcast.newHazelcastInstance();
    }

    @AfterClass
    public static void after() {
        hz.shutdown();
    }

    protected RateLimiter getRateLimiter(Set<LimitRule> rules, TimeSupplier timeSupplier) {
        return new HazelcastSlidingWindowRateLimiter(hz, rules, timeSupplier);
    }

    @Test
    public void shouldCleanUpExpiredKeys() throws Exception {
        ImmutableSet<LimitRule> rules = ImmutableSet.of(LimitRule.of(2, TimeUnit.SECONDS, 5));
        RateLimiter rateLimiter = getRateLimiter(rules, timeBandit);

        IntStream.rangeClosed(1, 5).forEach(value -> {
            timeBandit.addUnixTimeMilliSeconds(100L);
            assertThat(rateLimiter.overLimit("ip:127.0.0.5")).isFalse();
        });

        System.out.println(hz.getMap("ip:127.0.0.5").size());

        Thread.sleep(2500);

        System.out.println(hz.getMap("ip:127.0.0.5").size());
        assertThat(hz.getMap("ip:127.0.0.5").size()).isZero();


    }

}

@@ -162,16 +162,48 @@
 stance;%0A
+import com.hazelcast.core.IMap;%0A
 import e
@@ -1188,16 +1188,26 @@
 d should
+Eventually
 CleanUpE
@@ -1399,24 +1399,62 @@
 meBandit);%0A%0A
+        String key = %22ip:127.0.0.5%22;%0A%0A
         IntS
@@ -1595,30 +1595,19 @@
 erLimit(
-%22ip:127.0.0.5%22
+key
 )).isFal
@@ -1637,63 +1637,89 @@
     
-System.out.println(hz.getMap(%22ip:127.0.0.5%22).size());%0A%0A
+IMap%3CObject, Object%3E map = hz.getMap(key);%0A        while (map.size() != 0) %7B%0A    
     
@@ -1739,24 +1739,21 @@
 eep(
-250
+1
 0);%0A
-%0A
         
 Syst
@@ -1752,106 +1752,32 @@
     
-System.out.println(hz.getMap(%22ip:127.0.0.5%22).size());%0A        assertThat(hz.getMap(%22ip:127.0.0.5%22)
+%7D%0A        assertThat(map
 .siz
@@ -1791,18 +1791,16 @@
 Zero();%0A
-%0A%0A
     %7D%0A%0A%7D

Change to flickering Hazelcast test