@Override public void log( @Nonnull String format, @Nonnull Object... arguments ) { matcher = builder.error( messageMatcher( format ), arguments ); }
@Override public void log( @Nonnull String message, @Nonnull Throwable throwable ) { matcher = builder.error( messageMatcher( message ), sameInstance( throwable ) ); }
@Override public void log( @Nonnull String message ) { matcher = builder.error( messageMatcher( message ) ); }
@Test public void shouldDescribeUpgradeFailureInAFriendlyWay() { // given AssertableLogProvider logging = new AssertableLogProvider(); LifecycleException error = new LifecycleException( new Object(), STARTING, STARTED, new RuntimeException( "Error starting org.neo4j.kernel.ha.factory.EnterpriseFacadeFactory", new LifecycleException( new Object(), STARTING, STARTED, new LifecycleException( new Object(), STARTING, STARTED, new UpgradeNotAllowedByConfigurationException() ) ) ) ); // when translateToServerStartupError( error ).describeTo( logging.getLog( "console" ) ); // then logging.assertExactly( inLog( "console" ) .error( "Neo4j cannot be started because the database files require upgrading and upgrades are disabled " + "in the configuration. Please set '" + GraphDatabaseSettings.allow_upgrade.name() + "' to 'true' " + "in your configuration file and try again." ) ); } }
@Test public void shouldLogIOErrors() { // given IOException failure = new IOException(); OutputStream output = mock( OutputStream.class, new ThrowsException( failure ) ); AssertableLogProvider logProvider = new AssertableLogProvider(); ExecutionResultSerializer serializer = getSerializerWith( output, null, logProvider ); // when serializer.finish(); // then logProvider.assertExactly( AssertableLogProvider.inLog( ExecutionResultSerializer.class ).error( is( "Failed to generate JSON output." ), sameInstance( failure ) ) ); }
private AssertableLogProvider.LogMatcher currentLog( AssertableLogProvider.LogMatcherBuilder logMatcherBuilder, Matcher<String> stringMatcher ) { switch ( logName ) { case "debug": return logMatcherBuilder.debug( stringMatcher ); case "info": return logMatcherBuilder.info( stringMatcher ); case "warn": return logMatcherBuilder.warn( stringMatcher ); case "error": return logMatcherBuilder.error( stringMatcher ); default: throw new RuntimeException( "Unknown log name" ); } }
@Test public void shouldLogOnUnexpectedExceptionsAndClosesContext() throws Throwable { // Given ChannelHandlerContext context = channelHandlerContextMock(); AssertableLogProvider logging = new AssertableLogProvider(); TransportSelectionHandler handler = new TransportSelectionHandler( null, null, false, false, logging, null ); // When Throwable cause = new Throwable( "Oh no!" ); handler.exceptionCaught( context, cause ); // Then verify( context ).close(); logging.assertExactly( inLog( TransportSelectionHandler.class ) .error( equalTo( "Fatal error occurred when initialising pipeline: " + context.channel() ), sameInstance( cause ) ) ); }
@Test public void processNextBatchShouldCloseConnectionAndLogOnUnexpectedException() { RuntimeException exception = new RuntimeException( "unexpected exception" ); BoltConnection connection = newConnection(); connection.enqueue( machine -> { throw exception; } ); connection.processNextBatch(); verify( stateMachine ).close(); logProvider.assertExactly( AssertableLogProvider.inLog( containsString( BoltServer.class.getPackage().getName() ) ).error( containsString( "Unexpected error detected in bolt session" ), is( exception ) ) ); }
@Test public void processNextBatchShouldCloseConnectionAndLogOnFatalBoltError() { BoltConnectionFatality exception = new BoltProtocolBreachFatality( "fatal bolt error" ); BoltConnection connection = newConnection(); connection.enqueue( machine -> { throw exception; } ); connection.processNextBatch(); verify( stateMachine ).close(); logProvider.assertExactly( AssertableLogProvider.inLog( containsString( BoltServer.class.getPackage().getName() ) ).error( containsString( "Protocol breach detected in bolt session" ), is( exception ) ) ); }
@Test public void shouldAbbreviateWellKnownIOErrors() { // given OutputStream output = mock( OutputStream.class, new ThrowsException( new IOException( "Broken pipe" ) ) ); AssertableLogProvider logProvider = new AssertableLogProvider(); ExecutionResultSerializer serializer = getSerializerWith( output, null, logProvider ); // when serializer.finish(); // then logProvider.assertExactly( AssertableLogProvider.inLog( ExecutionResultSerializer.class ) .error( "Unable to reply to request, because the client has closed the connection (Broken pipe)." ) ); }
public LogMatcher error( String format, Object... arguments ) { return error( equalTo( format ), arguments ); }
@Test public void processNextBatchShouldNotThrowAssertionErrorIfStatementOpenButStopping() throws Exception { BoltConnection connection = newConnection( 1 ); connection.enqueue( Jobs.noop() ); connection.enqueue( Jobs.noop() ); // force to a message waiting loop when( stateMachine.hasOpenStatement() ).thenReturn( true ); connection.stop(); connection.processNextBatch(); logProvider.assertNone( AssertableLogProvider.inLog( DefaultBoltConnection.class.getName() ).error( startsWith( "Unexpected error" ), isA( AssertionError.class ) ) ); }
@Test void shouldFormatLogForChangedRecord() { // when logger.error( RecordType.NEO_STORE, new NeoStoreRecord(), new NeoStoreRecord(), "sample message", 1, 2 ); // then logProvider.assertExactly( INLOG.error( join( "sample message", "- " + neoStoreRecord( true, -1 ), "+ " + neoStoreRecord( true, -1 ), "Inconsistent with: 1 2" ) ) ); }
private static AssertableLogProvider.LogMatcher loggedException( Throwable exception ) { return inLog( CoreMatchers.any( String.class ) ) .error( CoreMatchers.any( String.class ), sameInstance( exception ) ); } }
@Test public void shouldLogFailedRule() { AssertableLogProvider logProvider = new AssertableLogProvider(); PreFlightTasks check = new PreFlightTasks( logProvider, getWithOneFailingRule() ); check.run(); logProvider.assertExactly( inLog( PreFlightTasks.class ).error( "blah blah" ) ); }
@Test public void processNextBatchShouldThrowAssertionErrorIfStatementOpen() throws Exception { BoltConnection connection = newConnection( 1 ); connection.enqueue( Jobs.noop() ); connection.enqueue( Jobs.noop() ); // force to a message waiting loop when( stateMachine.hasOpenStatement() ).thenReturn( true ); connection.processNextBatch(); logProvider.assertExactly( AssertableLogProvider.inLog( DefaultBoltConnection.class.getName() ).error( startsWith( "Unexpected error" ), isA( AssertionError.class ) ) ); }
@Test void shouldFormatErrorForRecord() { // when logger.error( RecordType.NEO_STORE, new NeoStoreRecord(), "sample message", 1, 2 ); // then logProvider.assertExactly( INLOG.error( join( "sample message", neoStoreRecord( true, -1 ), "Inconsistent with: 1 2" ) ) ); }
@Test public void shouldLogExceptionOnExceptionCaught() { AssertableLogProvider logProvider = new AssertableLogProvider(); BoltConnection connection = mock( BoltConnection.class ); channel = new EmbeddedChannel( new HouseKeeper( connection, logProvider.getLog( HouseKeeper.class ) ) ); RuntimeException exception = new RuntimeException( "some exception" ); channel.pipeline().fireExceptionCaught( exception ); verify( connection ).stop(); logProvider.assertExactly( inLog( HouseKeeper.class ).error( startsWith( "Fatal error occurred when handling a client connection" ), equalTo( exception ) ) ); }
@Test void shouldFlattenAMultiLineMessageToASingleLine() { // when logger.error( RecordType.NEO_STORE, new NeoStoreRecord(), "multiple\n line\r\n message", 1, 2 ); // then logProvider.assertExactly( INLOG.error( join( "multiple line message", neoStoreRecord( true, -1 ), "Inconsistent with: 1 2" ) ) ); }
private static void testLoggingOfWriteErrorAndOriginalErrorWhenUnknownFailure( Neo4jError original ) throws Exception { RuntimeException outputError = new RuntimeException( "Output failed" ); AssertableLogProvider logProvider = emulateFailureWritingError( original, outputError ); logProvider.assertExactly( inLog( "Test" ).error( startsWith( "Unable to send error back to the client" ), both( equalTo( outputError ) ).and( hasSuppressed( original.cause() ) ) ) ); }