https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

kev1n11

05/03/2020, 9:22 PM
Hi all, Looking for some testing suggestion with coroutines, in particular
CoroutineScope
. We are POCing the following into our Spring Boot server • as the server starts up, use
SqsAsyncClient
to read in messages, • these are sent into a buffered
Channel
, • also start a pool of worker coroutines to process these messages, the processing functions are all
suspend fun
The above are encapsulated in a
SQSConsumer
which implements
CoroutineScope
, and we cant seem to figure out a clean way to test that. The code looks something like
Copy code
class SQSConsumer(
    val sqs: SqsAsyncClient,
    val props: SQSProperties
) : CoroutineScope {
    private val supervisorJob = SupervisorJob()
    override val coroutineContext: CoroutineContext
        get() = <http://Dispatchers.IO|Dispatchers.IO> + supervisorJob

    // called from another class using @PostConstruct
    fun start() = launch {
        val launchQueueReceiveChannel = launchReceiver(
            props.queueUrl,
            props.waitTimeSeconds,
            props.maxNumberOfMessages
        )
        repeat(props.workers) {
            launchWorker(launchQueueReceiveChannel, props.queueUrl, props.timeout)
        }
    }
}
What are the common patterns for overriding
Dispatcher
in tests? If you have a better suggest to encapsulate the above (Spring Boot or not), we’d love to hear too.
d

Dominaezzz

05/03/2020, 9:33 PM
override the dispatcher via DI.
👍 2
5 Views