public AbstractAsyncThreadContextTestBase(final ContextImpl contextImpl, final Mode asyncMode) { this.contextImpl = contextImpl; this.asyncMode = asyncMode; asyncMode.initSelector(); asyncMode.initConfigFile(); contextImpl.init(); }
@Override BlockingQueue<String> createQueue(final int capacity) { return DisruptorBlockingQueueFactory.<String>createFactory(SpinPolicy.SPINNING).create(capacity); } }
@Override BlockingQueue<String> createQueue(final int capacity) { return JCToolsBlockingQueueFactory.<String>createFactory(WaitStrategy.SPIN).create(capacity); }
@Test public void testGetRouteDiscardsIfQueueFullAndLevelEqualOrLessSpecificThanThreshold() throws Exception { final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN); for (final Level level : new Level[] {Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE, Level.ALL}) { assertEquals(level.name(), EventRoute.DISCARD, router.getRoute(currentThreadId(), level)); assertEquals(level.name(), EventRoute.DISCARD, router.getRoute(otherThreadId(), level)); } }
@Test public void testGetRouteSynchronousIfQueueFullAndCalledFromSameThread() throws Exception { final DefaultAsyncQueueFullPolicy router = new DefaultAsyncQueueFullPolicy(); assertEquals(EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), Level.ALL)); assertEquals(EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), Level.OFF)); } }
@Test public void testGetRouteEnqueuesIfQueueFullAndCalledFromDifferentThread() throws Exception { final DefaultAsyncQueueFullPolicy router = new DefaultAsyncQueueFullPolicy(); assertEquals(EventRoute.ENQUEUE, router.getRoute(otherThreadId(), Level.ALL)); assertEquals(EventRoute.ENQUEUE, router.getRoute(otherThreadId(), Level.OFF)); }
@Test public void testGetRouteSynchronousIfCurrentThreadQueueFullAndLevelMoreSpecificThanThreshold() throws Exception { final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN); for (final Level level : new Level[] {Level.ERROR, Level.FATAL, Level.OFF}) { assertEquals(level.name(), EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), level)); } }
@Test public void testGetRouteEnqueueIfOtherThreadQueueFullAndLevelMoreSpecificThanThreshold() throws Exception { final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN); for (final Level level : new Level[] {Level.ERROR, Level.FATAL, Level.OFF}) { assertEquals(level.name(), EventRoute.ENQUEUE, router.getRoute(otherThreadId(), level)); } }
@Test public void testCreateDiscardingRouterDefaultThresholdLevelInfo() throws Exception { System.setProperty(AsyncQueueFullPolicyFactory.PROPERTY_NAME_ASYNC_EVENT_ROUTER, AsyncQueueFullPolicyFactory.PROPERTY_VALUE_DISCARDING_ASYNC_EVENT_ROUTER); assertEquals(Level.INFO, ((DiscardingAsyncQueueFullPolicy) AsyncQueueFullPolicyFactory.create()). getThresholdLevel()); }
@Test(timeout = 5000) public void testLoggingFromToStringCausesOutOfOrderMessages() throws InterruptedException { //TRACE = true; final Logger logger = LogManager.getLogger(this.getClass()); blockingAppender.countDownLatch = new CountDownLatch(1); unlocker = new Unlocker(new CountDownLatch(129)); // count slightly different from "pure" async loggers unlocker.start(); asyncLoggerConfigRecursiveTest(logger, unlocker, blockingAppender, this); }
@Test public void testForEachParameterNothingSet() { final RingBufferLogEvent evt = new RingBufferLogEvent(); evt.forEachParameter(new ParameterConsumer<Void>() { @Override public void accept(Object parameter, int parameterIndex, Void state) { fail("Should not have been called"); } }, null); } }
@Test(timeout = 5000) public void testNormalQueueFullKeepsMessagesInOrder() throws InterruptedException { final Logger logger = LogManager.getLogger(this.getClass()); blockingAppender.countDownLatch = new CountDownLatch(1); unlocker = new Unlocker(new CountDownLatch(129)); unlocker.start(); asyncAppenderTest(logger, unlocker, blockingAppender); }
@Test(timeout = 5000) public void testLoggingFromToStringCausesOutOfOrderMessages() throws InterruptedException { final Logger logger = LogManager.getLogger(this.getClass()); blockingAppender.countDownLatch = new CountDownLatch(1); unlocker = new Unlocker(new CountDownLatch(129)); unlocker.start(); asyncLoggerRecursiveTest(logger, unlocker, blockingAppender, this); }
@Test(timeout = 5000) public void testNormalQueueFullKeepsMessagesInOrder() throws InterruptedException { final Logger logger = LogManager.getLogger(this.getClass()); blockingAppender.countDownLatch = new CountDownLatch(1); unlocker = new Unlocker(new CountDownLatch(129)); unlocker.start(); asyncLoggerConfigTest(logger, unlocker, blockingAppender); }
@Override public boolean stop(final long timeout, final TimeUnit timeUnit) { setStopping(); super.stop(timeout, timeUnit, false); setStopped(); return true; } }
@Test(expected = NullPointerException.class) public void testConstructorDisallowsNullThresholdLevel() { new DiscardingAsyncQueueFullPolicy(null); }
@Test public void testGetRouteEnqueuesIfThresholdCapacityReachedButLevelMoreSpecificThanThreshold() throws Exception { final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN); for (final Level level : new Level[] {Level.ERROR, Level.FATAL, Level.OFF}) { assertEquals(level.name(), EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), level)); assertEquals(level.name(), EventRoute.ENQUEUE, router.getRoute(otherThreadId(), level)); assertEquals(level.name(), EventRoute.SYNCHRONOUS, router.getRoute(currentThreadId(), level)); assertEquals(level.name(), EventRoute.ENQUEUE, router.getRoute(otherThreadId(), level)); } }
@Test public void testCreateDiscardingRouterThresholdLevelCustomizable() throws Exception { System.setProperty(AsyncQueueFullPolicyFactory.PROPERTY_NAME_ASYNC_EVENT_ROUTER, AsyncQueueFullPolicyFactory.PROPERTY_VALUE_DISCARDING_ASYNC_EVENT_ROUTER); for (final Level level : Level.values()) { System.setProperty(AsyncQueueFullPolicyFactory.PROPERTY_NAME_DISCARDING_THRESHOLD_LEVEL, level.name()); assertEquals(level, ((DiscardingAsyncQueueFullPolicy) AsyncQueueFullPolicyFactory.create()). getThresholdLevel()); } }
@Test(timeout = 5000) public void testLoggingFromToStringCausesOutOfOrderMessages() throws InterruptedException { //TRACE = true; final Logger logger = LogManager.getLogger(this.getClass()); blockingAppender.countDownLatch = new CountDownLatch(1); unlocker = new Unlocker(new CountDownLatch(129)); // count slightly different from "pure" async loggers unlocker.start(); QueueFullAsyncLoggerConfigLoggingFromToStringTest.asyncLoggerConfigRecursiveTest(logger, unlocker, blockingAppender, this); } }
@Test public void testGetRouteDiscardsIfThresholdCapacityReachedAndLevelEqualOrLessSpecificThanThreshold() throws Exception { final DiscardingAsyncQueueFullPolicy router = new DiscardingAsyncQueueFullPolicy(Level.WARN); for (final Level level : new Level[] {Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE, Level.ALL}) { assertEquals(level.name(), EventRoute.DISCARD, router.getRoute(currentThreadId(), level)); assertEquals(level.name(), EventRoute.DISCARD, router.getRoute(otherThreadId(), level)); assertEquals(level.name(), EventRoute.DISCARD, router.getRoute(currentThreadId(), level)); assertEquals(level.name(), EventRoute.DISCARD, router.getRoute(otherThreadId(), level)); } }