|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.modelix.model.server |
| 18 | + |
| 19 | +import io.ktor.server.testing.ApplicationTestBuilder |
| 20 | +import io.ktor.server.testing.testApplication |
| 21 | +import io.ktor.util.reflect.instanceOf |
| 22 | +import kotlinx.coroutines.CoroutineScope |
| 23 | +import kotlinx.coroutines.Dispatchers |
| 24 | +import kotlinx.coroutines.launch |
| 25 | +import org.junit.Assert.assertFalse |
| 26 | +import org.modelix.authorization.installAuthentication |
| 27 | +import org.modelix.model.api.ChildLinkFromName |
| 28 | +import org.modelix.model.api.ConceptReference |
| 29 | +import org.modelix.model.api.IBranch |
| 30 | +import org.modelix.model.api.ITree |
| 31 | +import org.modelix.model.api.PBranch |
| 32 | +import org.modelix.model.api.getRootNode |
| 33 | +import org.modelix.model.client2.ModelClientV2 |
| 34 | +import org.modelix.model.client2.ReplicatedModel |
| 35 | +import org.modelix.model.lazy.BranchReference |
| 36 | +import org.modelix.model.lazy.CLTree |
| 37 | +import org.modelix.model.lazy.CLVersion |
| 38 | +import org.modelix.model.lazy.RepositoryId |
| 39 | +import org.modelix.model.operations.OTBranch |
| 40 | +import org.modelix.model.server.handlers.ModelReplicationServer |
| 41 | +import org.modelix.model.server.store.InMemoryStoreClient |
| 42 | +import org.modelix.model.server.store.forContextRepository |
| 43 | +import java.util.UUID |
| 44 | +import java.util.concurrent.CountDownLatch |
| 45 | +import java.util.concurrent.TimeUnit |
| 46 | +import kotlin.test.Test |
| 47 | +import kotlin.test.assertEquals |
| 48 | +import kotlin.test.assertTrue |
| 49 | + |
| 50 | +class ReplicatedModelTest { |
| 51 | + |
| 52 | + @Test |
| 53 | + fun startsWithLatestVersion() = runTest { |
| 54 | + val client = createModelClient() |
| 55 | + val repositoryId = RepositoryId(UUID.randomUUID().toString()) |
| 56 | + |
| 57 | + // Step 1: prepare repository with two versions beside the initial version |
| 58 | + // Step 1.1: create an empty repository |
| 59 | + val initialVersion = client.initRepository(repositoryId) as CLVersion |
| 60 | + |
| 61 | + // Step 1.2: add a new child node to get a new version |
| 62 | + val defaultBranchReference = repositoryId.getBranchReference() |
| 63 | + addHelloChild(initialVersion, client, defaultBranchReference) |
| 64 | + |
| 65 | + // Step 2: in a new client, fetch the latest repository data |
| 66 | + val newClient = createModelClient() |
| 67 | + // we do not provide an initial version, so we expect to fetch the latest one (with one "hello" child) |
| 68 | + val scope = CoroutineScope(Dispatchers.Default) |
| 69 | + val replicatedModel = ReplicatedModel(newClient, defaultBranchReference, scope) |
| 70 | + try { |
| 71 | + replicatedModel.getBranch() |
| 72 | + // if we get here, then we missed an expected exception |
| 73 | + assertFalse(true) |
| 74 | + } catch (ex: Exception) { |
| 75 | + /* |
| 76 | + Expected exception, because we did not specify an initial version. |
| 77 | + So without an explicit start we do not expect anything useful here. |
| 78 | + */ |
| 79 | + assertTrue(ex.instanceOf(IllegalStateException::class)) |
| 80 | + } |
| 81 | + |
| 82 | + val branch = replicatedModel.start() |
| 83 | + // Step 3: wait a bit so replicated model can fetch the new versions from the server |
| 84 | + waitUntilChildArrives(branch, scope, 500) |
| 85 | + |
| 86 | + // Step 4: check, eventually we must have the one "hello" child |
| 87 | + val children = getHelloChildrenOfRootNode(branch) |
| 88 | + assertEquals(1, children.size) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun startsWithSpecificVersion() = runTest { |
| 93 | + val client = createModelClient() |
| 94 | + val repositoryId = RepositoryId(UUID.randomUUID().toString()) |
| 95 | + val defaultBranchReference = repositoryId.getBranchReference() |
| 96 | + |
| 97 | + // Step 1: prepare repository with two versions beside the initial version |
| 98 | + // Step 1.1: create an empty repository |
| 99 | + val initialVersion = client.initRepository(repositoryId) as CLVersion |
| 100 | + |
| 101 | + // Step 1.2: add a new child node to get a new version |
| 102 | + addHelloChild(initialVersion, client, defaultBranchReference) |
| 103 | + |
| 104 | + // Step 2: in a new client, fetch the oneHelloChildVersion |
| 105 | + val newClient = createModelClient() |
| 106 | + |
| 107 | + // Step 2.1: to avoid version was not created by this client exception |
| 108 | + val initialVersionClone = newClient.loadVersion(repositoryId, initialVersion.getContentHash(), null) |
| 109 | + |
| 110 | + val scope = CoroutineScope(Dispatchers.Default) |
| 111 | + // Step 2.2: we provide an initial version, so we expect to fetch it first (0 "hello" child) |
| 112 | + val replicatedModel = ReplicatedModel( |
| 113 | + newClient, |
| 114 | + defaultBranchReference, |
| 115 | + providedScope = scope, |
| 116 | + initialRemoteVersion = initialVersionClone as CLVersion, |
| 117 | + ) |
| 118 | + val branch = replicatedModel.getBranch() |
| 119 | + |
| 120 | + // Step 3: check, here we must have 0 "hello" child |
| 121 | + val emptyChildren = getHelloChildrenOfRootNode(branch) |
| 122 | + assertTrue(emptyChildren.isEmpty()) |
| 123 | + |
| 124 | + replicatedModel.start() |
| 125 | + // Step 4: wait a bit so replicated model can fetch the new versions from the server |
| 126 | + waitUntilChildArrives(branch, scope, 500) |
| 127 | + |
| 128 | + // Step 5: check, eventually we must have 1 "hello" child |
| 129 | + val children = getHelloChildrenOfRootNode(branch) |
| 130 | + assertEquals(1, children.size) |
| 131 | + } |
| 132 | + |
| 133 | + private fun runTest(block: suspend ApplicationTestBuilder.() -> Unit) = testApplication { |
| 134 | + application { |
| 135 | + installAuthentication(unitTestMode = true) |
| 136 | + installDefaultServerPlugins() |
| 137 | + ModelReplicationServer(InMemoryStoreClient().forContextRepository()).init(this) |
| 138 | + } |
| 139 | + block() |
| 140 | + } |
| 141 | + |
| 142 | + private fun waitUntilChildArrives(branch: IBranch, scope: CoroutineScope, timeout: Long) { |
| 143 | + val barrier = CountDownLatch(1) |
| 144 | + scope.launch { |
| 145 | + var childArrived = false |
| 146 | + while (!childArrived) { |
| 147 | + childArrived = getHelloChildrenOfRootNode(branch).isNotEmpty() |
| 148 | + } |
| 149 | + barrier.countDown() |
| 150 | + } |
| 151 | + // wait at most timeout ms for the child to arrive |
| 152 | + barrier.await(timeout, TimeUnit.MILLISECONDS) |
| 153 | + } |
| 154 | + |
| 155 | + private suspend fun addHelloChild( |
| 156 | + baseVersion: CLVersion, |
| 157 | + client: ModelClientV2, |
| 158 | + branchReference: BranchReference, |
| 159 | + ): CLVersion { |
| 160 | + val branch = |
| 161 | + OTBranch( |
| 162 | + PBranch(baseVersion.getTree(), client.getIdGenerator()), |
| 163 | + client.getIdGenerator(), |
| 164 | + baseVersion.store, |
| 165 | + ) |
| 166 | + branch.runWriteT { t -> |
| 167 | + t.addNewChild(ITree.ROOT_ID, "hello", -1, null as ConceptReference?) |
| 168 | + } |
| 169 | + val (ops, newTree) = branch.getPendingChanges() |
| 170 | + val newVersion = CLVersion.createRegularVersion( |
| 171 | + client.getIdGenerator().generate(), |
| 172 | + null, |
| 173 | + null, |
| 174 | + newTree as CLTree, |
| 175 | + baseVersion, |
| 176 | + ops.map { it.getOriginalOp() }.toTypedArray(), |
| 177 | + ) |
| 178 | + return client.push(branchReference, newVersion, baseVersion) as CLVersion |
| 179 | + } |
| 180 | + |
| 181 | + private fun getHelloChildrenOfRootNode(branch: IBranch) = |
| 182 | + branch.computeReadT { it.branch.getRootNode().getChildren(ChildLinkFromName("hello")).toList() } |
| 183 | +} |
0 commit comments