Skip to content

Instantly share code, notes, and snippets.

@arekolek
Last active July 15, 2018 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arekolek/e9e0d050cdd6ed16cd7dd9183eee62c0 to your computer and use it in GitHub Desktop.
Save arekolek/e9e0d050cdd6ed16cd7dd9183eee62c0 to your computer and use it in GitHub Desktop.
LiveData extension property for use in unit tests
val <T> LiveData<T>.blockingValue: T?
get() {
var value: T? = null
val latch = CountDownLatch(1)
observeForever {
value = it
latch.countDown()
}
if (latch.await(2, TimeUnit.SECONDS)) return value
else throw Exception("LiveData value was not set within 2 seconds")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment