|
| 1 | +package org.modelix.model.server |
| 2 | + |
| 3 | +import io.ktor.server.testing.ApplicationTestBuilder |
| 4 | +import io.ktor.server.testing.testApplication |
| 5 | +import mu.KotlinLogging |
| 6 | +import org.modelix.model.IVersion |
| 7 | +import org.modelix.model.client2.HistoryInterval |
| 8 | +import org.modelix.model.client2.IModelClientV2 |
| 9 | +import org.modelix.model.historyAsSequence |
| 10 | +import org.modelix.model.lazy.RepositoryId |
| 11 | +import org.modelix.model.server.api.RepositoryConfig |
| 12 | +import org.modelix.model.server.handlers.IdsApiImpl |
| 13 | +import org.modelix.model.server.handlers.ModelReplicationServer |
| 14 | +import org.modelix.model.server.handlers.RepositoriesManager |
| 15 | +import org.modelix.model.server.store.InMemoryStoreClient |
| 16 | +import kotlin.random.Random |
| 17 | +import kotlin.test.Test |
| 18 | +import kotlin.test.assertEquals |
| 19 | +import kotlin.time.Duration |
| 20 | +import kotlin.time.Duration.Companion.minutes |
| 21 | +import kotlin.time.Duration.Companion.seconds |
| 22 | + |
| 23 | +private val LOG = KotlinLogging.logger { } |
| 24 | + |
| 25 | +class HistoryIndexSessionsTest { |
| 26 | + |
| 27 | + private lateinit var statistics: StoreClientWithStatistics |
| 28 | + private fun runTest(block: suspend ApplicationTestBuilder.() -> Unit) = testApplication { |
| 29 | + application { |
| 30 | + try { |
| 31 | + installDefaultServerPlugins() |
| 32 | + statistics = StoreClientWithStatistics(InMemoryStoreClient()) |
| 33 | + val repoManager = RepositoriesManager(statistics) |
| 34 | + ModelReplicationServer(repoManager).init(this) |
| 35 | + IdsApiImpl(repoManager).init(this) |
| 36 | + } catch (ex: Throwable) { |
| 37 | + LOG.error("", ex) |
| 38 | + } |
| 39 | + } |
| 40 | + block() |
| 41 | + } |
| 42 | + |
| 43 | + @Test fun interval_0_201_1() = runIntervalTest(0, 201, 1.minutes) |
| 44 | + |
| 45 | + @Test fun interval_0_201_2() = runIntervalTest(0, 201, 2.minutes) |
| 46 | + |
| 47 | + @Test fun interval_0_201_3() = runIntervalTest(0, 201, 3.minutes) |
| 48 | + |
| 49 | + @Test fun interval_0_201_4() = runIntervalTest(0, 201, 4.minutes) |
| 50 | + |
| 51 | + @Test fun interval_0_201_5() = runIntervalTest(0, 201, 5.minutes) |
| 52 | + |
| 53 | + @Test fun interval_0_201_6() = runIntervalTest(0, 201, 6.minutes) |
| 54 | + |
| 55 | + @Test fun interval_0_201_7() = runIntervalTest(0, 201, 7.minutes) |
| 56 | + |
| 57 | + @Test fun interval_0_201_8() = runIntervalTest(0, 201, 8.minutes) |
| 58 | + |
| 59 | + @Test fun interval_0_201_9() = runIntervalTest(0, 201, 9.minutes) |
| 60 | + |
| 61 | + @Test fun interval_0_201_10() = runIntervalTest(0, 201, 10.minutes) |
| 62 | + |
| 63 | + @Test fun interval_0_201_11() = runIntervalTest(0, 201, 11.minutes) |
| 64 | + |
| 65 | + @Test fun interval_0_1_5() = runIntervalTest(0, 1, 5.minutes) |
| 66 | + |
| 67 | + @Test fun interval_0_2_5() = runIntervalTest(0, 2, 5.minutes) |
| 68 | + |
| 69 | + @Test fun interval_0_3_5() = runIntervalTest(0, 3, 5.minutes) |
| 70 | + |
| 71 | + @Test fun interval_0_4_5() = runIntervalTest(0, 4, 5.minutes) |
| 72 | + |
| 73 | + @Test fun interval_1_4_5() = runIntervalTest(1, 4, 5.minutes) |
| 74 | + |
| 75 | + @Test fun interval_2_4_5() = runIntervalTest(2, 4, 5.minutes) |
| 76 | + |
| 77 | + @Test fun interval_3_4_5() = runIntervalTest(3, 4, 5.minutes) |
| 78 | + |
| 79 | + @Test fun interval_4_4_5() = runIntervalTest(4, 4, 5.minutes) |
| 80 | + |
| 81 | + @Test fun interval_5_4_5() = runIntervalTest(5, 4, 5.minutes) |
| 82 | + |
| 83 | + private fun runIntervalTest(skip: Int, limit: Int, delay: Duration) = runTest { |
| 84 | + val rand = Random(8923345) |
| 85 | + val modelClient: IModelClientV2 = createModelClient() |
| 86 | + val repositoryId = RepositoryId("repo1") |
| 87 | + val branchRef = repositoryId.getBranchReference() |
| 88 | + val initialVersion = modelClient.initRepository(RepositoryConfig(repositoryId = repositoryId.id, repositoryName = repositoryId.id, modelId = "61bd6cb0-33ff-45d8-9d1b-2149fdb01d16")) |
| 89 | + var currentVersion = initialVersion |
| 90 | + |
| 91 | + var nextTimestamp = currentVersion.getTimestamp()!! + rand.nextInt(0, 3).seconds |
| 92 | + repeat(10) { |
| 93 | + repeat(10) { |
| 94 | + run { |
| 95 | + val newVersion = IVersion.builder() |
| 96 | + .baseVersion(currentVersion) |
| 97 | + .tree(currentVersion.getModelTree()) |
| 98 | + .author("user1") |
| 99 | + .time(nextTimestamp) |
| 100 | + .build() |
| 101 | + currentVersion = modelClient.push(branchRef, newVersion, currentVersion) |
| 102 | + } |
| 103 | + nextTimestamp += rand.nextInt(0, 3).seconds |
| 104 | + run { |
| 105 | + val newVersion = IVersion.builder() |
| 106 | + .baseVersion(currentVersion) |
| 107 | + .tree(currentVersion.getModelTree()) |
| 108 | + .author("user2") |
| 109 | + .time(nextTimestamp) |
| 110 | + .build() |
| 111 | + currentVersion = modelClient.push(branchRef, newVersion, currentVersion) |
| 112 | + } |
| 113 | + nextTimestamp += rand.nextInt(0, 3).seconds |
| 114 | + } |
| 115 | + nextTimestamp += rand.nextInt(1 * 60, 10 * 60).seconds |
| 116 | + } |
| 117 | + |
| 118 | + val expectedHistory = currentVersion.historyAsSequence().toList().reversed() |
| 119 | + |
| 120 | + val expectedIntervals = expectedHistory.fold(listOf<HistoryInterval>()) { acc, version -> |
| 121 | + if (acc.isEmpty() || version.getTimestamp()!! - acc.last().maxTime >= delay) { |
| 122 | + acc + HistoryInterval( |
| 123 | + firstVersionHash = version.getObjectHash(), |
| 124 | + lastVersionHash = version.getObjectHash(), |
| 125 | + size = 1, |
| 126 | + minTime = version.getTimestamp()!!, |
| 127 | + maxTime = version.getTimestamp()!!, |
| 128 | + authors = setOfNotNull(version.getAuthor()), |
| 129 | + ) |
| 130 | + } else { |
| 131 | + val lastInterval = acc.last() |
| 132 | + acc.dropLast(1) + HistoryInterval( |
| 133 | + firstVersionHash = lastInterval.firstVersionHash, |
| 134 | + lastVersionHash = version.getObjectHash(), |
| 135 | + size = lastInterval.size + 1, |
| 136 | + minTime = lastInterval.minTime, |
| 137 | + maxTime = version.getTimestamp()!!, |
| 138 | + authors = lastInterval.authors + listOfNotNull(version.getAuthor()), |
| 139 | + ) |
| 140 | + } |
| 141 | + }.reversed().drop(skip).take(limit) |
| 142 | + |
| 143 | + val timeRange = (expectedIntervals.minOf { it.minTime })..(expectedIntervals.maxOf { it.maxTime }) |
| 144 | + val history = modelClient.getHistorySessions( |
| 145 | + repositoryId = repositoryId, |
| 146 | + headVersion = currentVersion.getObjectHash(), |
| 147 | + timeRange = timeRange, |
| 148 | + delay = delay, |
| 149 | + ) |
| 150 | + assertEquals(expectedIntervals, history) |
| 151 | + } |
| 152 | +} |
0 commit comments