|
| 1 | +/* |
| 2 | + * Copyright (c) 2023. |
| 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 | +import org.modelix.model.VersionMerger |
| 18 | +import org.modelix.model.api.IIdGenerator |
| 19 | +import org.modelix.model.api.ITree |
| 20 | +import org.modelix.model.api.IWriteTransaction |
| 21 | +import org.modelix.model.client.IdGenerator |
| 22 | +import org.modelix.model.lazy.CLTree |
| 23 | +import org.modelix.model.lazy.CLVersion |
| 24 | +import org.modelix.model.lazy.ObjectStoreCache |
| 25 | +import org.modelix.model.lazy.runWrite |
| 26 | +import org.modelix.model.persistent.MapBaseStore |
| 27 | +import kotlin.test.Test |
| 28 | +import kotlin.test.assertContains |
| 29 | +import kotlin.test.assertEquals |
| 30 | + |
| 31 | +/** |
| 32 | + * This tests indirectly the algorithm in LinearHistory, by creating and merging versions. |
| 33 | + */ |
| 34 | +class MergeOrderTest { |
| 35 | + private var store: MapBaseStore = MapBaseStore() |
| 36 | + private var storeCache: ObjectStoreCache = ObjectStoreCache(store) |
| 37 | + private var idGenerator: IIdGenerator = IdGenerator.newInstance(3) |
| 38 | + |
| 39 | + fun CLVersion.runWrite(id: Long, body: IWriteTransaction.() -> Unit): CLVersion { |
| 40 | + return nextId(id) { runWrite(idGenerator, null, { it.apply(body) }) } |
| 41 | + } |
| 42 | + fun CLVersion.runWrite(body: IWriteTransaction.() -> Unit) = runWrite(idGenerator, null, { it.apply(body) }) |
| 43 | + fun merge(v1: CLVersion, v2: CLVersion) = VersionMerger(storeCache, idGenerator).mergeChange(v1, v2) |
| 44 | + fun merge(id: Long, v1: CLVersion, v2: CLVersion) = nextId(id) { VersionMerger(storeCache, idGenerator).mergeChange(v1, v2) } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun mergeOrderShouldNotMatter() = mergeOrderShouldNotMatter(false) |
| 48 | + |
| 49 | + @Test |
| 50 | + fun mergeOrderShouldNotMatterAlternativeIds() = mergeOrderShouldNotMatter(true) |
| 51 | + |
| 52 | + fun mergeOrderShouldNotMatter(alternativeIds: Boolean) { |
| 53 | + val merger = VersionMerger(storeCache, idGenerator) |
| 54 | + |
| 55 | + val v0 = CLVersion.createRegularVersion( |
| 56 | + idGenerator.generate(), |
| 57 | + null, |
| 58 | + null, |
| 59 | + CLTree(storeCache), |
| 60 | + null, |
| 61 | + emptyArray(), |
| 62 | + ) |
| 63 | + |
| 64 | + // There are two clients working on the same version and both change the same property. |
| 65 | + // This creates a conflict. |
| 66 | + val va1 = v0.runWrite(0xa1) { setProperty(ITree.ROOT_ID, "name", "MyClassA") } |
| 67 | + assertEquals("MyClassA", va1.getTree().getProperty(ITree.ROOT_ID, "name")) |
| 68 | + val vb1 = v0.runWrite(0xb1) { setProperty(ITree.ROOT_ID, "name", "MyClassB") } |
| 69 | + assertEquals("MyClassB", vb1.getTree().getProperty(ITree.ROOT_ID, "name")) |
| 70 | + |
| 71 | + // Now both clients exchange their modifications ... |
| 72 | + |
| 73 | + // In the meantime, Client A keeps working on his branch and creates a new version that doesn't actually |
| 74 | + // change anything, so should not have an effect on the following merge. |
| 75 | + // It changes an unrelated part of the model that wouldn't cause a conflict even if the operations were ordered |
| 76 | + // in a completely unexpected way. |
| 77 | + val va2 = va1.runWrite(0xa2L + (if (alternativeIds) 0x100 else 0)) { |
| 78 | + setProperty(ITree.ROOT_ID, "unrelated", "Class renamed") |
| 79 | + setProperty(ITree.ROOT_ID, "unrelated", null) |
| 80 | + } |
| 81 | + assertEquals("MyClassA", va2.getTree().getProperty(ITree.ROOT_ID, "name")) |
| 82 | + |
| 83 | + // Client B receives the version with the first property change from client A, but not the second empty change. |
| 84 | + val vb2 = merge(0xb2, vb1, va1) |
| 85 | + assertContains(setOf("MyClassA", "MyClassB"), vb2.getTree().getProperty(ITree.ROOT_ID, "name")) |
| 86 | + |
| 87 | + // Client A receives the version with the property change from client B. |
| 88 | + val va3 = merge(0xa3, va2, vb1) |
| 89 | + assertContains(setOf("MyClassA", "MyClassB"), va3.getTree().getProperty(ITree.ROOT_ID, "name")) |
| 90 | + |
| 91 | + // The history should now look like this: |
| 92 | + // |
| 93 | + // v0 |
| 94 | + // / \ |
| 95 | + // va1 vb1 |
| 96 | + // | \ / | |
| 97 | + // va2 /\ | |
| 98 | + // | / \ | |
| 99 | + // |/ \ | |
| 100 | + // va3 vb2 |
| 101 | + // |
| 102 | + // - va1 and vb2 contain the property change |
| 103 | + // - va2 didn't change anything |
| 104 | + // - va3 and vb2 are the current head versions that merged both property changes, |
| 105 | + // and are expected to contain the same resulting model. |
| 106 | + |
| 107 | + // After Client B receives another update from Client A that includes the empty change va2 |
| 108 | + // there shouldn't be any doubt that the merge result has to be identical ... |
| 109 | + assertEquals( |
| 110 | + va3.getTree().getProperty(ITree.ROOT_ID, "name"), |
| 111 | + merger.mergeChange(vb2, va2).getTree().getProperty(ITree.ROOT_ID, "name"), |
| 112 | + ) |
| 113 | + assertSameTree(va3.getTree(), merger.mergeChange(vb2, va2).getTree()) |
| 114 | + |
| 115 | + // ... but even the previous merge should have an identical result. |
| 116 | + // It would be confusing for the user if the merge algorithm keeps changing its mind about the result for |
| 117 | + // no obvious reason. |
| 118 | + assertEquals( |
| 119 | + va3.getTree().getProperty(ITree.ROOT_ID, "name"), |
| 120 | + vb2.getTree().getProperty(ITree.ROOT_ID, "name"), |
| 121 | + ) |
| 122 | + assertSameTree(va3.getTree(), vb2.getTree()) |
| 123 | + } |
| 124 | + |
| 125 | + fun <R> nextId(id: Long, body: () -> R): R { |
| 126 | + val saved = idGenerator |
| 127 | + try { |
| 128 | + idGenerator = object : IIdGenerator { |
| 129 | + override fun generate(): Long = id |
| 130 | + } |
| 131 | + return body() |
| 132 | + } finally { |
| 133 | + idGenerator = saved |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments