@SuppressWarnings( "rawtypes" ) public static boolean contains( final Throwable cause, final String containsMessage, final Class... anyOfTheseClasses ) { final Predicate<Throwable> anyOfClasses = Predicates.instanceOfAny( anyOfTheseClasses ); return contains( cause, item -> item.getMessage() != null && item.getMessage().contains( containsMessage ) && anyOfClasses.test( item ) ); }
private boolean hasCriticalFailure() { return !isHealthy() && Exceptions.contains( causeOfPanic, CRITICAL_EXCEPTIONS ); }
@SuppressWarnings( "rawtypes" ) public static boolean contains( Throwable cause, Class... anyOfTheseClasses ) { return contains( cause, org.neo4j.function.Predicates.instanceOfAny( anyOfTheseClasses ) ); }
@Override public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) throws Exception { try { // Netty throws a NativeIoException on connection reset - directly importing that class // caused a host of linking errors, because it depends on JNI to work. Hence, we just // test on the message we know we'll get. if ( Exceptions.contains( cause, e -> e.getMessage().contains( "Connection reset by peer" ) ) ) { log.warn( "Fatal error occurred when initialising pipeline, " + "remote peer unexpectedly closed connection: %s", ctx.channel() ); } else { log.error( "Fatal error occurred when initialising pipeline: " + ctx.channel(), cause ); } } finally { ctx.close(); } }
private IOException loggedIOException( IOException exception ) { if ( Exceptions.contains( exception, "Broken pipe", IOException.class ) ) { log.error( "Unable to reply to request, because the client has closed the connection (Broken pipe)." ); } else { log.error( "Failed to generate JSON output.", exception ); } return exception; } }
@Override public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) { if ( failed || isShuttingDown( ctx ) ) { return; } failed = true; // log only the first exception to not pollute the log try { // Netty throws a NativeIoException on connection reset - directly importing that class // caused a host of linking errors, because it depends on JNI to work. Hence, we just // test on the message we know we'll get. if ( Exceptions.contains( cause, e -> e.getMessage().contains( "Connection reset by peer" ) ) ) { log.warn( "Fatal error occurred when handling a client connection, " + "remote peer unexpectedly closed connection: %s", ctx.channel() ); } else { log.error( "Fatal error occurred when handling a client connection: " + ctx.channel(), cause ); } } finally { ctx.close(); } }
static void assertExceptionContains( Exception e, String message, Class<? extends Exception> type ) throws Exception { if ( !contains( e, message, type ) ) { // Rethrow the exception since we'd like to see what it was instead throw withMessage( e, format( "Expected exception to contain cause '%s', %s. but was %s", message, type, e ) ); } }
@Test void shouldDetectContainsOneOfSome() { // GIVEN Throwable cause = new ARuntimeException( new AnotherRuntimeException( new NullPointerException( "Some words" ) ) ); // THEN assertTrue( Exceptions.contains( cause, NullPointerException.class ) ); assertTrue( Exceptions.contains( cause, "words", NullPointerException.class ) ); assertFalse( Exceptions.contains( cause, "not", NullPointerException.class ) ); }
@Test public void shouldFailWithProperMessageOnAppendException() throws Exception { // GIVEN TransactionAppender appender = mock( TransactionAppender.class ); IOException rootCause = new IOException( "Mock exception" ); doThrow( new IOException( rootCause ) ).when( appender ).append( any( TransactionToApply.class ), any( LogAppendEvent.class ) ); StorageEngine storageEngine = mock( StorageEngine.class ); TransactionCommitProcess commitProcess = new TransactionRepresentationCommitProcess( appender, storageEngine ); // WHEN try { commitProcess.commit( mockedTransaction(), commitEvent, INTERNAL ); fail( "Should have failed, something is wrong with the mocking in this test" ); } catch ( TransactionFailureException e ) { assertThat( e.getMessage(), containsString( "Could not append transaction representation to log" ) ); assertTrue( contains( e, rootCause.getMessage(), rootCause.getClass() ) ); } }
@Test public void shouldCloseTransactionRegardlessOfWhetherOrNotItAppliedCorrectly() throws Exception { // GIVEN TransactionIdStore transactionIdStore = mock( TransactionIdStore.class ); TransactionAppender appender = new TestableTransactionAppender( transactionIdStore ); long txId = 11; when( transactionIdStore.nextCommittingTransactionId() ).thenReturn( txId ); IOException rootCause = new IOException( "Mock exception" ); StorageEngine storageEngine = mock( StorageEngine.class ); doThrow( new IOException( rootCause ) ).when( storageEngine ).apply( any( TransactionToApply.class ), any( TransactionApplicationMode.class ) ); TransactionCommitProcess commitProcess = new TransactionRepresentationCommitProcess( appender, storageEngine ); TransactionToApply transaction = mockedTransaction(); // WHEN try { commitProcess.commit( transaction, commitEvent, INTERNAL ); } catch ( TransactionFailureException e ) { assertThat( e.getMessage(), containsString( "Could not apply the transaction to the store" ) ); assertTrue( contains( e, rootCause.getMessage(), rootCause.getClass() ) ); } // THEN // we can't verify transactionCommitted since that's part of the TransactionAppender, which we have mocked verify( transactionIdStore, times( 1 ) ).transactionClosed( eq( txId ), anyLong(), anyLong() ); }
@Test public void shouldSurfaceTaskErrorInAssertHealthy() throws Exception { // GIVEN TaskExecutor<Void> executor = new DynamicTaskExecutor<>( 2, 0, 10, PARK, getClass().getSimpleName() ); IOException exception = new IOException( "Failure" ); // WHEN FailingTask failingTask = new FailingTask( exception ); executor.submit( failingTask ); failingTask.latch.await(); failingTask.latch.release(); // WHEN for ( int i = 0; i < 5; i++ ) { try { executor.assertHealthy(); // OK, so the executor hasn't executed the finally block after task was done yet Thread.sleep( 100 ); } catch ( Exception e ) { assertTrue( Exceptions.contains( e, exception.getMessage(), exception.getClass() ) ); return; } } fail( "Should not be considered healthy after failing task" ); }
if ( Exceptions.contains( e, CorruptIndexException.class ) || exceptionContainsStackTraceElementFromPackage( e, "org.apache.lucene" ) )
@Test void executeAllTheJobsWhenSeparateJobFails() { List<DummyJob> allRuns = new ArrayList<>(); collector.init(); DummyJob firstJob = new DummyJob( "first", allRuns ); DummyJob thirdJob = new DummyJob( "third", allRuns ); DummyJob fourthJob = new DummyJob( "fourth", allRuns ); List<DummyJob> expectedJobs = Arrays.asList( firstJob, thirdJob, fourthJob ); collector.add( firstJob ); collector.add( new EvilJob() ); collector.add( thirdJob ); collector.add( fourthJob ); RuntimeException exception = assertThrows( RuntimeException.class, collector::start ); assertTrue( Exceptions.contains( exception, "Resilient to run attempts", RuntimeException.class ) ); assertSame( expectedJobs, allRuns ); }
@Test public void updaterShouldThrowOnDuplicateValues() throws Exception { // given populator.create(); IndexEntryUpdate<IndexDescriptor>[] updates = valueCreatorUtil.someUpdatesWithDuplicateValues( random ); IndexUpdater updater = populator.newPopulatingUpdater( null_property_accessor ); // when for ( IndexEntryUpdate<IndexDescriptor> update : updates ) { updater.process( update ); } try { updater.close(); fail( "Updates should have conflicted" ); } catch ( Exception e ) { // then assertTrue( e.getMessage(), Exceptions.contains( e, IndexEntryConflictException.class ) ); } finally { populator.close( true ); } }
assertTrue( Exceptions.contains( e, failureMessage, RuntimeException.class ) ); failed = true;
assertTrue( contains( e, IOException.class ) ); assertTrue( fired.booleanValue() );
assertTrue( Exceptions.contains( exceptionThrown, IOException.class ) );
@SuppressWarnings( "rawtypes" ) public static boolean contains( Throwable cause, Class... anyOfTheseClasses ) { return contains( cause, org.neo4j.function.Predicates.instanceOfAny( anyOfTheseClasses ) ); }
private IOException loggedIOException( IOException exception ) { if ( Exceptions.contains( exception, "Broken pipe", IOException.class ) ) { log.error( "Unable to reply to request, because the client has closed the connection (Broken pipe)." ); } else { log.error( "Failed to generate JSON output.", exception ); } return exception; } }