#949 Created TimedCounter + implement it in TempbanManager

This commit is contained in:
ljacqu
2017-02-18 22:50:30 +01:00
parent ef1d006cdf
commit 152d1dc216
6 changed files with 172 additions and 135 deletions
@@ -1,6 +1,5 @@
package fr.xephi.authme.util;
import fr.xephi.authme.ReflectionTestUtils;
import org.junit.Test;
import java.util.Map;
@@ -45,14 +44,14 @@ public class ExpiringMapTest {
}
@Test
public void shouldUpdateExpiration() {
public void shouldUpdateExpirationAndSupportNegativeValues() {
// given
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(2, TimeUnit.DAYS);
map.put(2, 4);
map.put(3, 9);
// when
map.setExpiration(0, TimeUnit.SECONDS);
map.setExpiration(-100, TimeUnit.MILLISECONDS);
// then
map.put(5, 25);
@@ -61,20 +60,10 @@ public class ExpiringMapTest {
assertThat(map.get(5), nullValue());
}
@Test
public void shouldAcceptNegativeExpiration() {
// given / when
ExpiringMap<Integer, String> map = new ExpiringMap<>(-3, TimeUnit.MINUTES);
map.put(3, "trois");
// then
assertThat(map.get(3), nullValue());
}
@Test
public void shouldCleanUpExpiredEntries() throws InterruptedException {
// given
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(400, TimeUnit.MILLISECONDS);
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(200, TimeUnit.MILLISECONDS);
map.put(144, 12);
map.put(121, 11);
map.put(81, 9);
@@ -83,12 +72,24 @@ public class ExpiringMapTest {
map.put(25, 5);
// when
Thread.sleep(400);
Thread.sleep(300);
map.removeExpiredEntries();
// then
Map<Integer, ?> internalMap = ReflectionTestUtils.getFieldValue(ExpiringMap.class, map, "entries");
Map<Integer, ?> internalMap = map.entries;
assertThat(internalMap.keySet(), containsInAnyOrder(64, 25));
}
@Test
public void shouldReturnIfIsEmpty() {
// given
ExpiringMap<String, String> map = new ExpiringMap<>(-8, TimeUnit.SECONDS);
// when / then
assertThat(map.isEmpty(), equalTo(true));
map.put("hoi", "Welt");
assertThat(map.isEmpty(), equalTo(false));
map.removeExpiredEntries();
assertThat(map.isEmpty(), equalTo(true));
}
}
@@ -0,0 +1,55 @@
package fr.xephi.authme.util;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test for {@link TimedCounter}.
*/
public class TimedCounterTest {
@Test
public void shouldReturnZeroForAnyKey() {
// given
TimedCounter<Double> counter = new TimedCounter<>(1, TimeUnit.DAYS);
// when / then
assertThat(counter.get(2.0), equalTo(0));
assertThat(counter.get(-3.14159), equalTo(0));
}
@Test
public void shouldIncrementCount() {
// given
TimedCounter<String> counter = new TimedCounter<>(10, TimeUnit.MINUTES);
counter.put("moto", 12);
// when
counter.increment("hello");
counter.increment("moto");
// then
assertThat(counter.get("hello"), equalTo(1));
assertThat(counter.get("moto"), equalTo(13));
}
@Test
public void shouldSumUpEntries() {
// given
TimedCounter<String> counter = new TimedCounter<>(90, TimeUnit.SECONDS);
counter.entries.put("expired", new ExpiringMap.ExpiringEntry<>(800, 0));
counter.entries.put("expired2", new ExpiringMap.ExpiringEntry<>(24, System.currentTimeMillis() - 100));
counter.put("other", 10);
counter.put("Another", 4);
// when
int totals = counter.total();
// then
assertThat(totals, equalTo(14));
}
}