@Test public void testNonPublicInterfaceLogsWarning() throws Exception { PipelineOptionsFactory.as(NonPublicPipelineOptions.class); // Make sure we print the name of the class. expectedLogs.verifyWarn(NonPublicPipelineOptions.class.getName()); expectedLogs.verifyWarn("all non-public interfaces to be in the same package"); }
@Test(expected = AssertionError.class) public void testVerifyNotLoggedWhenMatchedWithException() throws Throwable { String expected = generateRandomString(); LOG.error(expected, new IOException("Fake Exception")); expectedLogs.verifyNotLogged(expected); }
@Override public void evaluate() throws Throwable { evaluateRan[0] = true; expectedLogs.verifyNotLogged(messageUnexpected); LOG.info(messageExpected); expectedLogs.verifyInfo(messageExpected); } },
public class FooTest { @Rule public final ExpectedLogs logs = new ExpectedLogs() {{ captureFor(Foo.class, LogLevel.WARN); }}; @Test public void barShouldLogWarning() { assertThat(logs.isEmpty(), is(true)); // Nothing captured yet. // Logic using the class you are capturing logs for: Foo foo = new Foo(); assertThat(foo.bar(), is(not(nullValue()))); // Assert content of the captured logs: assertThat(logs.isEmpty(), is(false)); assertThat(logs.contains("Your warning message here"), is(true)); } }
/** * Verify there are no logging events with messages containing the given substring. * * @param substring The message to match against. */ public void verifyNotLogged(String substring) { verifyNotLogged(matcher(substring)); }
unknownJobMessage)); expectedLogs.verifyError("ERRORERROR"); expectedLogs.verifyError(errorTime.toString()); expectedLogs.verifyWarn("WARNINGWARNING"); expectedLogs.verifyWarn(warningTime.toString()); expectedLogs.verifyInfo("BASICBASIC"); expectedLogs.verifyInfo(basicTime.toString()); expectedLogs.verifyInfo("DETAILEDDETAILED"); expectedLogs.verifyInfo(detailedTime.toString()); expectedLogs.verifyDebug("DEBUGDEBUG"); expectedLogs.verifyDebug(debugTime.toString()); expectedLogs.verifyTrace("UNKNOWN TIMESTAMP"); expectedLogs.verifyTrace("UNKNOWNUNKNOWN"); expectedLogs.verifyNotLogged(unknownTime.toString());
/** * Tests that the {@link DataflowPipelineJob} understands that the {@link State#DONE DONE} state * is terminal. */ @Test public void testWaitToFinishDone() throws Exception { assertEquals(State.DONE, mockWaitToFinishInState(State.DONE)); expectedLogs.verifyInfo(String.format("Job %s finished with status DONE.", JOB_ID)); }
@Test public void testLogsCleared() throws Throwable { final String messageUnexpected = "Message prior to ExpectedLogs."; final String messageExpected = "Message expected."; LOG.info(messageUnexpected); expectedLogs = ExpectedLogs.none(ExpectedLogsTest.class); final boolean[] evaluateRan = new boolean[1]; expectedLogs .apply( new Statement() { @Override public void evaluate() throws Throwable { evaluateRan[0] = true; expectedLogs.verifyNotLogged(messageUnexpected); LOG.info(messageExpected); expectedLogs.verifyInfo(messageExpected); } }, Description.EMPTY) .evaluate(); assertTrue(evaluateRan[0]); // Verify expectedLogs is cleared. expectedLogs.verifyNotLogged(messageExpected); expectedLogs.verifyNotLogged(messageUnexpected); }
/** Tests that an IOException is retried. */ @Test public void testThrowIOException() throws IOException { when(mockLowLevelRequest.execute()) .thenThrow(new IOException("Fake Error")) .thenReturn(mockLowLevelResponse); when(mockLowLevelResponse.getStatusCode()).thenReturn(200); Storage.Buckets.Get result = storage.buckets().get("test"); HttpResponse response = result.executeUnparsed(); assertNotNull(response); verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class)); verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString()); verify(mockLowLevelRequest, times(2)).setTimeout(anyInt(), anyInt()); verify(mockLowLevelRequest, times(2)).setWriteTimeout(anyInt()); verify(mockLowLevelRequest, times(2)).execute(); verify(mockLowLevelResponse).getStatusCode(); expectedLogs.verifyDebug("Request failed with IOException"); }
@Test public void testVerifyWhenMatchedWithExceptionBeingLogged() throws Throwable { String expected = generateRandomString(); LOG.error(expected, new IOException("Fake Exception")); expectedLogs.verifyError(expected); }
@Test public void testLogCaptureOccursAtLowestLogLevel() throws Throwable { String expected = generateRandomString(); LOG.trace(expected); expectedLogs.verifyTrace(expected); }
/** * Returns a {@link TestRule} that captures logs for the given logger name. * * @param name The logger name to capture logs for. * @return A {@link ExpectedLogs} test rule. */ public static ExpectedLogs none(String name) { return new ExpectedLogs(name); }
/** * Tests that the {@link DataflowPipelineJob} understands that the {@link State#FAILED FAILED} * state is terminal. */ @Test public void testWaitToFinishFailed() throws Exception { assertEquals(State.FAILED, mockWaitToFinishInState(State.FAILED)); expectedLogs.verifyInfo(String.format("Job %s failed with status FAILED.", JOB_ID)); }
private void verifyNo(final Level level, final String substring, final Throwable throwable) { verifyNotLogged(matcher(level, substring, throwable)); }
/** Tests that a retriable error is retried. */ @Test public void testRetryableError() throws IOException { when(mockLowLevelRequest.execute()) .thenReturn(mockLowLevelResponse) .thenReturn(mockLowLevelResponse) .thenReturn(mockLowLevelResponse); when(mockLowLevelResponse.getStatusCode()) .thenReturn(503) // Retryable .thenReturn(429) // We also retry on 429 Too Many Requests. .thenReturn(200); Storage.Buckets.Get result = storage.buckets().get("test"); HttpResponse response = result.executeUnparsed(); assertNotNull(response); verify(mockHttpResponseInterceptor).interceptResponse(any(HttpResponse.class)); verify(mockLowLevelRequest, atLeastOnce()).addHeader(anyString(), anyString()); verify(mockLowLevelRequest, times(3)).setTimeout(anyInt(), anyInt()); verify(mockLowLevelRequest, times(3)).setWriteTimeout(anyInt()); verify(mockLowLevelRequest, times(3)).execute(); verify(mockLowLevelResponse, times(3)).getStatusCode(); expectedLogs.verifyDebug("Request failed with code 503"); }
public class FooTest { @Rule public final ExpectedLogs logs = new ExpectedLogs() {{ captureFor(Foo.class, LogLevel.WARN); }}; @Test public void barShouldLogWarning() { assertThat(logs.isEmpty(), is(true)); // Nothing captured yet. // Logic using the class you are capturing logs for: Foo foo = new Foo(); assertThat(foo.bar(), is(not(nullValue()))); // Assert content of the captured logs: assertThat(logs.isEmpty(), is(false)); assertThat(logs.contains("Your warning message here"), is(true)); } }
@Test public void testVerifyWhenMatchedFully() throws Throwable { String expected = generateRandomString(); LOG.error(expected); expectedLogs.verifyError(expected); }
@Test public void testThreadSafetyOfLogSaver() throws Throwable { CompletionService<Void> completionService = new ExecutorCompletionService<>(Executors.newCachedThreadPool()); final long scheduledLogTime = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS) + 500L; List<String> expectedStrings = new ArrayList<>(); for (int i = 0; i < 100; i++) { final String expected = generateRandomString(); expectedStrings.add(expected); completionService.submit( () -> { // Have all threads started and waiting to log at about the same moment. sleepMillis( Math.max( 1, scheduledLogTime - TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS))); LOG.trace(expected); return null; }); } // Wait for all the threads to complete. for (int i = 0; i < 100; i++) { completionService.take().get(); } for (String expected : expectedStrings) { expectedLogs.verifyTrace(expected); } }
/** * Tests that the {@link DataflowPipelineJob} understands that the {@link State#UNKNOWN UNKNOWN} * state is terminal. */ @Test public void testWaitToFinishUnknown() throws Exception { assertEquals(null, mockWaitToFinishInState(State.UNKNOWN)); expectedLogs.verifyWarn("No terminal state was returned. State value UNKNOWN"); }
@Test(expected = AssertionError.class) public void testVerifyNotLoggedWhenMatchedFully() throws Throwable { String expected = generateRandomString(); LOG.error(expected); expectedLogs.verifyNotLogged(expected); }