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

Save unconsumed state in DefaultStateKeeperDispatcher #179

Merged
merged 1 commit into from
Oct 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal class DefaultStateKeeperDispatcher(
private val suppliers = HashMap<String, Supplier<*>>()

override fun save(): SerializableContainer {
val map = HashMap<String, SerializableContainer>()
val map = savedState?.toMutableMap() ?: HashMap()

suppliers.forEach { (key, supplier) ->
supplier.toSerializableContainer()?.also { container ->
map[key] = container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class DefaultStateKeeperDispatcherTest {
dispatcher1.register(key = "key2", strategy = Data.serializer()) { data2 }
dispatcher1.register(key = "key3", strategy = Data.serializer()) { null }

val savedState = dispatcher1.save()
.serialize(strategy = SerializableContainer.serializer())
.deserialize(strategy = SerializableContainer.serializer())
val savedState = dispatcher1.save().serializeAndDeserialize()

val dispatcher2 = DefaultStateKeeperDispatcher(savedState = savedState)

Expand All @@ -36,6 +34,30 @@ class DefaultStateKeeperDispatcherTest {
assertNull(restoredData3)
}

@Test
fun WHEN_save_recreate_twice_consume_THEN_data_restored() {
val dispatcher1 = DefaultStateKeeperDispatcher(savedState = null)

val data1 = Data(value = "value1")
val data2 = Data(value = "value2")
val data3 = Data(value = "value3")

dispatcher1.register(key = "key1", strategy = Data.serializer()) { data1 }
dispatcher1.register(key = "key2", strategy = Data.serializer()) { data2 }

val savedState1 = dispatcher1.save().serializeAndDeserialize()
val dispatcher2 = DefaultStateKeeperDispatcher(savedState = savedState1)
dispatcher2.register(key = "key1", strategy = Data.serializer()) { data3 }
val savedState2 = dispatcher2.save().serializeAndDeserialize()
val dispatcher3 = DefaultStateKeeperDispatcher(savedState = savedState2)

val restoredData1 = dispatcher3.consume(key = "key1", strategy = Data.serializer())
val restoredData2 = dispatcher3.consume(key = "key2", strategy = Data.serializer())

assertEquals(data3, restoredData1)
assertEquals(data2, restoredData2)
}

@Test
fun WHEN_consume_second_time_THEN_returns_null() {
val dispatcher1 = DefaultStateKeeperDispatcher(savedState = null)
Expand Down
Loading