protected Appender<E> wrapAsync(Appender<E> appender, AsyncAppenderFactory<E> asyncAppenderFactory, Context context) { final AsyncAppenderBase<E> asyncAppender = asyncAppenderFactory.build(); if (asyncAppender instanceof AsyncAppender) { ((AsyncAppender) asyncAppender).setIncludeCallerData(includeCallerData); } asyncAppender.setQueueSize(queueSize); asyncAppender.setDiscardingThreshold(discardingThreshold); asyncAppender.setContext(context); asyncAppender.setName("async-" + appender.getName()); asyncAppender.addAppender(appender); asyncAppender.setNeverBlock(neverBlock); asyncAppender.start(); if (messageRate == null) { return asyncAppender; } else { return new ThrottlingAppenderWrapper<>(asyncAppender, messageRate.getQuantity(), messageRate.getUnit()); } }
@Test public void verifyInterruptionIsNotSwallowed() { asyncAppenderBase.addAppender(delayingListAppender); asyncAppenderBase.start(); Thread.currentThread().interrupt(); asyncAppenderBase.doAppend(0); assertTrue(Thread.currentThread().isInterrupted()); // clear interrupt flag for subsequent tests Thread.interrupted(); }
@Test public void verifyInterruptionDoesNotPreventLogging() { asyncAppenderBase.addAppender(listAppender); asyncAppenderBase.start(); asyncAppenderBase.doAppend(0); Thread.currentThread().interrupt(); asyncAppenderBase.doAppend(1); asyncAppenderBase.doAppend(1); assertTrue(Thread.currentThread().isInterrupted()); asyncAppenderBase.stop(); verify(listAppender, 3); // clear interrupt flag for subsequent tests Thread.interrupted(); }
@Test public void checkThatStartMethodIsIdempotent() { asyncAppenderBase.addAppender(lossyAsyncAppender); asyncAppenderBase.start(); // we don't need mockito for this test, but if we did here is how it would look //AsyncAppenderBase<Integer> spied = Mockito.spy(asyncAppenderBase); //Mockito.doThrow(new IllegalStateException("non idempotent start")).when((UnsynchronizedAppenderBase<Integer>) spied).start(); // a second invocation of start will cause a IllegalThreadStateException thrown by the asyncAppenderBase.worker thread asyncAppenderBase.start(); } }
@Test(timeout = 2000) public void emptyQueueShouldBeStoppable() { asyncAppenderBase.addAppender(listAppender); asyncAppenderBase.start(); asyncAppenderBase.stop(); verify(listAppender, 0); }
@Test(timeout = 2000) public void workerShouldStopEvenIfInterruptExceptionConsumedWithinSubappender() { delayingListAppender.delay = 100; asyncAppenderBase.addAppender(delayingListAppender); asyncAppenderBase.start(); asyncAppenderBase.doAppend(0); asyncAppenderBase.stop(); verify(delayingListAppender, 1); assertTrue(delayingListAppender.interrupted); Thread.interrupted(); }
protected Appender<E> wrapAsync(Appender<E> appender, AsyncAppenderFactory<E> asyncAppenderFactory, Context context) { final AsyncAppenderBase<E> asyncAppender = asyncAppenderFactory.build(); if (asyncAppender instanceof AsyncAppender) { ((AsyncAppender) asyncAppender).setIncludeCallerData(includeCallerData); } asyncAppender.setQueueSize(queueSize); asyncAppender.setDiscardingThreshold(discardingThreshold); asyncAppender.setContext(context); asyncAppender.setName("async-" + appender.getName()); asyncAppender.addAppender(appender); asyncAppender.setNeverBlock(neverBlock); asyncAppender.start(); return asyncAppender; }
@Test(timeout = 2000) public void smoke() { asyncAppenderBase.addAppender(listAppender); asyncAppenderBase.start(); asyncAppenderBase.doAppend(0); asyncAppenderBase.stop(); verify(listAppender, 1); }
@Test(timeout = 2000) public void noEventLoss() { int bufferSize = 10; int loopLen = bufferSize * 2; asyncAppenderBase.addAppender(delayingListAppender); asyncAppenderBase.setQueueSize(bufferSize); asyncAppenderBase.start(); for (int i = 0; i < loopLen; i++) { asyncAppenderBase.doAppend(i); } asyncAppenderBase.stop(); verify(delayingListAppender, loopLen); }
@Test public void verifyInterruptionOfWorkerIsSwallowed() { asyncAppenderBase.addAppender(delayingListAppender); asyncAppenderBase.start(); asyncAppenderBase.stop(); assertFalse(asyncAppenderBase.worker.isAlive()); assertFalse(asyncAppenderBase.worker.isInterrupted()); }
@Test public void invalidQueueCapacityShouldResultInNonStartedAppender() { asyncAppenderBase.addAppender(new NOPAppender<Integer>()); asyncAppenderBase.setQueueSize(0); assertEquals(0, asyncAppenderBase.getQueueSize()); asyncAppenderBase.start(); assertFalse(asyncAppenderBase.isStarted()); statusChecker.assertContainsMatch("Invalid queue size"); }
@SuppressWarnings("deprecation") @Test public void workerThreadFlushesOnStop() { int loopLen = 5; int maxRuntime = (loopLen + 1) * Math.max(1000, delayingListAppender.delay); ListAppender<Integer> la = delayingListAppender; asyncAppenderBase.addAppender(la); asyncAppenderBase.setDiscardingThreshold(0); asyncAppenderBase.setMaxFlushTime(maxRuntime); asyncAppenderBase.start(); asyncAppenderBase.worker.suspend(); for (int i = 0; i < loopLen; i++) { asyncAppenderBase.doAppend(i); } assertEquals(loopLen, asyncAppenderBase.getNumberOfElementsInQueue()); assertEquals(0, la.list.size()); asyncAppenderBase.worker.resume(); asyncAppenderBase.stop(); assertEquals(0, asyncAppenderBase.getNumberOfElementsInQueue()); verify(la, loopLen); }
@Test(timeout = 2000) public void eventLossIfNeverBlock() { int bufferSize = 10; int loopLen = bufferSize * 2; delayingListAppender.setDelay(5000); // something greater than the test timeout asyncAppenderBase.addAppender(delayingListAppender); asyncAppenderBase.setQueueSize(bufferSize); asyncAppenderBase.setNeverBlock(true); asyncAppenderBase.start(); for (int i = 0; i < loopLen; i++) { asyncAppenderBase.doAppend(i); } asyncAppenderBase.stop(); // ListAppender size isn't a reliable test here, so just make sure we didn't // have any errors, and that we could complete the test in time. statusChecker.assertIsErrorFree(); }
@Test public void stopExitsWhenMaxRuntimeReached() throws InterruptedException { int maxFlushTime = 1; // runtime of 0 means wait forever, so use 1 ms instead int loopLen = 10; ListAppender<Integer> la = delayingListAppender; asyncAppenderBase.addAppender(la); asyncAppenderBase.setMaxFlushTime(maxFlushTime); asyncAppenderBase.start(); for (int i = 0; i < loopLen; i++) { asyncAppenderBase.doAppend(i); } asyncAppenderBase.stop(); // confirms that stop exited when runtime reached statusChecker.assertContainsMatch("Max queue flush timeout \\(" + maxFlushTime + " ms\\) exceeded."); asyncAppenderBase.worker.join(); // confirms that all entries do end up being flushed if we wait long enough verify(la, loopLen); }
@Test public void exceptionsShouldNotCauseHalting() throws InterruptedException { NPEAppender<Integer> npeAppender = new NPEAppender<Integer>(); npeAppender.setName("bad"); npeAppender.setContext(context); npeAppender.start(); asyncAppenderBase.addAppender(npeAppender); asyncAppenderBase.start(); assertTrue(asyncAppenderBase.isStarted()); for (int i = 0; i < 10; i++) asyncAppenderBase.append(i); asyncAppenderBase.stop(); assertFalse(asyncAppenderBase.isStarted()); assertEquals(AppenderBase.ALLOWED_REPEATS, statusChecker.matchCount("Appender \\[bad\\] failed to append.")); }
asyncAppender.setContext(context); asyncAppender.setQueueSize(queueSize); asyncAppender.addAppender(fileAppender);
public RollingFileHandler(String filename, int maxHistory, long maxSizeInBytes) { setFormatter(new StaticFormatter()); ContextBase context = new ContextBase(); recoverTempFiles(filename); RollingFileAppender<String> fileAppender = new RollingFileAppender<>(); TimeBasedRollingPolicy<String> rollingPolicy = new TimeBasedRollingPolicy<>(); SizeAndTimeBasedFNATP<String> triggeringPolicy = new SizeAndTimeBasedFNATP<>(); rollingPolicy.setContext(context); rollingPolicy.setFileNamePattern(filename + "-%d{yyyy-MM-dd}.%i.log.gz"); rollingPolicy.setMaxHistory(maxHistory); rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy); rollingPolicy.setParent(fileAppender); triggeringPolicy.setContext(context); triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy); triggeringPolicy.setMaxFileSize(new FileSize(maxSizeInBytes)); fileAppender.setContext(context); fileAppender.setFile(filename); fileAppender.setAppend(true); fileAppender.setBufferSize(BUFFER_SIZE_IN_BYTES); fileAppender.setEncoder(new StringEncoder()); fileAppender.setRollingPolicy(rollingPolicy); asyncAppender = new AsyncAppenderBase<>(); asyncAppender.setContext(context); asyncAppender.addAppender(fileAppender); rollingPolicy.start(); triggeringPolicy.start(); fileAppender.start(); asyncAppender.start(); }
asyncAppender.setContext(context); asyncAppender.setQueueSize(queueSize); asyncAppender.addAppender(fileAppender);
asyncAppender.setContext(context); asyncAppender.setQueueSize(queueSize); asyncAppender.addAppender(fileAppender);