From 65ca05a18ee9cc761cb4b4fb3c9cc042d338c834 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Sat, 18 Apr 2020 10:35:08 +0300 Subject: [PATCH] Tests to try reproduce #1396 #1388 - Reproduces activity index difference issue on H2 randomly. --- .../database/queries/SessionQueriesTest.java | 63 +++++++++++++++++++ .../src/test/java/utilities/RandomData.java | 4 ++ 2 files changed, 67 insertions(+) diff --git a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/SessionQueriesTest.java b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/SessionQueriesTest.java index 31c0267aa..19f2e76c4 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/SessionQueriesTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/storage/database/queries/SessionQueriesTest.java @@ -16,24 +16,30 @@ */ package com.djrapitops.plan.storage.database.queries; +import com.djrapitops.plan.delivery.domain.TablePlayer; +import com.djrapitops.plan.delivery.domain.container.PlayerContainer; import com.djrapitops.plan.delivery.domain.keys.SessionKeys; import com.djrapitops.plan.delivery.domain.mutators.SessionsMutator; import com.djrapitops.plan.gathering.domain.PlayerKill; import com.djrapitops.plan.gathering.domain.Session; import com.djrapitops.plan.gathering.domain.WorldTimes; import com.djrapitops.plan.storage.database.DatabaseTestPreparer; +import com.djrapitops.plan.storage.database.queries.containers.PlayerContainerQuery; import com.djrapitops.plan.storage.database.queries.objects.KillQueries; +import com.djrapitops.plan.storage.database.queries.objects.ServerTablePlayersQuery; import com.djrapitops.plan.storage.database.queries.objects.SessionQueries; import com.djrapitops.plan.storage.database.queries.objects.WorldTimesQueries; import com.djrapitops.plan.storage.database.transactions.Transaction; import com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction; import com.djrapitops.plan.storage.database.transactions.events.PlayerServerRegisterTransaction; import com.djrapitops.plan.storage.database.transactions.events.WorldNameStoreTransaction; +import com.djrapitops.plugin.api.TimeAmount; import org.junit.jupiter.api.Test; import utilities.RandomData; import utilities.TestConstants; import java.util.*; +import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.*; @@ -257,4 +263,61 @@ public interface SessionQueriesTest extends DatabaseTestPreparer { Collection result = db().query(LargeFetchQueries.fetchAllWorldNames()).get(serverUUID()); assertEquals(new HashSet<>(Arrays.asList(expected)), result); } + + @Test + default void playersTableAndPlayerPagePlaytimeMatches() { + prepareForSessionSave(); + List player1Sessions = RandomData.randomSessions(serverUUID(), worlds, playerUUID, player2UUID); + List player2Sessions = RandomData.randomSessions(serverUUID(), worlds, player2UUID, playerUUID); + player1Sessions.forEach(session -> execute(DataStoreQueries.storeSession(session))); + player2Sessions.forEach(session -> execute(DataStoreQueries.storeSession(session))); + + long playtimeThreshold = RandomData.randomLong(TimeUnit.HOURS.toMillis(1L), TimeUnit.DAYS.toMillis(2L)); + + PlayerContainer playerContainer = db().query(new PlayerContainerQuery(playerUUID)); + TablePlayer tablePlayer = db().query(new ServerTablePlayersQuery(serverUUID(), System.currentTimeMillis(), playtimeThreshold, 5)) + .stream().filter(player -> playerUUID.equals(player.getPlayerUUID())).findAny() + .orElseThrow(AssertionError::new); + + long expected = SessionsMutator.forContainer(playerContainer).toPlaytime(); + long got = tablePlayer.getPlaytime().orElseThrow(AssertionError::new); + assertEquals(expected, got); + } + + @Test + default void playersTableAndPlayerPageActivityIndexMatches() { + prepareForSessionSave(); + List player1Sessions = RandomData.randomSessions(serverUUID(), worlds, playerUUID, player2UUID); + List player2Sessions = RandomData.randomSessions(serverUUID(), worlds, player2UUID, playerUUID); + player1Sessions.forEach(session -> execute(DataStoreQueries.storeSession(session))); + player2Sessions.forEach(session -> execute(DataStoreQueries.storeSession(session))); + + long time = System.currentTimeMillis(); + long playtimeThreshold = RandomData.randomLong(TimeUnit.HOURS.toMillis(1L), TimeUnit.DAYS.toMillis(2L)); + + PlayerContainer playerContainer = db().query(new PlayerContainerQuery(playerUUID)); + TablePlayer tablePlayer = db().query(new ServerTablePlayersQuery(serverUUID(), time, playtimeThreshold, 5)) + .stream().filter(player -> playerUUID.equals(player.getPlayerUUID())).findAny() + .orElseThrow(AssertionError::new); + + SessionsMutator sessionsMutator = SessionsMutator.forContainer(playerContainer); + long week = TimeAmount.WEEK.toMillis(1L); + long weekAgo = time - week; + long twoWeeksAgo = time - 2L * week; + long threeWeeksAgo = time - 3L * week; + SessionsMutator weekOne = sessionsMutator.filterSessionsBetween(weekAgo, time); + SessionsMutator weekTwo = sessionsMutator.filterSessionsBetween(twoWeeksAgo, weekAgo); + SessionsMutator weekThree = sessionsMutator.filterSessionsBetween(threeWeeksAgo, twoWeeksAgo); + + long playtime1 = weekOne.toActivePlaytime(); + long playtime2 = weekTwo.toActivePlaytime(); + long playtime3 = weekThree.toActivePlaytime(); + + double expected = playerContainer.getActivityIndex(time, playtimeThreshold).getValue(); + double got = tablePlayer.getCurrentActivityIndex().orElseThrow(AssertionError::new).getValue(); + assertEquals(expected, got, 0.00001, + () -> "Activity Indexes between queries differed, expected: <" + expected + "> but was: <" + got + ">" + + ". Playtime for reference container: " + ); + } } diff --git a/Plan/common/src/test/java/utilities/RandomData.java b/Plan/common/src/test/java/utilities/RandomData.java index 2809f5174..5ab00b2a9 100644 --- a/Plan/common/src/test/java/utilities/RandomData.java +++ b/Plan/common/src/test/java/utilities/RandomData.java @@ -110,6 +110,10 @@ public class RandomData { return picked; } + public static List randomSessions(UUID serverUUID, String[] worlds, UUID... uuids) { + return pickMultiple(randomInt(5, 50), () -> randomSession(serverUUID, worlds, uuids)); + } + public static Session randomSession(UUID serverUUID, String[] worlds, UUID... uuids) { Session session = new Session(uuids[0], serverUUID, RandomData.randomTime(), pickAtRandom(worlds), randomGameMode()); session.endSession(RandomData.randomTimeAfter(session.getDate()));