Created
March 21, 2019 17:28
-
-
Save y2k/561cbe17c04e0da4f008fa56b977c64a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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