private void collect( ProblemReporter report ) { boolean collect = collects( report.type() ); if ( collect ) { // This type of problem is collected and we're within the max threshold, so it's OK long count = badEntries.incrementAndGet(); if ( tolerance == UNLIMITED_TOLERANCE || count <= tolerance ) { // We're within the threshold if ( logBadEntries ) { // Send this to the logger... but first apply some back pressure if queue is growing big while ( queueSize.sum() >= backPressureThreshold ) { LockSupport.parkNanos( TimeUnit.MILLISECONDS.toNanos( 10 ) ); } logger.send( report ); queueSize.add( 1 ); } return; // i.e. don't treat this as an exception } } InputException exception = report.exception(); throw collect ? withMessage( exception, format( "Too many bad entries %d, where last one was: %s", badEntries.longValue(), exception.getMessage() ) ) : exception; }
@Test void mustNotProcessEventInSameThreadWhenNotShutDown() throws Exception { EventConsumer consumer = new EventConsumer(); AsyncEvents<Event> asyncEvents = new AsyncEvents<>( consumer, AsyncEvents.Monitor.NONE ); executor.submit( asyncEvents ); asyncEvents.send( new Event() ); Thread processingThread = consumer.poll( 10, TimeUnit.SECONDS ).processedBy; asyncEvents.shutdown(); assertThat( processingThread, is( not( Thread.currentThread() ) ) ); }
@Test void eventsMustBeProcessedByBackgroundThread() throws Exception { EventConsumer consumer = new EventConsumer(); AsyncEvents<Event> asyncEvents = new AsyncEvents<>( consumer, AsyncEvents.Monitor.NONE ); executor.submit( asyncEvents ); Event firstSentEvent = new Event(); asyncEvents.send( firstSentEvent ); Event firstProcessedEvent = consumer.poll( 10, TimeUnit.SECONDS ); Event secondSentEvent = new Event(); asyncEvents.send( secondSentEvent ); Event secondProcessedEvent = consumer.poll( 10, TimeUnit.SECONDS ); asyncEvents.shutdown(); assertThat( firstProcessedEvent, is( firstSentEvent ) ); assertThat( secondProcessedEvent, is( secondSentEvent ) ); }
@Test void mustProcessEventsDirectlyWhenShutDown() { assertTimeout( ofSeconds( 10 ), () -> { EventConsumer consumer = new EventConsumer(); AsyncEvents<Event> asyncEvents = new AsyncEvents<>( consumer, AsyncEvents.Monitor.NONE ); executor.submit( asyncEvents ); asyncEvents.send( new Event() ); Thread threadForFirstEvent = consumer.poll( 10, TimeUnit.SECONDS ).processedBy; asyncEvents.shutdown(); assertThat( threadForFirstEvent, is( not( Thread.currentThread() ) ) ); Thread threadForSubsequentEvents; do { asyncEvents.send( new Event() ); threadForSubsequentEvents = consumer.poll( 10, TimeUnit.SECONDS ).processedBy; } while ( threadForSubsequentEvents != Thread.currentThread() ); } ); }
private void collect( ProblemReporter report ) { boolean collect = collects( report.type() ); if ( collect ) { // This type of problem is collected and we're within the max threshold, so it's OK long count = badEntries.incrementAndGet(); if ( tolerance == UNLIMITED_TOLERANCE || count <= tolerance ) { // We're within the threshold if ( logBadEntries ) { // Send this to the logger logger.send( report ); } return; // i.e. don't treat this as an exception } } InputException exception = report.exception(); throw collect ? withMessage( exception, format( "Too many bad entries %d, where last one was: %s", badEntries.longValue(), exception.getMessage() ) ) : exception; }