Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for concurrent tree modifications causing incorrect remote even… #193

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import org.junit.Test
Expand Down Expand Up @@ -2159,6 +2160,78 @@ class JsonTreeTest {
}
}

// Tests for concurrent tree modifications causing incorrect remote event paths.
@Test
fun test_concurrent_tree_style_and_insertion() {
withTwoClientsAndDocuments(syncMode = Client.SyncMode.Manual) { c1, c2, d1, d2, _ ->
updateAndSync(
Updater(c1, d1) { root, _ ->
root.setNewTree(
"t",
element("doc") {
element("p")
},
)
},
Updater(c2, d2),
)
assertTreesXmlEquals("""<doc><p></p></doc>""", d1, d2)

val d1Events = mutableListOf<TreeEditOpInfo>()
val d2Events = mutableListOf<TreeStyleOpInfo>()

val collectJob = launch(start = CoroutineStart.UNDISPATCHED) {
launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<RemoteChange>()
.map {
it.changeInfo.operations.filterIsInstance<TreeEditOpInfo>()
}
.collect(d1Events::addAll)
}
launch(start = CoroutineStart.UNDISPATCHED) {
d2.events.filterIsInstance<RemoteChange>()
.map {
it.changeInfo.operations.filterIsInstance<TreeStyleOpInfo>()
}
.collect(d2Events::addAll)
}
}

listOf(
launch {
repeat(10) {
d1.updateAsync { root, _ ->
root.rootTree().style(0, 1, mapOf("style" to it.toString()))
}.await()
}
},
launch {
d2.updateAsync { root, _ ->
root.rootTree().edit(0, 0, element("p2"))
}.await()
},
).joinAll()

c1.syncAsync().await()
c2.syncAsync().await()
c1.syncAsync().await()

// the resulting trees are the same.
assertEquals(d1.getRoot().rootTree().toXml(), d2.getRoot().rootTree().toXml())

// insert event is sent with path [0]
assertEquals(listOf(0), d1Events.single().fromPath)

// but here test fails.
// style event should be sent with path [1], but path [0] is given.
d2Events.forEach {
assertEquals(listOf(1), it.fromPath)
}

collectJob.cancel()
}
}

companion object {

fun JsonObject.rootTree() = getAs<JsonTree>("t")
Expand Down
Loading