-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add an `isEmpty` to the retry store interface and `hasNext` to the retry flow to allow skipping lots of calls to the store in the event the retry store is empty * Fix the last retry strategy from querying overflow value as `last`
- Loading branch information
Showing
9 changed files
with
102 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
ft-coroutines-retry/src/test/kotlin/tech/figure/coroutines/retry/flow/TestRetryFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package tech.figure.coroutines.retry.flow | ||
|
||
import io.kotest.core.spec.style.AnnotationSpec | ||
import java.time.OffsetDateTime | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.jupiter.api.fail | ||
import tech.figure.coroutines.retry.store.RetryRecord | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class TestRetryFlow : AnnotationSpec() { | ||
suspend fun <E> List<E>.asChannel(): Channel<E> { | ||
val ch = Channel<E>(capacity = size) | ||
forEach { | ||
ch.send(it) | ||
} | ||
return ch | ||
} | ||
|
||
suspend fun <T> testFlowRetry( | ||
list: List<RetryRecord<T>>, | ||
onSuccess: (item: T) -> Unit, | ||
onFailure: (item: T) -> Unit, | ||
block: (item: T, attempt: Int) -> Unit, | ||
): SimpleChannelFlowRetry<T> { | ||
return SimpleChannelFlowRetry( | ||
queue = list.asChannel(), | ||
onSuccess = onSuccess, | ||
onFailure = onFailure, | ||
block = block | ||
) | ||
} | ||
|
||
@Test | ||
fun testRetryFlowEmpty() = runTest { | ||
val flow = testFlowRetry( | ||
emptyList<RetryRecord<Int>>(), | ||
{ fail("should not be called") }, | ||
{ fail("should not be called") }, | ||
{ _, _ -> fail("should not be called") }, | ||
) | ||
|
||
assert(!flow.hasNext()) { "hasNext should be false" } | ||
assert(flow.produceNext(0..Int.MAX_VALUE, OffsetDateTime.MIN).toList().isEmpty()) { "should produce nothing" } | ||
} | ||
|
||
@Test | ||
fun testRetryFlowNotEmpty() = runTest { | ||
val flow = testFlowRetry( | ||
listOf(RetryRecord(1), RetryRecord(2), RetryRecord(3)), | ||
{ }, | ||
{ fail("should not be called") }, | ||
{ _, _ -> } | ||
) | ||
|
||
assert(flow.drain().size == 3) { "not all elements produced" } | ||
} | ||
} | ||
|
||
private suspend fun <T> FlowRetry<T>.drain(): List<RetryRecord<T>> { | ||
val list = mutableListOf<RetryRecord<T>>() | ||
while (hasNext()) { | ||
list.addAll(produceNext(0..Int.MAX_VALUE, OffsetDateTime.MIN, Int.MAX_VALUE).toList()) | ||
} | ||
return list | ||
} |