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

Prevent remote-change events in RealtimeSyncOff mode #189

Merged
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android.suppressUnsupportedOptionWarnings=android.suppressUnsupportedOptionWarni
kotlin.code.style=official
kotlin.mpp.stability.nowarn=true
GROUP=dev.yorkie
VERSION_NAME=0.4.19-rc2
VERSION_NAME=0.4.20
POM_DESCRIPTION=Document store for building collaborative editing applications.
POM_INCEPTION_YEAR=2022
POM_URL=https://github.com/yorkie-team/yorkie-android-sdk
Expand Down
71 changes: 71 additions & 0 deletions yorkie/src/androidTest/kotlin/dev/yorkie/core/ClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,75 @@ class ClientTest {
collectJobs.forEach(Job::cancel)
}
}

@Test
fun test_prevent_remote_changes_in_sync_off() {
withTwoClientsAndDocuments { c1, c2, d1, d2, _ ->
val d1Events = mutableListOf<Document.Event>()
val d2Events = mutableListOf<Document.Event>()
val collectJobs = listOf(
launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filter { it is RemoteChange || it is LocalChange }
.collect(d1Events::add)
},
launch(start = CoroutineStart.UNDISPATCHED) {
d2.events.filter { it is RemoteChange || it is LocalChange }
.collect(d2Events::add)
},
)

d1.updateAsync { root, _ ->
root.setNewTree(
"t",
element("doc") {
element("p") { text { "12" } }
element("p") { text { "34" } }
},
)
}.await()

withTimeout(GENERAL_TIMEOUT) {
while (d2Events.isEmpty()) {
delay(50)
}
}
assertIs<RemoteChange>(d2Events.first())
assertTreesXmlEquals("<doc><p>12</p><p>34</p></doc>", d1, d2)

d1.updateAsync { root, _ ->
root.rootTree().edit(2, 2, text { "a" })
}.await()
c1.syncAsync().await()

// Simulate the situation in the runSyncLoop where a pushpull request has been sent
// but a response has not yet been received.
d2Events.clear()
val deferred = c2.syncAsync()
c2.changeSyncMode(d2, RealtimeSyncOff)
deferred.await()

// In push-only mode, remote-change events should not occur.
d2Events.clear()
c2.changeSyncMode(d2, RealtimeSyncOff)

delay(100) // Keep the sync-off state.
assertTrue(d2Events.none { it is RemoteChange })

c2.changeSyncMode(d2, Realtime)

d2.updateAsync { root, _ ->
root.rootTree().edit(2, 2, text { "b" })
}.await()

withTimeout(GENERAL_TIMEOUT) {
while (d1Events.size < 3) {
delay(50)
}
}
assertIs<RemoteChange>(d1Events.last())
assertTreesXmlEquals("<doc><p>1ba2</p><p>34</p></doc>", d1)

collectJobs.forEach(Job::cancel)
}
}
}
4 changes: 3 additions & 1 deletion yorkie/src/main/kotlin/dev/yorkie/core/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ public class Client @VisibleForTesting internal constructor(
val responsePack = response.changePack.toChangePack()
// NOTE(7hong13, chacha912, hackerwins): If syncLoop already executed with
// PushPull, ignore the response when the syncMode is PushOnly.
val currentSyncMode = attachments.value[document.key]?.syncMode
if (responsePack.hasChanges &&
attachments.value[document.key]?.syncMode == SyncMode.RealtimePushOnly
currentSyncMode == SyncMode.RealtimePushOnly ||
currentSyncMode == SyncMode.RealtimeSyncOff
) {
return@runCatching
}
Expand Down
121 changes: 58 additions & 63 deletions yorkie/src/test/kotlin/dev/yorkie/document/crdt/CrdtTreeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Test

class CrdtTreeTest {
Expand Down Expand Up @@ -103,14 +104,8 @@ class CrdtTreeTest {
// 02. delete b from first paragraph
// 0 1 2 3 4 5 6 7
// <root> <p> a </p> <p> c d </p> </root>
target.edit(2 to 6, null)
// TODO(7hong13): should be resolved after the JS SDK implementation
// assertEquals("<root><p>ad</p></root>", target.toXml())

// 03. insert a new text node at the start of the first paragraph.
target.edit(1 to 1, CrdtTreeText(issuePos(), "@").toList())
// TODO(7hong13): should be resolved after the JS SDK implementation
// assertEquals("root><p>@ad</p></root>", target.toXml())
target.edit(2 to 3, null)
assertEquals("<root><p>a</p><p>cd</p></root>", target.toXml())
}

@Test
Expand Down Expand Up @@ -191,62 +186,62 @@ class CrdtTreeTest {
}

@Test
@Ignore("should be resolved after the JS SDK implementation")
fun `should merge and edit different levels with edit`() {
// TODO(7hong13): should be resolved after the JS SDK implementation
// fun initializeTree() {
// setUp()
// target.edit(0 to 0, CrdtTreeElement(issuePos(), "p").toList(), issueTime())
// target.edit(1 to 1, CrdtTreeElement(issuePos(), "b").toList(), issueTime())
// target.edit(2 to 2, CrdtTreeElement(issuePos(), "i").toList(), issueTime())
// target.edit(3 to 3, CrdtTreeText(issuePos(), "ab").toList(), issueTime())
// assertEquals("<root><p><b><i>ab</i></b></p></root>", target.toXml())
// }
//
// // 01. edit between two element nodes in the same hierarchy.
// // 0 1 2 3 4 5 6 7 8
// // <root> <p> <b> <i> a b </i> </b> </p> </root>
// initializeTree()
// target.edit(5 to 6, null, issueTime())
// assertEquals("<root><p><b>ab</b></p></root>", target.toXml())
//
// // 02. edit between two element nodes in same hierarchy.
// initializeTree()
// target.edit(6 to 7, null, issueTime())
// assertEquals("<root><p><i>ab</i></p></root>", target.toXml())
//
// // 03. edit between text and element node in different hierarchy.
// initializeTree()
// target.edit(4 to 6, null, issueTime())
// assertEquals("<root><p><b>a</b></p></root>", target.toXml())
//
// // 04. edit between text and element node in different hierarchy.
// initializeTree()
// target.edit(5 to 7, null, issueTime())
// assertEquals("<root><p>ab</p></root>", target.toXml())
//
// // 05. edit between text and element node in different hierarchy.
// initializeTree()
// target.edit(4 to 7, null, issueTime())
// assertEquals("<root><p>a</p></root>", target.toXml())
//
// // 06. edit between text and element node in different hierarchy.
// initializeTree()
// target.edit(3 to 7, null, issueTime())
// assertEquals("<root><p></p></root>", target.toXml())
//
// // 07. edit between text and element node in same hierarchy.
// setUp()
// target.edit(0 to 0, CrdtTreeElement(issuePos(), "p").toList(), issueTime())
// target.edit(1 to 1, CrdtTreeText(issuePos(), "ab").toList(), issueTime())
// target.edit(4 to 4, CrdtTreeElement(issuePos(), "p").toList(), issueTime())
// target.edit(5 to 5, CrdtTreeElement(issuePos(), "b").toList(), issueTime())
// target.edit(6 to 6, CrdtTreeText(issuePos(), "cd").toList(), issueTime())
// target.edit(10 to 10, CrdtTreeElement(issuePos(), "p").toList(), issueTime())
// target.edit(11 to 11, CrdtTreeText(issuePos(), "ef").toList(), issueTime())
// assertEquals("<root><p>ab</p><p><b>cd</b></p><p>ef</p></root>", target.toXml())
//
// target.edit(9 to 10, null, issueTime())
// assertEquals("<root><p>ab</p><b>cd</b><p>ef</p></root>", target.toXml())
fun initializeTree() {
setUp()
target.edit(0 to 0, CrdtTreeElement(issuePos(), "p").toList())
target.edit(1 to 1, CrdtTreeElement(issuePos(), "b").toList())
target.edit(2 to 2, CrdtTreeElement(issuePos(), "i").toList())
target.edit(3 to 3, CrdtTreeText(issuePos(), "ab").toList())
assertEquals("<root><p><b><i>ab</i></b></p></root>", target.toXml())
}

// 01. edit between two element nodes in the same hierarchy.
// 0 1 2 3 4 5 6 7 8
// <root> <p> <b> <i> a b </i> </b> </p> </root>
initializeTree()
target.edit(5 to 6, null)
assertEquals("<root><p><b>ab</b></p></root>", target.toXml())

// 02. edit between two element nodes in same hierarchy.
initializeTree()
target.edit(6 to 7, null)
assertEquals("<root><p><i>ab</i></p></root>", target.toXml())

// 03. edit between text and element node in different hierarchy.
initializeTree()
target.edit(4 to 6, null)
assertEquals("<root><p><b>a</b></p></root>", target.toXml())

// 04. edit between text and element node in different hierarchy.
initializeTree()
target.edit(5 to 7, null)
assertEquals("<root><p>ab</p></root>", target.toXml())

// 05. edit between text and element node in different hierarchy.
initializeTree()
target.edit(4 to 7, null)
assertEquals("<root><p>a</p></root>", target.toXml())

// 06. edit between text and element node in different hierarchy.
initializeTree()
target.edit(3 to 7, null)
assertEquals("<root><p></p></root>", target.toXml())

// 07. edit between text and element node in same hierarchy.
setUp()
target.edit(0 to 0, CrdtTreeElement(issuePos(), "p").toList())
target.edit(1 to 1, CrdtTreeText(issuePos(), "ab").toList())
target.edit(4 to 4, CrdtTreeElement(issuePos(), "p").toList())
target.edit(5 to 5, CrdtTreeElement(issuePos(), "b").toList())
target.edit(6 to 6, CrdtTreeText(issuePos(), "cd").toList())
target.edit(10 to 10, CrdtTreeElement(issuePos(), "p").toList())
target.edit(11 to 11, CrdtTreeText(issuePos(), "ef").toList())
assertEquals("<root><p>ab</p><p><b>cd</b></p><p>ef</p></root>", target.toXml())

target.edit(9 to 10, null)
assertEquals("<root><p>ab</p><b>cd</b><p>ef</p></root>", target.toXml())
}

private fun issuePos(offset: Int = 0) = CrdtTreeNodeID(issueTime(), offset)
Expand Down
Loading