Skip to content

Commit

Permalink
Add Presence for Client.Options (#108)
Browse files Browse the repository at this point in the history
* add Presence value class for Client.Options.presence

* setup JDK 17 for the deploy pages action
  • Loading branch information
7hong13 committed Jun 19, 2023
1 parent 707e3dc commit b5cf896
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: gradle
- run: ./gradlew dokkaHtml
- uses: JamesIves/github-pages-deploy-action@v4.4.1
with:
Expand Down
2 changes: 1 addition & 1 deletion yorkie/src/androidTest/kotlin/dev/yorkie/JsonTestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.google.gson.Gson
import com.google.gson.JsonObject
import org.junit.Assert.assertEquals

private val gson = Gson()
internal val gson = Gson()

fun assertJsonContentEquals(expected: String, actual: String) {
val expectedJson = gson.fromJson(expected, JsonObject::class.java)
Expand Down
55 changes: 55 additions & 0 deletions yorkie/src/androidTest/kotlin/dev/yorkie/core/ClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import dev.yorkie.document.change.CheckPoint
import dev.yorkie.document.json.JsonCounter
import dev.yorkie.document.json.JsonPrimitive
import dev.yorkie.document.operation.OperationInfo
import dev.yorkie.gson
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -482,6 +483,60 @@ class ClientTest {
}
}

@Test
fun test_access_peer_presence() {
runBlocking {
data class Cursor(val x: Int, val y: Int)

val serializedCursor = gson.toJson(Cursor(1, 1))

val client1 = createClient(presence = Presence(mapOf("name" to "a")))
val client2 = createClient(
presence = Presence(mapOf("name" to "b", "cursor" to serializedCursor)),
)
val client1Events = mutableListOf<Client.Event>()
val client1EventsJob = launch(start = CoroutineStart.UNDISPATCHED) {
client1.events.collect(client1Events::add)
}

client1.activateAsync().await()
client2.activateAsync().await()

val documentKey = UUID.randomUUID().toString().toDocKey()
val document1 = Document(documentKey)
val document2 = Document(documentKey)

client1.attachAsync(document1).await()
withTimeout(2_000) {
// initialized
while (client1Events.isEmpty()) {
delay(50)
}
}
assertEquals(
null,
client1.peerStatusByDoc(documentKey).first()[client2.requireClientId()]?.data,
)

client2.attachAsync(document2).await()
withTimeout(2_000) {
client1.peerStatusByDoc(documentKey).first {
it[client2.requireClientId()]?.data != null
}
}
assertEquals(
mapOf("name" to "b", "cursor" to serializedCursor),
client1.peerStatusByDoc(documentKey).first()[client2.requireClientId()]?.data,
)

client1EventsJob.cancel()
client1.detachAsync(document1).await()
client2.detachAsync(document2).await()
client1.deactivateAsync().await()
client2.deactivateAsync().await()
}
}

private fun Client.peerStatusByDoc(key: Document.Key) = peerStatus.mapNotNull {
it[key]
}
Expand Down
3 changes: 2 additions & 1 deletion yorkie/src/androidTest/kotlin/dev/yorkie/core/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import java.util.UUID

fun createClient() = Client(
fun createClient(presence: Presence? = null) = Client(
InstrumentationRegistry.getInstrumentation().targetContext,
"10.0.2.2",
8080,
usePlainText = true,
options = Client.Options(presence = presence),
)

fun String.toDocKey(): Document.Key {
Expand Down
4 changes: 2 additions & 2 deletions yorkie/src/main/kotlin/dev/yorkie/core/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class Client @VisibleForTesting internal constructor(
private val _peerStatus = MutableStateFlow(emptyMap<Document.Key, Peers>())
public val peerStatus = _peerStatus.asStateFlow()

public var presenceInfo = options.presence ?: PresenceInfo(0, emptyMap())
public var presenceInfo = PresenceInfo(0, options.presence?.value.orEmpty())
private set

private val service by lazy {
Expand Down Expand Up @@ -738,7 +738,7 @@ public class Client @VisibleForTesting internal constructor(
* If the [Client] attaches a [Document], the [PresenceInfo] is sent to the other peers
* attached to the [Document].
*/
public val presence: PresenceInfo? = null,
public val presence: Presence? = null,
/**
* API key of the project used to identify the project.
*/
Expand Down
5 changes: 4 additions & 1 deletion yorkie/src/main/kotlin/dev/yorkie/core/Peers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class Peers private constructor(
}

public data class PresenceInfo(
public val clock: Int,
internal val clock: Int,
public val data: Map<String, String>,
)

@JvmInline
public value class Presence(val value: Map<String, String>)

0 comments on commit b5cf896

Please sign in to comment.