YarnUncaughtExceptionHandler implements UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { if(ShutdownHookManager.get().isShutdownInProgress()) { LOG.error("Thread " + t + " threw an Throwable, but we are shutting " + "down, so ignoring this", e); } else if(e instanceof Error) { try { LOG.error(FATAL, "Thread " + t + " threw an Error. Shutting down now...", e); } catch (Throwable err) { } if(e instanceof OutOfMemoryError) { try { System.err.println("Halting due to Out Of Memory Error..."); } catch (Throwable err) { } ExitUtil.halt(-1); } else { ExitUtil.terminate(-1); } } else { LOG.error("Thread " + t + " threw an Exception.", e); } }  @Override void uncaughtException(Thread t, Throwable e);  }
@Test public void testUncaughtExceptionHandlerWithRuntimeException() throws InterruptedException { final YarnUncaughtExceptionHandler spyYarnHandler = spy(exHandler); final YarnRuntimeException yarnException = new YarnRuntimeException( "test-yarn-runtime-exception"); final Thread yarnThread = new Thread(new Runnable() { @Override public void run() { throw yarnException; } }); yarnThread.setUncaughtExceptionHandler(spyYarnHandler); assertSame(spyYarnHandler, yarnThread.getUncaughtExceptionHandler()); yarnThread.start(); yarnThread.join(); verify(spyYarnHandler).uncaughtException(yarnThread, yarnException); }
@Test public void testUncaughtExceptionHandlerWithError() throws InterruptedException { ExitUtil.disableSystemExit(); final YarnUncaughtExceptionHandler spyErrorHandler = spy(exHandler); final java.lang.Error error = new java.lang.Error("test-error"); final Thread errorThread = new Thread(new Runnable() { @Override public void run() { throw error; } }); errorThread.setUncaughtExceptionHandler(spyErrorHandler); assertSame(spyErrorHandler, errorThread.getUncaughtExceptionHandler()); errorThread.start(); errorThread.join(); verify(spyErrorHandler).uncaughtException(errorThread, error); }
@Test public void testUncaughtExceptionHandlerWithOutOfMemoryError() throws InterruptedException { ExitUtil.disableSystemHalt(); final YarnUncaughtExceptionHandler spyOomHandler = spy(exHandler); final OutOfMemoryError oomError = new OutOfMemoryError("out-of-memory-error"); final Thread oomThread = new Thread(new Runnable() { @Override public void run() { throw oomError; } }); oomThread.setUncaughtExceptionHandler(spyOomHandler); assertSame(spyOomHandler, oomThread.getUncaughtExceptionHandler()); oomThread.start(); oomThread.join(); verify(spyOomHandler).uncaughtException(oomThread, oomError); }