mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-31 07:17:04 +00:00
[AB-xxx] adding unit test and improvement for runtime experience storage
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package dev.sheldan.abstracto.experience.service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import lombok.Getter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -38,6 +40,7 @@ public class RunTimeExperienceService {
|
||||
|
||||
public void cleanupRunTimeStorage() {
|
||||
Instant now = Instant.now();
|
||||
Set<Long> serverIdsToRemove = new HashSet<>();
|
||||
runtimeExperience.forEach((serverId, userInstantMap) -> {
|
||||
List<Long> userIdsToRemove = new ArrayList<>();
|
||||
userInstantMap.forEach((userId, instant) -> {
|
||||
@@ -46,6 +49,10 @@ public class RunTimeExperienceService {
|
||||
}
|
||||
});
|
||||
userIdsToRemove.forEach(userInstantMap::remove);
|
||||
if(userInstantMap.isEmpty()) {
|
||||
serverIdsToRemove.add(serverId);
|
||||
}
|
||||
});
|
||||
serverIdsToRemove.forEach(runtimeExperience::remove);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package dev.sheldan.abstracto.experience.service;
|
||||
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RuntimeExperienceServiceTest {
|
||||
|
||||
@Test
|
||||
public void shouldCleanExpiredExperience() {
|
||||
RunTimeExperienceService experienceService = new RunTimeExperienceService();
|
||||
Map<Long, Instant> mapValue = new HashMap<>(Map.of(2L, Instant.now().minusSeconds(5)));
|
||||
experienceService.getRuntimeExperience().put(1L, mapValue);
|
||||
experienceService.cleanupRunTimeStorage();
|
||||
assertThat(experienceService.getRuntimeExperience()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLeaveExperienceIfNotYetExpired() {
|
||||
RunTimeExperienceService experienceService = new RunTimeExperienceService();
|
||||
Map<Long, Instant> mapValue2 = new HashMap<>(Map.of(3L, Instant.now().plusSeconds(5)));
|
||||
experienceService.getRuntimeExperience().put(2L, mapValue2);
|
||||
experienceService.cleanupRunTimeStorage();
|
||||
assertThat(experienceService.getRuntimeExperience().get(2L)).containsKey(3L);
|
||||
assertThat(experienceService.getRuntimeExperience()).hasSize(1);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user