Skip to content

Instantly share code, notes, and snippets.

@y2k
Created March 21, 2019 17:28
Show Gist options
  • Save y2k/561cbe17c04e0da4f008fa56b977c64a to your computer and use it in GitHub Desktop.
Save y2k/561cbe17c04e0da4f008fa56b977c64a to your computer and use it in GitHub Desktop.
fun runRobolectric(action: suspend () -> Unit) {
val result = AtomicReference<Result<Unit>>()
@Suppress("EXPERIMENTAL_API_USAGE")
MainScope().launch {
try {
action()
} catch (e: Throwable) {
result.set(Result.failure(e))
} finally {
result.compareAndSet(null, Result.success(Unit))
}
}
while (result.get() == null) {
Thread.sleep(100)
ShadowLooper.runUiThreadTasks()
}
result.get().exceptionOrNull()?.let { throw it }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment