Bubble up exceptions in visibleOnHandlerThread

Test: NetworkStaticLibTests
Fixes: 269390906
Change-Id: I3c2d330dd719869562f437c1995e85abd32dbd68
diff --git a/staticlibs/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt b/staticlibs/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt
index 46a3588..30e0daf 100644
--- a/staticlibs/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt
+++ b/staticlibs/tests/unit/src/com/android/testutils/HandlerUtilsTest.kt
@@ -19,6 +19,7 @@
 import android.os.Handler
 import android.os.HandlerThread
 import kotlin.test.assertEquals
+import kotlin.test.assertFailsWith
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.junit.runners.JUnit4
@@ -72,5 +73,9 @@
             assertEquals(attempt, x)
             handler.post { assertEquals(attempt, x) }
         }
+
+        assertFailsWith<IllegalArgumentException> {
+            visibleOnHandlerThread(handler) { throw IllegalArgumentException() }
+        }
     }
 }
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/HandlerUtils.kt b/staticlibs/testutils/devicetests/com/android/testutils/HandlerUtils.kt
index 6871349..aa252a5 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/HandlerUtils.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/HandlerUtils.kt
@@ -65,11 +65,13 @@
  */
 fun visibleOnHandlerThread(handler: Handler, r: ThrowingRunnable) {
     val cv = ConditionVariable()
+    var e: Exception? = null
     handler.post {
         try {
             r.run()
         } catch (exception: Exception) {
             Log.e(TAG, "visibleOnHandlerThread caught exception", exception)
+            e = exception
         }
         cv.open()
     }
@@ -77,4 +79,5 @@
     // and this thread also has seen the change (since cv.open() happens-before cv.block()
     // returns).
     cv.block()
+    e?.let { throw it }
 }