Search in sources :

Example 6 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class CacheLoadingTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    logHandler = new TestLogHandler();
    LocalCache.logger.addHandler(logHandler);
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler)

Example 7 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class LocalCacheTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    logHandler = new TestLogHandler();
    LocalCache.logger.addHandler(logHandler);
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler)

Example 8 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class ByteSourceTest method testCopyExceptions.

public void testCopyExceptions() {
    if (!Closer.SuppressingSuppressor.isAvailable()) {
        // test that exceptions are logged
        TestLogHandler logHandler = new TestLogHandler();
        Closeables.logger.addHandler(logHandler);
        try {
            for (ByteSource in : BROKEN_SOURCES) {
                runFailureTest(in, newNormalByteSink());
                assertTrue(logHandler.getStoredLogRecords().isEmpty());
                runFailureTest(in, BROKEN_CLOSE_SINK);
                assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, getAndResetRecords(logHandler));
            }
            for (ByteSink out : BROKEN_SINKS) {
                runFailureTest(newNormalByteSource(), out);
                assertTrue(logHandler.getStoredLogRecords().isEmpty());
                runFailureTest(BROKEN_CLOSE_SOURCE, out);
                assertEquals(1, getAndResetRecords(logHandler));
            }
            for (ByteSource in : BROKEN_SOURCES) {
                for (ByteSink out : BROKEN_SINKS) {
                    runFailureTest(in, out);
                    assertTrue(getAndResetRecords(logHandler) <= 1);
                }
            }
        } finally {
            Closeables.logger.removeHandler(logHandler);
        }
    } else {
        for (ByteSource in : BROKEN_SOURCES) {
            int suppressed = runSuppressionFailureTest(in, newNormalByteSink());
            assertEquals(0, suppressed);
            suppressed = runSuppressionFailureTest(in, BROKEN_CLOSE_SINK);
            assertEquals((in == BROKEN_OPEN_SOURCE) ? 0 : 1, suppressed);
        }
        for (ByteSink out : BROKEN_SINKS) {
            int suppressed = runSuppressionFailureTest(newNormalByteSource(), out);
            assertEquals(0, suppressed);
            suppressed = runSuppressionFailureTest(BROKEN_CLOSE_SOURCE, out);
            assertEquals(1, suppressed);
        }
        for (ByteSource in : BROKEN_SOURCES) {
            for (ByteSink out : BROKEN_SINKS) {
                int suppressed = runSuppressionFailureTest(in, out);
                assertTrue(suppressed <= 1);
            }
        }
    }
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler)

Example 9 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class ServiceManagerTest method testPartiallyConstructedManager.

/**
   * 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());
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler) Logger(java.util.logging.Logger)

Example 10 with TestLogHandler

use of com.google.common.testing.TestLogHandler in project guava by google.

the class ServiceManagerTest method testEmptyServiceManager.

/**
   * This is for covering a case where the ServiceManager would behave strangely if constructed
   * with no service under management.  Listeners would never fire because the ServiceManager was
   * healthy and stopped at the same time.  This test ensures that listeners fire and isHealthy
   * makes sense.
   */
public void testEmptyServiceManager() {
    Logger logger = Logger.getLogger(ServiceManager.class.getName());
    logger.setLevel(Level.FINEST);
    TestLogHandler logHandler = new TestLogHandler();
    logger.addHandler(logHandler);
    ServiceManager manager = new ServiceManager(Arrays.<Service>asList());
    RecordingListener listener = new RecordingListener();
    manager.addListener(listener);
    manager.startAsync().awaitHealthy();
    assertTrue(manager.isHealthy());
    assertTrue(listener.healthyCalled);
    assertFalse(listener.stoppedCalled);
    assertTrue(listener.failedServices.isEmpty());
    manager.stopAsync().awaitStopped();
    assertFalse(manager.isHealthy());
    assertTrue(listener.stoppedCalled);
    assertTrue(listener.failedServices.isEmpty());
    // check that our NoOpService is not directly observable via any of the inspection methods or
    // via logging.
    assertEquals("ServiceManager{services=[]}", manager.toString());
    assertTrue(manager.servicesByState().isEmpty());
    assertTrue(manager.startupTimes().isEmpty());
    Formatter logFormatter = new Formatter() {

        @Override
        public String format(LogRecord record) {
            return formatMessage(record);
        }
    };
    for (LogRecord record : logHandler.getStoredLogRecords()) {
        assertThat(logFormatter.format(record)).doesNotContain("NoOpService");
    }
}
Also used : TestLogHandler(com.google.common.testing.TestLogHandler) LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) Logger(java.util.logging.Logger)

Aggregations

TestLogHandler (com.google.common.testing.TestLogHandler)12 Logger (java.util.logging.Logger)4 LogRecord (java.util.logging.LogRecord)2 IOException (java.io.IOException)1 Formatter (java.util.logging.Formatter)1