Skip to content

Compatibility with Arrowopensavvy.prepared.compat.arrow.coroutinesasPrepared

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