CLOVIS
02/12/2023, 2:51 PMCoroutineScope.async(CoroutineStart.LAZY) {}
, but without the requirement for the CoroutineScope (instead, the first user to await
uses their own CoroutineScope to compute the value, and then the same is returned for all other users).Sam
02/12/2023, 3:14 PMCLOVIS
02/12/2023, 3:14 PMfun <T: Any> lazySingleton(block: CoroutineScope.() -> T): CoroutineScope.() -> T {
val lock = Mutex()
var data: T? = null
return {
lock.withLock {
if (data == null) {
block().also { data = it }
} else {
data!!
}
}
}
}
CLOVIS
02/12/2023, 3:15 PM@BeforeTest
cannot suspend
.CLOVIS
02/12/2023, 3:16 PMephemient
02/12/2023, 3:47 PMprivate fun runTest(testBody: suspend TestScope.() -> Unit) = kotlinx.coroutines.test.runTest {
setUp()
try {
testBody()
} finally {
tearDown()
}
}
in the test classes that need a suspending setup/teardown