private static int getAndResetRecords(TestLogHandler logHandler) { int records = logHandler.getStoredLogRecords().size(); logHandler.clear(); return records; }
private static int getAndResetRecords(TestLogHandler logHandler) { int records = logHandler.getStoredLogRecords().size(); logHandler.clear(); return records; }
private void checkNothingLogged() { assertTrue(logHandler.getStoredLogRecords().isEmpty()); }
private void checkNothingLogged() { assertThat(logHandler.getStoredLogRecords()).isEmpty(); }
private Throwable popLoggedThrowable() { List<LogRecord> logRecords = logHandler.getStoredLogRecords(); assertSame(1, logRecords.size()); LogRecord logRecord = logRecords.get(0); logHandler.clear(); return logRecord.getThrown(); }
private Throwable popLoggedThrowable() { List<LogRecord> logRecords = logHandler.getStoredLogRecords(); assertEquals(1, logRecords.size()); LogRecord logRecord = logRecords.get(0); logHandler.clear(); return logRecord.getThrown(); }
public void test() throws Exception { assertTrue(handler.getStoredLogRecords().isEmpty()); ExampleClassUnderTest.foo(); LogRecord record = handler.getStoredLogRecords().get(0); assertEquals(Level.INFO, record.getLevel()); assertEquals("message", record.getMessage()); assertSame(EXCEPTION, record.getThrown()); }
public void testConcurrentModification() throws Exception { // Tests for the absence of a bug where logging while iterating over the // stored log records causes a ConcurrentModificationException assertTrue(handler.getStoredLogRecords().isEmpty()); ExampleClassUnderTest.foo(); ExampleClassUnderTest.foo(); for (LogRecord unused : handler.getStoredLogRecords()) { ExampleClassUnderTest.foo(); } }
public void testSuccessfulAsList_resultCancelledRacingInputDone() throws Exception { TestLogHandler listenerLoggerHandler = new TestLogHandler(); Logger exceptionLogger = Logger.getLogger(AbstractFuture.class.getName()); exceptionLogger.addHandler(listenerLoggerHandler); try { doTestSuccessfulAsList_resultCancelledRacingInputDone(); assertWithMessage("Nothing should be logged") .that(listenerLoggerHandler.getStoredLogRecords()) .isEmpty(); } finally { exceptionLogger.removeHandler(listenerLoggerHandler); } }
public static void testLoggingSuppressor() throws IOException { TestLogHandler logHandler = new TestLogHandler(); Closeables.logger.addHandler(logHandler); try { Closer closer = new Closer(new Closer.LoggingSuppressor()); TestCloseable c1 = closer.register(TestCloseable.throwsOnClose(new IOException())); TestCloseable c2 = closer.register(TestCloseable.throwsOnClose(new RuntimeException())); try { throw closer.rethrow(new IOException("thrown"), IOException.class); } catch (IOException expected) { } assertTrue(logHandler.getStoredLogRecords().isEmpty()); closer.close(); assertEquals(2, logHandler.getStoredLogRecords().size()); LogRecord record = logHandler.getStoredLogRecords().get(0); assertEquals("Suppressing exception thrown when closing " + c2, record.getMessage()); record = logHandler.getStoredLogRecords().get(1); assertEquals("Suppressing exception thrown when closing " + c1, record.getMessage()); } finally { Closeables.logger.removeHandler(logHandler); } }
/** Ensure that errors are always logged. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_error() throws Exception { try { getDone(allAsList(immediateFailedFuture(new MyError()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyError.class); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyError.class); } }
/** A single non-error failure is not logged because it is reported via the output future. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_exception() throws Exception { try { getDone(allAsList(immediateFailedFuture(new MyException()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } }
/** All as list will log extra exceptions that occur later. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_multipleExceptions_doneLater() throws Exception { SettableFuture<Object> future1 = SettableFuture.create(); SettableFuture<Object> future2 = SettableFuture.create(); SettableFuture<Object> future3 = SettableFuture.create(); ListenableFuture<List<Object>> all = allAsList(future1, future2, future3); future1.setException(new MyException()); future2.setException(new MyException()); future3.setException(new MyException()); try { getDone(all); fail(); } catch (ExecutionException expected) { List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(2); // failures after the first are logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyException.class); assertThat(logged.get(1).getThrown()).isInstanceOf(MyException.class); } }
public void testEnqueueAndDispatch_withLabeledExceptions() { Object listener = new MyListener(); ListenerCallQueue<Object> queue = new ListenerCallQueue<>(); queue.addListener(listener, directExecutor()); queue.enqueue(THROWING_EVENT, "custom-label"); Logger logger = Logger.getLogger(ListenerCallQueue.class.getName()); logger.setLevel(Level.SEVERE); TestLogHandler logHandler = new TestLogHandler(); logger.addHandler(logHandler); try { queue.dispatch(); } finally { logger.removeHandler(logHandler); } assertEquals(1, logHandler.getStoredLogRecords().size()); assertEquals( "Exception while executing callback: MyListener custom-label", logHandler.getStoredLogRecords().get(0).getMessage()); }
/** Ensure that errors are always logged. */ @SuppressWarnings("unchecked") public void testSuccessfulAsList_logging_error() throws Exception { assertEquals( newArrayList((Object) null), getDone(successfulAsList(immediateFailedFuture(new MyError())))); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // errors are always logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyError.class); }
/** The same exception happening on multiple futures should not be logged. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_same_exception() throws Exception { try { MyException sameInstance = new MyException(); getDone(allAsList(immediateFailedFuture(sameInstance), immediateFailedFuture(sameInstance))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } }
/** All as list will log extra exceptions that have already occurred. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_multipleExceptions_alreadyDone() throws Exception { try { getDone( allAsList( immediateFailedFuture(new MyException()), immediateFailedFuture(new MyException()))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); List<LogRecord> logged = aggregateFutureLogHandler.getStoredLogRecords(); assertThat(logged).hasSize(1); // the second failure is logged assertThat(logged.get(0).getThrown()).isInstanceOf(MyException.class); } }
/** * Catches a bug where when constructing a service manager failed, later interactions with the * service could cause IllegalStateExceptions inside the partially constructed ServiceManager. * This ISE wouldn't actually bubble up but would get logged by ExecutionQueue. This obfuscated * the original error (which was not constructing ServiceManager correctly). */ public void testPartiallyConstructedManager() { Logger logger = Logger.getLogger("global"); logger.setLevel(Level.FINEST); TestLogHandler logHandler = new TestLogHandler(); logger.addHandler(logHandler); NoOpService service = new NoOpService(); service.startAsync(); try { new ServiceManager(Arrays.asList(service)); fail(); } catch (IllegalArgumentException expected) { } service.stopAsync(); // Nothing was logged! assertEquals(0, logHandler.getStoredLogRecords().size()); }
/** * Different exceptions happening on multiple futures with the same cause should not be logged. */ @SuppressWarnings("unchecked") public void testAllAsList_logging_same_cause() throws Exception { try { MyException exception1 = new MyException(); MyException exception2 = new MyException(); MyException exception3 = new MyException(); MyException sameInstance = new MyException(); exception1.initCause(sameInstance); exception2.initCause(sameInstance); exception3.initCause(exception2); getDone(allAsList(immediateFailedFuture(exception1), immediateFailedFuture(exception3))); fail(); } catch (ExecutionException expected) { assertThat(expected.getCause()).isInstanceOf(MyException.class); assertEquals( "Nothing should be logged", 0, aggregateFutureLogHandler.getStoredLogRecords().size()); } }
/** Non-Error exceptions are never logged. */ @SuppressWarnings("unchecked") public void testSuccessfulAsList_logging_exception() throws Exception { assertEquals( newArrayList((Object) null), getDone(successfulAsList(immediateFailedFuture(new MyException())))); assertWithMessage("Nothing should be logged") .that(aggregateFutureLogHandler.getStoredLogRecords()) .isEmpty(); // Not even if there are a bunch of failures. assertEquals( newArrayList(null, null, null), getDone( successfulAsList( immediateFailedFuture(new MyException()), immediateFailedFuture(new MyException()), immediateFailedFuture(new MyException())))); assertWithMessage("Nothing should be logged") .that(aggregateFutureLogHandler.getStoredLogRecords()) .isEmpty(); }