[AB-xxx] adding unit test and improvement for runtime experience storage

This commit is contained in:
Sheldan
2026-03-05 21:11:36 +01:00
parent 3500ec4123
commit 81b1688d97
2 changed files with 39 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}