Copy over upstream changes to WorkRequestHandlerTest (#16)

https://cs.opensource.google/bazel/bazel/+/825e0d3c6d148c13a8b0585fa0d8f760a0b2e4a5
https://cs.opensource.google/bazel/bazel/+/e53ed1df40b0233daf5be5b4f2f4a54391bbfb7c
diff --git a/java/src/test/java/com/google/devtools/build/lib/worker/WorkRequestHandlerTest.java b/java/src/test/java/com/google/devtools/build/lib/worker/WorkRequestHandlerTest.java
index 817ef75..cc6d187 100644
--- a/java/src/test/java/com/google/devtools/build/lib/worker/WorkRequestHandlerTest.java
+++ b/java/src/test/java/com/google/devtools/build/lib/worker/WorkRequestHandlerTest.java
@@ -120,14 +120,14 @@
         new WorkRequestHandler(
             (args, err) -> {
               // Each call to this runs in its own thread.
+              synchronized (workerThreads) {
+                workerThreads.add(Thread.currentThread());
+              }
+              started.release();
               try {
-                synchronized (workerThreads) {
-                  workerThreads.add(Thread.currentThread());
-                }
-                started.release();
-                eternity.acquire(); // This blocks forever.
+                eternity.acquire(); // This blocks until the thread is interrupted at shutdown.
               } catch (InterruptedException e) {
-                throw new AssertionError("Unhandled exception", e);
+                Thread.currentThread().interrupt();
               }
               return 0;
             },
@@ -193,10 +193,15 @@
                 if (workerThreads.size() < 2) {
                   eternity.acquire(); // This blocks forever.
                 } else {
-                  throw new Error("Intentional death!");
+                  // This is triggered by the second WorkRequest. This causes the PipedInputStream
+                  // under the hood to throw an InterruptedIOException. This process helps us
+                  // simulate the situation when the infinite loop in the WorkRequestHandler catches
+                  // an IOException while calling messageProcess.readWorkRequest(). This exception
+                  // will then trigger the path we're testing to stop the worker.
+                  messageProcessor.interruptReader();
                 }
               } catch (InterruptedException e) {
-                throw new AssertionError("Unhandled exception", e);
+                Thread.currentThread().interrupt();
               }
               return 0;
             },
@@ -671,5 +676,9 @@
         readerThread.interrupt();
       }
     }
+
+    public void interruptReader() {
+      readerThread.interrupt();
+    }
   }
 }