Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class KottageEventFlow internal constructor(
): Flow<KottageEvent> {
private var lastEventTime: Long? = initialTime
private val subscribing = Mutex()
private val initializing = Mutex(locked = true)

@OptIn(FlowPreview::class)
private val flow: Flow<KottageEvent> = flow {
Expand Down Expand Up @@ -116,6 +117,7 @@ class KottageEventFlow internal constructor(
bridgeFlow.emit(KottageEvent.from(it))
}
}
initializing.unlock()
}
})
}.flattenConcat()
Expand All @@ -127,6 +129,18 @@ class KottageEventFlow internal constructor(
flow.collect(collector)
}

/**
* ensure and wait until subscription starts.
*
* This method is mainly for debug purpose.
*/
suspend fun ensureSubscribed() {
if (initializing.isLocked) {
initializing.lock()
initializing.unlock()
}
}

internal sealed interface EventFlowType {
object All : EventFlowType
data class Item(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ internal class KottageDatabaseManager(
suspend fun onEventCreated() {
val operator = operator.await()
val limit = 100L
_eventFlow.updateWithLock { lastEvent, emit ->
var lastEventTime = lastEvent.time
_eventFlow.updateWithLock { latestEventTime, emit ->
var lastEventTime = latestEventTime
var remains = true
while (remains) {
val events = databaseConnection.transactionWithResult {
Expand All @@ -170,6 +170,7 @@ internal class KottageDatabaseManager(
emit(it)
}
}
lastEventTime
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package io.github.irgaly.kottage.internal.model

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

Expand All @@ -14,28 +16,21 @@ internal class ItemEventFlow(initialTime: Long, scope: CoroutineScope) {
// イベント送信漏れが発生しないように SUSPEND
onBufferOverflow = BufferOverflow.SUSPEND
)
private val lastEvent = source.map {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map { } は、Dispatcher の都合により遅延実行されることがあり、テストでイベントの発行が重複することがあった。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map を使わないようにして解決

Event(it.createdAt, it)
}.stateIn(scope, SharingStarted.Eagerly, Event(initialTime, null))
private var lastEventTime: Long = initialTime

init {
flow = source.asSharedFlow()
}

suspend fun updateWithLock(block: suspend (latestEvent: Event, emit: suspend (event: ItemEvent) -> Unit) -> Unit) {
suspend fun updateWithLock(block: suspend (latestEventTime: Long, emit: suspend (event: ItemEvent) -> Unit) -> Long) {
mutex.withLock {
block(lastEvent.value, source::emit)
lastEventTime = block(lastEventTime, source::emit)
}
}

suspend fun withLock(block: suspend (latestEvent: Event) -> Unit) {
suspend fun withLock(block: suspend (latestEventTime: Long) -> Unit) {
mutex.withLock {
block(lastEvent.value)
block(lastEventTime)
}
}

data class Event(
val time: Long,
val event: ItemEvent?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.days
import kotlin.time.ExperimentalTime

/**
* Event 関連のテスト
*/
@OptIn(ExperimentalTime::class)
class KottageEventTest : KottageSpec("kottage_event", body = {
describe("Kottage Event Test") {
context("Storage 操作") {
Expand Down Expand Up @@ -71,65 +73,71 @@ class KottageEventTest : KottageSpec("kottage_event", body = {
val (kottage, calendar) = kottage("event_list")
val cache = kottage.cache("event_list")
val list = cache.list("list_event_list")
cache.eventFlow().test {
val entry1 = list.add("key1", "value1")
awaitItem() should {
it.itemKey shouldBe entry1.itemKey
it.listType shouldBe null
it.eventType shouldBe KottageEventType.Create
}
cache.put("key2", "value2")
awaitItem() should {
it.itemKey shouldBe "key2"
it.listType shouldBe null
it.eventType shouldBe KottageEventType.Create
cache.eventFlow().let { flow ->
flow.test {
flow.ensureSubscribed()
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KottageEventFlow は subscribe 完了まで短い時間 suspend されることがある。
Turbine は subscribe での suspend を待たないため、ensureSubscribed() で待てるようにした。

val entry1 = list.add("key1", "value1")
awaitItem() should {
it.itemKey shouldBe entry1.itemKey
it.listType shouldBe null
it.eventType shouldBe KottageEventType.Create
}
cache.put("key2", "value2")
awaitItem() should {
it.itemKey shouldBe "key2"
it.listType shouldBe null
it.eventType shouldBe KottageEventType.Create
}
}
}
list.eventFlow().test {
val entry3 = list.add("key3", "value3")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.eventType shouldBe KottageEventType.Create
}
cache.put("key3", "value3-2")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.eventType shouldBe KottageEventType.Update
}
list.update(entry3.positionId, "key4", "value4")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key3"
it.eventType shouldBe KottageEventType.Delete
}
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key4"
it.eventType shouldBe KottageEventType.Create
}
cache.put("key5", "value5")
list.updateKey(entry3.positionId, "key5")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key4"
it.eventType shouldBe KottageEventType.Delete
}
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key5"
it.eventType shouldBe KottageEventType.Create
}
list.remove(entry3.positionId)
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.eventType shouldBe KottageEventType.Delete
list.eventFlow().let { flow ->
flow.test {
flow.ensureSubscribed()
val entry3 = list.add("key3", "value3")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.eventType shouldBe KottageEventType.Create
}
cache.put("key3", "value3-2")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.eventType shouldBe KottageEventType.Update
}
list.update(entry3.positionId, "key4", "value4")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key3"
it.eventType shouldBe KottageEventType.Delete
}
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key4"
it.eventType shouldBe KottageEventType.Create
}
cache.put("key5", "value5")
list.updateKey(entry3.positionId, "key5")
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key4"
it.eventType shouldBe KottageEventType.Delete
}
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.itemKey shouldBe "key5"
it.eventType shouldBe KottageEventType.Create
}
list.remove(entry3.positionId)
awaitItem().let {
it.listPositionId shouldNotBe null
it.listType shouldBe "list_event_list"
it.eventType shouldBe KottageEventType.Delete
}
}
}
}
Expand Down