Compatibility with Arrow • opensavvy.prepared.compat.arrow.coroutines • asPrepared
asPrepared¶
fun <T> Resource<T>.asPrepared(): PreparedProvider<T>
Converts an Arrow Resource into a Prepared value.
Prepared's prepared values are similar in concept to resources:
-
They both are lazy (declaring them and executing them happens in two different steps)
-
They both contain cleanup logic
The resulting prepared value executes the clean-up logic at the end of the test.
Example¶
val userProcessor: Resource<UserProcessor> = resource({
UserProcessor().also { it.start() }
}) { p, _ -> p.shutdown() }
val dataSource: Resource<DataSource> = resource({
DataSource().also { it.connect() }
}) { ds, exitCase ->
println("Releasing $ds with exit: $exitCase")
withContext(Dispatchers.IO) { ds.close() }
}
val service: Resource<Service> = resource {
Service(dataSource.bind(), userProcessor.bind())
}
// Note the 'by'!
val prepareService by service.asPrepared()
test("Test the creation") {
check(prepareService().create("…") != null)
}
At the end of the test, service, userProcessor and dataSource are cleaned-up (in this order).
External resources¶
See also¶
-
TestResourceScopeOverview of Prepared and Arrow's compatibility. -
preparedResourceAccess aResourceScopeto declare multiple resources as a single Prepared value.