--- /dev/null
+package org.lttng.ust.agent.integration.common;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.List;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.lttng.ust.agent.ILttngHandler;
+import org.lttng.ust.agent.utils.LttngSession;
+import org.lttng.ust.agent.utils.LttngSession.Domain;
+
+/**
+ * Tests with multiple concurrent tracing sessions
+ */
+public abstract class MultiSessionTest {
+
+ protected static final String EVENT_NAME_A = "EventA";
+ protected static final String EVENT_NAME_B = "EventAB";
+ protected static final String EVENT_NAME_C = "EventABC";
+ protected static final String EVENT_NAME_D = "EventABCD";
+
+ private LttngSession session1;
+ private LttngSession session2;
+ private LttngSession session3;
+
+ /* Fields defined by the sub-class */
+ protected ILttngHandler handlerA;
+ protected ILttngHandler handlerB;
+ protected ILttngHandler handlerC;
+ protected ILttngHandler handlerD;
+
+ protected abstract Domain getDomain();
+
+ protected abstract void sendEventsToLoggers();
+
+ @Before
+ public void testSetup() {
+ session1 = new LttngSession(null, getDomain());
+ session2 = new LttngSession(null, getDomain());
+ session3 = new LttngSession(null, getDomain());
+ }
+
+ @After
+ public void testTeardown() {
+ session1.close();
+ session2.close();
+ session3.close();
+
+ handlerA.close();
+ handlerB.close();
+ handlerC.close();
+ handlerD.close();
+
+ handlerA = null;
+ handlerB = null;
+ handlerC = null;
+ handlerD = null;
+ }
+
+ /**
+ * Test with no events in any session.
+ */
+ @Test
+ public void testNoEvents() {
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertTrue(output1.isEmpty());
+ assertTrue(output2.isEmpty());
+ assertTrue(output3.isEmpty());
+
+ assertEquals(0, handlerA.getEventCount());
+ assertEquals(0, handlerB.getEventCount());
+ assertEquals(0, handlerC.getEventCount());
+ assertEquals(0, handlerD.getEventCount());
+ }
+
+ /**
+ * Test with all events enabled in one session only. Everything should be
+ * sent through JNI, but only that session should keep the trace events.
+ */
+ @Test
+ public void testAllEventsOneSession() {
+ assertTrue(session1.enableAllEvents());
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertEquals(40, output1.size());
+ assertTrue(output2.isEmpty());
+ assertTrue(output3.isEmpty());
+
+ assertEquals(10, handlerA.getEventCount());
+ assertEquals(10, handlerB.getEventCount());
+ assertEquals(10, handlerC.getEventCount());
+ assertEquals(10, handlerD.getEventCount());
+ }
+
+ /**
+ * Test with all events enabled in all sessions. All traces and handlers
+ * should see every event that was logged.
+ */
+ @Test
+ public void testAllEventsAllSessions() {
+ assertTrue(session1.enableAllEvents());
+ assertTrue(session2.enableAllEvents());
+ assertTrue(session3.enableAllEvents());
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertEquals(40, output1.size());
+ assertEquals(40, output2.size());
+ assertEquals(40, output3.size());
+
+ assertEquals(10, handlerA.getEventCount());
+ assertEquals(10, handlerB.getEventCount());
+ assertEquals(10, handlerC.getEventCount());
+ assertEquals(10, handlerD.getEventCount());
+ }
+
+ /**
+ * Test enabling some events in some sessions only.
+ */
+ @Test
+ public void testSomeEvents() {
+ assertTrue(session1.enableEvents(EVENT_NAME_A));
+ assertTrue(session2.enableEvents(EVENT_NAME_B));
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertEquals(10, output1.size());
+ assertEquals(10, output2.size());
+ assertEquals(0, output3.size());
+
+ assertEquals(10, handlerA.getEventCount());
+ assertEquals(10, handlerB.getEventCount());
+ assertEquals(0, handlerC.getEventCount());
+ assertEquals(0, handlerD.getEventCount());
+ }
+
+ /**
+ * Test with all events enabled in one session, and some others in another.
+ * All events should arrive where expected, with no duplicates.
+ */
+ @Test
+ public void testAllEventsAndSome() {
+ assertTrue(session1.enableAllEvents());
+ assertTrue(session2.enableEvents(EVENT_NAME_D));
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertEquals(40, output1.size());
+ assertEquals(10, output2.size());
+ assertEquals(0, output3.size());
+
+ assertEquals(10, handlerA.getEventCount());
+ assertEquals(10, handlerB.getEventCount());
+ assertEquals(10, handlerC.getEventCount());
+ assertEquals(10, handlerD.getEventCount());
+ }
+
+ /**
+ * Test with enabling then disabling some events. Makes sure the refcounting
+ * works properly.
+ */
+ @Test
+ public void testSomeEventsAfterDisabling() {
+ assertTrue(session1.enableEvents(EVENT_NAME_A, EVENT_NAME_B, EVENT_NAME_C));
+ assertTrue(session2.enableEvents(EVENT_NAME_B, EVENT_NAME_C, EVENT_NAME_D));
+ assertTrue(session3.enableEvents(EVENT_NAME_A));
+
+ assertTrue(session1.disableEvents(EVENT_NAME_C));
+ assertTrue(session2.disableEvents(EVENT_NAME_B, EVENT_NAME_C));
+ assertTrue(session3.disableEvents(EVENT_NAME_A));
+
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertEquals(20, output1.size());
+ assertEquals(10, output2.size());
+ assertEquals(0, output3.size());
+
+ assertEquals(10, handlerA.getEventCount());
+ assertEquals(10, handlerB.getEventCount());
+ assertEquals(0, handlerC.getEventCount());
+ assertEquals(10, handlerD.getEventCount());
+ }
+
+ /**
+ * Test with a prefix in one session and a standard event in another.
+ */
+ @Test
+ public void testPrefixAndEvent() {
+ assertTrue(session1.enableEvents("EventAB*"));
+ assertTrue(session3.enableEvents(EVENT_NAME_A));
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertEquals(30, output1.size());
+ assertEquals(0, output2.size());
+ assertEquals(10, output3.size());
+
+ assertEquals(10, handlerA.getEventCount());
+ assertEquals(10, handlerB.getEventCount());
+ assertEquals(10, handlerC.getEventCount());
+ assertEquals(10, handlerD.getEventCount());
+ }
+
+ /**
+ * Test with all events enabled in one session, and an event prefix in
+ * another. Once again, there should be no duplicates.
+ */
+ @Test
+ public void testAllEventsAndPrefix() {
+ assertTrue(session1.enableAllEvents());
+ assertTrue(session2.enableEvents("EventABC*"));
+ assertTrue(session1.start());
+ assertTrue(session2.start());
+ assertTrue(session3.start());
+
+ sendEventsToLoggers();
+
+ assertTrue(session1.stop());
+ assertTrue(session2.stop());
+ assertTrue(session3.stop());
+
+ List<String> output1 = session1.view();
+ List<String> output2 = session2.view();
+ List<String> output3 = session3.view();
+ assertNotNull(output1);
+ assertNotNull(output2);
+ assertNotNull(output3);
+ assertEquals(40, output1.size());
+ assertEquals(20, output2.size());
+ assertEquals(0, output3.size());
+
+ assertEquals(10, handlerA.getEventCount());
+ assertEquals(10, handlerB.getEventCount());
+ assertEquals(10, handlerC.getEventCount());
+ assertEquals(10, handlerD.getEventCount());
+ }
+}
--- /dev/null
+package org.lttng.ust.agent.integration.jul;
+
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.lttng.ust.agent.integration.common.MultiSessionTest;
+import org.lttng.ust.agent.jul.LttngLogHandler;
+import org.lttng.ust.agent.utils.LttngSession;
+import org.lttng.ust.agent.utils.LttngSession.Domain;
+import org.lttng.ust.agent.utils.TestUtils;
+
+public class JulMultiSessionTest extends MultiSessionTest {
+
+ private static final Domain DOMAIN = Domain.JUL;
+
+ private Logger loggerA;
+ private Logger loggerB;
+ private Logger loggerC;
+ private Logger loggerD;
+
+ @BeforeClass
+ public static void julClassSetup() {
+ /* Skip tests if we can't find the JNI library or lttng-tools */
+ assumeTrue(TestUtils.checkForJulLibrary());
+ assumeTrue(TestUtils.checkForLttngTools(Domain.JUL));
+
+ LttngSession.destroyAllSessions();
+ }
+
+ @AfterClass
+ public static void julClassCleanup() {
+ LttngSession.deleteAllTracee();
+ }
+
+ @Before
+ public void julSetup() throws SecurityException, IOException {
+ loggerA = Logger.getLogger(EVENT_NAME_A);
+ loggerB = Logger.getLogger(EVENT_NAME_B);
+ loggerC = Logger.getLogger(EVENT_NAME_C);
+ loggerD = Logger.getLogger(EVENT_NAME_D);
+
+ loggerA.setLevel(Level.ALL);
+ loggerB.setLevel(Level.ALL);
+ loggerC.setLevel(Level.ALL);
+ loggerD.setLevel(Level.ALL);
+
+ handlerA = new LttngLogHandler();
+ handlerB = new LttngLogHandler();
+ handlerC = new LttngLogHandler();
+ handlerD = new LttngLogHandler();
+
+ loggerA.addHandler((Handler) handlerA);
+ loggerB.addHandler((Handler) handlerB);
+ loggerC.addHandler((Handler) handlerC);
+ loggerD.addHandler((Handler) handlerD);
+ }
+
+ @After
+ public void julTeardown() {
+ loggerA.removeHandler((Handler) handlerA);
+ loggerB.removeHandler((Handler) handlerB);
+ loggerC.removeHandler((Handler) handlerC);
+ loggerD.removeHandler((Handler) handlerD);
+
+ loggerA = null;
+ loggerB = null;
+ loggerC = null;
+ loggerD = null;
+ }
+
+ @Override
+ protected Domain getDomain() {
+ return DOMAIN;
+ }
+
+ @Override
+ protected void sendEventsToLoggers() {
+ JulTestUtils.send10EventsTo(loggerA);
+ JulTestUtils.send10EventsTo(loggerB);
+ JulTestUtils.send10EventsTo(loggerC);
+ JulTestUtils.send10EventsTo(loggerD);
+ }
+}
--- /dev/null
+package org.lttng.ust.agent.integration.log4j;
+
+import static org.junit.Assume.assumeTrue;
+
+import java.io.IOException;
+
+import org.apache.log4j.Appender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.lttng.ust.agent.integration.common.MultiSessionTest;
+import org.lttng.ust.agent.log4j.LttngLogAppender;
+import org.lttng.ust.agent.utils.LttngSession;
+import org.lttng.ust.agent.utils.LttngSession.Domain;
+import org.lttng.ust.agent.utils.TestUtils;
+
+public class Log4jMultiSessionTest extends MultiSessionTest {
+
+ private static final Domain DOMAIN = Domain.LOG4J;
+
+ private Logger loggerA;
+ private Logger loggerB;
+ private Logger loggerC;
+ private Logger loggerD;
+
+ @BeforeClass
+ public static void log4jClassSetup() {
+ /* Skip tests if we can't find the JNI library or lttng-tools */
+ assumeTrue(TestUtils.checkForLog4jLibrary());
+ assumeTrue(TestUtils.checkForLttngTools(Domain.LOG4J));
+
+ LttngSession.destroyAllSessions();
+ }
+
+ @AfterClass
+ public static void log4jClassCleanup() {
+ LttngSession.deleteAllTracee();
+ }
+
+ @Before
+ public void log4jSetup() throws SecurityException, IOException {
+ // TODO Wipe all existing LTTng sessions?
+
+ loggerA = Logger.getLogger(EVENT_NAME_A);
+ loggerB = Logger.getLogger(EVENT_NAME_B);
+ loggerC = Logger.getLogger(EVENT_NAME_C);
+ loggerD = Logger.getLogger(EVENT_NAME_D);
+
+ loggerA.setLevel(Level.ALL);
+ loggerB.setLevel(Level.ALL);
+ loggerC.setLevel(Level.ALL);
+ loggerD.setLevel(Level.ALL);
+
+ handlerA = new LttngLogAppender();
+ handlerB = new LttngLogAppender();
+ handlerC = new LttngLogAppender();
+ handlerD = new LttngLogAppender();
+
+ loggerA.addAppender((Appender) handlerA);
+ loggerB.addAppender((Appender) handlerB);
+ loggerC.addAppender((Appender) handlerC);
+ loggerD.addAppender((Appender) handlerD);
+ }
+
+ @After
+ public void log4jTeardown() {
+ loggerA.removeAppender((Appender) handlerA);
+ loggerB.removeAppender((Appender) handlerB);
+ loggerC.removeAppender((Appender) handlerC);
+ loggerD.removeAppender((Appender) handlerD);
+
+ loggerA = null;
+ loggerB = null;
+ loggerC = null;
+ loggerD = null;
+ }
+
+ @Override
+ protected Domain getDomain() {
+ return DOMAIN;
+ }
+
+ @Override
+ protected void sendEventsToLoggers() {
+ Log4jTestUtils.send10Events(loggerA);
+ Log4jTestUtils.send10Events(loggerB);
+ Log4jTestUtils.send10Events(loggerC);
+ Log4jTestUtils.send10Events(loggerD);
+ }
+}