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

Reset online clients when stream is disconnected #181

Merged
merged 8 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -33,7 +33,7 @@ class KanbanBoardViewModel(private val client: Client) : ViewModel() {

viewModelScope.launch {
document.events.collect {
if (it is Document.Event.SyncStatusChange.Synced) {
if (it is Document.Event.SyncStatusChanged.Synced) {
document.getRoot().getAsOrNull<JsonArray>(DOCUMENT_LIST_KEY)
?.let(::updateDocument)
?: run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dev.yorkie.core.Client.SyncMode.Realtime
import dev.yorkie.core.Client.SyncMode.RealtimePushOnly
import dev.yorkie.core.PresenceInfo
import dev.yorkie.document.Document
import dev.yorkie.document.Document.Event.PresenceChange
import dev.yorkie.document.Document.Event.PresenceChanged
import dev.yorkie.document.json.JsonText
import dev.yorkie.document.operation.OperationInfo
import dev.yorkie.document.time.ActorID
Expand Down Expand Up @@ -39,7 +39,7 @@ class EditorViewModel(private val client: Client) : ViewModel(), YorkieEditText.
private val _selections = MutableSharedFlow<Selection>()
val selections = _selections.asSharedFlow()

val removedPeers = document.events.filterIsInstance<PresenceChange.Others.Unwatched>()
val removedPeers = document.events.filterIsInstance<PresenceChanged.Others.Unwatched>()
.map { it.changed.actorID }

private val _selectionColors = mutableMapOf<ActorID, Int>()
Expand Down Expand Up @@ -70,7 +70,7 @@ class EditorViewModel(private val client: Client) : ViewModel(), YorkieEditText.

is Document.Event.RemoteChange -> emitEditOpInfos(event.changeInfo)

is PresenceChange.Others.PresenceChanged -> event.changed.emitSelection()
is PresenceChanged.Others.PresenceChanged -> event.changed.emitSelection()

else -> {}
}
Expand Down
30 changes: 15 additions & 15 deletions yorkie/src/androidTest/kotlin/dev/yorkie/core/ClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import dev.yorkie.core.Client.SyncMode.RealtimeSyncOff
import dev.yorkie.document.Document
import dev.yorkie.document.Document.Event.LocalChange
import dev.yorkie.document.Document.Event.RemoteChange
import dev.yorkie.document.Document.Event.SyncStatusChange
import dev.yorkie.document.Document.Event.SyncStatusChanged
import dev.yorkie.document.json.JsonCounter
import dev.yorkie.document.json.JsonPrimitive
import dev.yorkie.document.json.JsonTree
Expand Down Expand Up @@ -49,16 +49,16 @@ class ClientTest {
val d1 = Document(key)
val d2 = Document(key)

val d1SyncEvents = mutableListOf<SyncStatusChange>()
val d2SyncEvents = mutableListOf<SyncStatusChange>()
val d1SyncEvents = mutableListOf<SyncStatusChanged>()
val d2SyncEvents = mutableListOf<SyncStatusChanged>()
val d1ChangeEvents = mutableListOf<Document.Event>()
val d2ChangeEvents = mutableListOf<Document.Event>()
val collectJobs = listOf(
launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<SyncStatusChange>().collect(d1SyncEvents::add)
d1.events.filterIsInstance<SyncStatusChanged>().collect(d1SyncEvents::add)
},
launch(start = CoroutineStart.UNDISPATCHED) {
d2.events.filterIsInstance<SyncStatusChange>().collect(d2SyncEvents::add)
d2.events.filterIsInstance<SyncStatusChanged>().collect(d2SyncEvents::add)
},
launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filter { it is RemoteChange || it is LocalChange }
Expand All @@ -84,7 +84,7 @@ class ClientTest {
}.await()

withTimeout(GENERAL_TIMEOUT) {
while (d2SyncEvents.none { it is SyncStatusChange.Synced }) {
while (d2SyncEvents.none { it is SyncStatusChanged.Synced }) {
delay(50)
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ class ClientTest {
}.await()

withTimeout(GENERAL_TIMEOUT) {
while (d1SyncEvents.none { it is SyncStatusChange.Synced }) {
while (d1SyncEvents.none { it is SyncStatusChanged.Synced }) {
delay(50)
}
}
Expand Down Expand Up @@ -183,9 +183,9 @@ class ClientTest {
assertEquals(d1.toJson(), d2.toJson())

// 02. c2 changes the sync mode to realtime sync mode.
val d2SyncEvents = mutableListOf<SyncStatusChange>()
val d2SyncEvents = mutableListOf<SyncStatusChanged>()
val collectJob = launch(start = CoroutineStart.UNDISPATCHED) {
d2.events.filterIsInstance<SyncStatusChange>().collect(d2SyncEvents::add)
d2.events.filterIsInstance<SyncStatusChanged>().collect(d2SyncEvents::add)
}

c2.changeSyncMode(d2, Realtime)
Expand All @@ -194,7 +194,7 @@ class ClientTest {
delay(50)
}
}
assertIs<SyncStatusChange.Synced>(d2SyncEvents.first())
assertIs<SyncStatusChanged.Synced>(d2SyncEvents.first())
d2SyncEvents.clear()

d1.updateAsync { root, _ ->
Expand All @@ -207,7 +207,7 @@ class ClientTest {
delay(50)
}
}
assertIs<SyncStatusChange.Synced>(d2SyncEvents.last())
assertIs<SyncStatusChanged.Synced>(d2SyncEvents.last())
assertEquals(d1.toJson(), d2.toJson())
collectJob.cancel()

Expand Down Expand Up @@ -246,7 +246,7 @@ class ClientTest {

val document2Events = mutableListOf<Document.Event>()
val collectJob = launch(start = CoroutineStart.UNDISPATCHED) {
document2.events.filterIsInstance<SyncStatusChange>().collect(document2Events::add)
document2.events.filterIsInstance<SyncStatusChanged>().collect(document2Events::add)
}

suspend fun assertDocument2IsSynced(expected: String) {
Expand All @@ -255,7 +255,7 @@ class ClientTest {
delay(50)
}
}
assertIs<SyncStatusChange.Synced>(document2Events.first())
assertIs<SyncStatusChanged.Synced>(document2Events.first())
assertJsonContentEquals(expected, document2.toJson())
}

Expand Down Expand Up @@ -655,7 +655,7 @@ class ClientTest {
// and sync with push-only mode: CP(2, 2) -> CP(3, 2)
val d1Events = mutableListOf<Document.Event>()
val collectJob = launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<SyncStatusChange>().collect(d1Events::add)
d1.events.filterIsInstance<SyncStatusChanged>().collect(d1Events::add)
}
d1.updateAsync { root, _ ->
root.getAs<JsonCounter>("counter").increase(1)
Expand All @@ -664,7 +664,7 @@ class ClientTest {
assertEquals(1, changePack.changes.size)
c1.changeSyncMode(d1, RealtimePushOnly)
withTimeout(GENERAL_TIMEOUT) {
while (d1Events.firstOrNull() !is SyncStatusChange.Synced) {
while (d1Events.firstOrNull() !is SyncStatusChanged.Synced) {
delay(50)
}
}
Expand Down
14 changes: 7 additions & 7 deletions yorkie/src/androidTest/kotlin/dev/yorkie/core/DocumentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ class DocumentTest {
}
}.await()

withTimeout(2_000) {
withTimeout(GENERAL_TIMEOUT) {
while (document2Ops.size < 4) {
delay(50)
}
Expand All @@ -300,7 +300,7 @@ class DocumentTest {
root.getAs<JsonText>("content").style(0, 5, mapOf("bold" to "true"))
}.await()

withTimeout(2_000) {
withTimeout(GENERAL_TIMEOUT) {
while (document1Ops.size < 3) {
delay(50)
}
Expand Down Expand Up @@ -386,7 +386,7 @@ class DocumentTest {
}
}.await()

withTimeout(2_000) {
withTimeout(GENERAL_TIMEOUT) {
// ops: counter, todos, todoOps: todos
while (document1Ops.size < 2 || document1TodosOps.isEmpty()) {
delay(50)
Expand Down Expand Up @@ -416,7 +416,7 @@ class DocumentTest {
root.getAs<JsonCounter>("counter").increase(10)
}.await()

withTimeout(2_000) {
withTimeout(GENERAL_TIMEOUT) {
while (document1Ops.isEmpty() || document1CounterOps.isEmpty()) {
delay(50)
}
Expand All @@ -434,7 +434,7 @@ class DocumentTest {
root.getAs<JsonArray>("todos").put("todo3")
}.await()

withTimeout(2_000) {
withTimeout(GENERAL_TIMEOUT) {
while (document1Ops.isEmpty() || document1TodosOps.isEmpty()) {
delay(50)
}
Expand All @@ -450,7 +450,7 @@ class DocumentTest {
root.getAs<JsonArray>("todos").put("todo4")
}.await()

withTimeout(2_000) {
withTimeout(GENERAL_TIMEOUT) {
while (document1Ops.isEmpty()) {
delay(50)
}
Expand All @@ -465,7 +465,7 @@ class DocumentTest {
root.getAs<JsonCounter>("counter").increase(10)
}.await()

withTimeout(2_000) {
withTimeout(GENERAL_TIMEOUT) {
while (document1Ops.isEmpty()) {
delay(50)
}
Expand Down
78 changes: 69 additions & 9 deletions yorkie/src/androidTest/kotlin/dev/yorkie/core/PresenceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import dev.yorkie.core.Client.SyncMode.Manual
import dev.yorkie.core.Client.SyncMode.Realtime
import dev.yorkie.document.Document
import dev.yorkie.document.Document.Event.PresenceChange
import dev.yorkie.document.Document.Event.PresenceChange.MyPresence
import dev.yorkie.document.Document.Event.PresenceChange.Others
import dev.yorkie.document.Document.Event.PresenceChanged
import dev.yorkie.document.Document.Event.PresenceChanged.MyPresence
import dev.yorkie.document.Document.Event.PresenceChanged.Others
import dev.yorkie.document.Document.Event.StreamConnectionChanged
import dev.yorkie.gson
import java.util.UUID
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNull
import kotlin.test.assertIs
import kotlin.test.assertTrue
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -100,7 +102,7 @@ class PresenceTest {
c2.activateAsync().await()

val d1Job = launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<PresenceChange>()
d1.events.filterIsInstance<PresenceChanged>()
.collect(d1Events::add)
}
c1.attachAsync(d1, initialPresence = mapOf("name" to "a")).await()
Expand All @@ -114,7 +116,7 @@ class PresenceTest {
d1Events.clear()

val d2Job = launch(start = CoroutineStart.UNDISPATCHED) {
d2.events.filterIsInstance<PresenceChange>()
d2.events.filterIsInstance<PresenceChanged>()
.collect(d2Events::add)
}
c2.attachAsync(d2, initialPresence = mapOf("name" to "b")).await()
Expand Down Expand Up @@ -251,7 +253,7 @@ class PresenceTest {
val c2ID = c2.requireClientId()

val d1Job = launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<PresenceChange>().collect(d1Events::add)
d1.events.filterIsInstance<PresenceChanged>().collect(d1Events::add)
}
c1.attachAsync(d1, mapOf("name" to "a", "cursor" to previousCursor)).await()

Expand Down Expand Up @@ -364,7 +366,7 @@ class PresenceTest {
)

assertEquals(mapOf(c1ID to mapOf("name" to "a")), d1.presences.value.toMap())
assertEquals(d1.presences.value.entries, d2.presences.value.entries)
assertTrue(d2.presences.value.isEmpty())

d1CollectJob.cancel()
c1.detachAsync(d1).await()
Expand Down Expand Up @@ -666,7 +668,7 @@ class PresenceTest {
syncMode = Manual,
detachDocuments = false,
) { c1, c2, d1, d2, _ ->
val d1Events = mutableListOf<PresenceChange>()
val d1Events = mutableListOf<PresenceChanged>()
val collectJob = launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<Others>()
.collect { event ->
Expand Down Expand Up @@ -729,7 +731,7 @@ class PresenceTest {
@Test
fun test_emit_the_same_presence_multiple_times() {
withTwoClientsAndDocuments(syncMode = Manual) { c1, c2, d1, d2, _ ->
val d1Events = mutableListOf<PresenceChange>()
val d1Events = mutableListOf<PresenceChanged>()
val collectJob = launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<Others>().collect(d1Events::add)
}
Expand Down Expand Up @@ -768,5 +770,63 @@ class PresenceTest {
}
}

@Test
fun test_presence_access_after_stream_disconnection() {
runBlocking {
val c1 = createClient()
val c2 = createClient()
val key = UUID.randomUUID().toString().toDocKey()
val d1 = Document(key)
val d2 = Document(key)

c1.activateAsync().await()
c2.activateAsync().await()

c1.attachAsync(d1, initialPresence = mapOf("name" to "a")).await()
val d1PresenceEvents = mutableListOf<Others>()
val d1ConnectionEvents = mutableListOf<StreamConnectionChanged>()
val jobs = listOf(
launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<Others>().collect(d1PresenceEvents::add)
},
launch(start = CoroutineStart.UNDISPATCHED) {
d1.events.filterIsInstance<StreamConnectionChanged>()
.collect(d1ConnectionEvents::add)
},
)

c2.attachAsync(d2, initialPresence = mapOf("name" to "b")).await()
withTimeout(GENERAL_TIMEOUT) {
while (d1PresenceEvents.isEmpty()) {
delay(50)
}
}

assertEquals(
Others.Watched(PresenceInfo(c2.requireClientId(), mapOf("name" to "b"))),
d1PresenceEvents.last(),
)

println(d1ConnectionEvents)
c1.changeSyncMode(d1, Manual)
withTimeout(GENERAL_TIMEOUT) {
while (d1ConnectionEvents.none { it is StreamConnectionChanged.Disconnected }) {
delay(50)
}
}
assertNull(d1.presences.value[c2.requireClientId()])

jobs.forEach(Job::cancel)
c1.detachAsync(d1).await()
c2.detachAsync(d2).await()
c1.deactivateAsync().await()
c2.deactivateAsync().await()
c1.close()
c2.close()
d1.close()
d2.close()
}
}

private data class Cursor(val x: Int, val y: Int)
}
Loading
Loading