Create Duration class and ExpiringSet#getExpiration (prep for #1073)
- Move expiring collections to util.expiring package - Change ExpiringSet to remove expired entries during normal calls
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package fr.xephi.authme.util.expiring;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link Duration}.
|
||||
*/
|
||||
public class DurationTest {
|
||||
|
||||
@Test
|
||||
public void shouldConvertToAppropriateTimeUnit() {
|
||||
check(Duration.createWithSuitableUnit(0, TimeUnit.HOURS),
|
||||
0, TimeUnit.SECONDS);
|
||||
|
||||
check(Duration.createWithSuitableUnit(124, TimeUnit.MINUTES),
|
||||
2, TimeUnit.HOURS);
|
||||
|
||||
check(Duration.createWithSuitableUnit(300, TimeUnit.HOURS),
|
||||
12, TimeUnit.DAYS);
|
||||
|
||||
check(Duration.createWithSuitableUnit(60 * 24 * 50 + 8, TimeUnit.MINUTES),
|
||||
50, TimeUnit.DAYS);
|
||||
|
||||
check(Duration.createWithSuitableUnit(1000L * 60 * 60 * 24 * 7 + 3000, TimeUnit.MILLISECONDS),
|
||||
7, TimeUnit.DAYS);
|
||||
|
||||
check(Duration.createWithSuitableUnit(1000L * 60 * 60 * 3 + 1400, TimeUnit.MILLISECONDS),
|
||||
3, TimeUnit.HOURS);
|
||||
|
||||
check(Duration.createWithSuitableUnit(248, TimeUnit.SECONDS),
|
||||
4, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
private static void check(Duration duration, long expectedDuration, TimeUnit expectedUnit) {
|
||||
assertThat(duration.getTimeUnit(), equalTo(expectedUnit));
|
||||
assertThat(duration.getDuration(), equalTo(expectedDuration));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package fr.xephi.authme.util.expiring;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link ExpiringMap}.
|
||||
*/
|
||||
public class ExpiringMapTest {
|
||||
|
||||
@Test
|
||||
public void shouldAddAndRetrieveEntries() {
|
||||
// given
|
||||
ExpiringMap<String, Double> map = new ExpiringMap<>(3, TimeUnit.MINUTES);
|
||||
|
||||
// when / then
|
||||
map.put("three", 3.0);
|
||||
map.put("treefiddy", 3.50);
|
||||
|
||||
assertThat(map.get("three"), equalTo(3.0));
|
||||
assertThat(map.get("treefiddy"), equalTo(3.50));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveEntry() {
|
||||
// given
|
||||
ExpiringMap<String, Boolean> map = new ExpiringMap<>(1, TimeUnit.HOURS);
|
||||
map.put("hi", true);
|
||||
map.put("ha", false);
|
||||
|
||||
// when
|
||||
map.remove("ha");
|
||||
|
||||
// then
|
||||
assertThat(map.get("ha"), nullValue());
|
||||
assertThat(map.get("hi"), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUpdateExpirationAndSupportNegativeValues() {
|
||||
// given
|
||||
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(2, TimeUnit.DAYS);
|
||||
map.put(2, 4);
|
||||
map.put(3, 9);
|
||||
|
||||
// when
|
||||
map.setExpiration(-100, TimeUnit.MILLISECONDS);
|
||||
|
||||
// then
|
||||
map.put(5, 25);
|
||||
assertThat(map.get(2), equalTo(4));
|
||||
assertThat(map.get(3), equalTo(9));
|
||||
assertThat(map.get(5), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCleanUpExpiredEntries() throws InterruptedException {
|
||||
// given
|
||||
ExpiringMap<Integer, Integer> map = new ExpiringMap<>(200, TimeUnit.MILLISECONDS);
|
||||
map.put(144, 12);
|
||||
map.put(121, 11);
|
||||
map.put(81, 9);
|
||||
map.setExpiration(900, TimeUnit.MILLISECONDS);
|
||||
map.put(64, 8);
|
||||
map.put(25, 5);
|
||||
|
||||
// when
|
||||
Thread.sleep(300);
|
||||
map.removeExpiredEntries();
|
||||
|
||||
// then
|
||||
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,122 @@
|
||||
package fr.xephi.authme.util.expiring;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.hamcrest.Matchers.either;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link ExpiringSet}.
|
||||
*/
|
||||
public class ExpiringSetTest {
|
||||
|
||||
@Test
|
||||
public void shouldAddEntry() {
|
||||
// given
|
||||
ExpiringSet<String> set = new ExpiringSet<>(10, TimeUnit.MINUTES);
|
||||
|
||||
// when
|
||||
set.add("authme");
|
||||
|
||||
// then
|
||||
assertThat(set.contains("authme"), equalTo(true));
|
||||
assertThat(set.contains("other"), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRemoveEntries() {
|
||||
// given
|
||||
ExpiringSet<Integer> set = new ExpiringSet<>(20, TimeUnit.SECONDS);
|
||||
set.add(20);
|
||||
set.add(40);
|
||||
|
||||
// when
|
||||
set.remove(40);
|
||||
set.remove(60);
|
||||
|
||||
// then
|
||||
assertThat(set.contains(20), equalTo(true));
|
||||
assertThat(set.contains(40), equalTo(false));
|
||||
assertThat(set.contains(60), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNewExpirationAndSupportNegativeValues() {
|
||||
// given
|
||||
ExpiringSet<Character> set = new ExpiringSet<>(800, TimeUnit.MILLISECONDS);
|
||||
set.add('A');
|
||||
|
||||
// when
|
||||
set.setExpiration(-10, TimeUnit.SECONDS);
|
||||
set.add('Y');
|
||||
|
||||
// then
|
||||
assertThat(set.contains('A'), equalTo(true));
|
||||
assertThat(set.contains('Y'), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldClearAllValues() {
|
||||
// given
|
||||
ExpiringSet<String> set = new ExpiringSet<>(1, TimeUnit.MINUTES);
|
||||
set.add("test");
|
||||
|
||||
// when / then
|
||||
assertThat(set.isEmpty(), equalTo(false));
|
||||
set.clear();
|
||||
assertThat(set.isEmpty(), equalTo(true));
|
||||
assertThat(set.contains("test"), equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldClearExpiredValues() {
|
||||
// given
|
||||
ExpiringSet<Integer> set = new ExpiringSet<>(2, TimeUnit.HOURS);
|
||||
set.add(2);
|
||||
set.setExpiration(-100, TimeUnit.SECONDS);
|
||||
set.add(3);
|
||||
set.setExpiration(20, TimeUnit.MINUTES);
|
||||
set.add(6);
|
||||
|
||||
// when
|
||||
set.removeExpiredEntries();
|
||||
|
||||
// then
|
||||
assertThat(set.contains(2), equalTo(true));
|
||||
assertThat(set.contains(3), equalTo(false));
|
||||
assertThat(set.contains(6), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnExpiration() {
|
||||
// given
|
||||
ExpiringSet<String> set = new ExpiringSet<>(123, TimeUnit.MINUTES);
|
||||
set.add("my entry");
|
||||
|
||||
// when
|
||||
long expiresInHours = set.getExpiration("my entry", TimeUnit.HOURS);
|
||||
long expiresInMinutes = set.getExpiration("my entry", TimeUnit.MINUTES);
|
||||
long unknownExpires = set.getExpiration("bogus", TimeUnit.SECONDS);
|
||||
|
||||
// then
|
||||
assertThat(expiresInHours, equalTo(2L));
|
||||
assertThat(expiresInMinutes, either(equalTo(122L)).or(equalTo(123L)));
|
||||
assertThat(unknownExpires, equalTo(-1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnMinusOneForExpiredEntry() {
|
||||
// given
|
||||
ExpiringSet<Integer> set = new ExpiringSet<>(-100, TimeUnit.SECONDS);
|
||||
set.add(23);
|
||||
|
||||
// when
|
||||
long expiresInSeconds = set.getExpiration(23, TimeUnit.SECONDS);
|
||||
|
||||
// then
|
||||
assertThat(expiresInSeconds, equalTo(-1L));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package fr.xephi.authme.util.expiring;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user