Add multi-session tests
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 24 Jul 2015 08:58:25 +0000 (04:58 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 24 Jul 2015 08:58:25 +0000 (04:58 -0400)
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
src/org/lttng/ust/agent/integration/common/MultiSessionTest.java [new file with mode: 0644]
src/org/lttng/ust/agent/integration/jul/AllTests.java
src/org/lttng/ust/agent/integration/jul/JulEnabledEventsTest.java
src/org/lttng/ust/agent/integration/jul/JulLegacyApiTest.java
src/org/lttng/ust/agent/integration/jul/JulMultiSessionTest.java [new file with mode: 0644]
src/org/lttng/ust/agent/integration/log4j/AllTests.java
src/org/lttng/ust/agent/integration/log4j/Log4jEnabledEventsTest.java
src/org/lttng/ust/agent/integration/log4j/Log4jLegacyApiTest.java
src/org/lttng/ust/agent/integration/log4j/Log4jMultiSessionTest.java [new file with mode: 0644]
src/org/lttng/ust/agent/utils/LttngSession.java

diff --git a/src/org/lttng/ust/agent/integration/common/MultiSessionTest.java b/src/org/lttng/ust/agent/integration/common/MultiSessionTest.java
new file mode 100644 (file)
index 0000000..9efcb63
--- /dev/null
@@ -0,0 +1,336 @@
+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());
+    }
+}
index aa5cd80aa9b4e9abfa1dd9d526a5689bc03bf969..0906e460b7bc3f8c045de4c22284456c2f7ee70c 100644 (file)
@@ -6,7 +6,8 @@ import org.junit.runners.Suite;
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
     JulEnabledEventsTest.class,
-    JulLegacyApiTest.class
+    JulLegacyApiTest.class,
+    JulMultiSessionTest.class
 })
 public class AllTests {
 
index cbb1413cf090df03301fc37a634938516756f517..0e546acad11990073bb121159b8a4b7be5db43ef 100644 (file)
@@ -31,6 +31,8 @@ public class JulEnabledEventsTest extends EnabledEventsTest {
         /* Skip tests if we can't find the JNI library or lttng-tools */
         assumeTrue(TestUtils.checkForJulLibrary());
         assumeTrue(TestUtils.checkForLttngTools(Domain.JUL));
+
+        LttngSession.destroyAllSessions();
     }
 
     @AfterClass
@@ -40,8 +42,6 @@ public class JulEnabledEventsTest extends EnabledEventsTest {
 
     @Before
     public void julSetup() 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);
index d66479e7acc507eeb9ae74849af329dc7c5080f0..88ec2681aeca3f31b13cc2388605d2d447a4e1a3 100644 (file)
@@ -40,6 +40,8 @@ public class JulLegacyApiTest {
         /* Skip tests if we can't find the JNI library or lttng-tools */
         assumeTrue(TestUtils.checkForJulLibrary());
         assumeTrue(TestUtils.checkForLttngTools(Domain.JUL));
+
+        LttngSession.destroyAllSessions();
     }
 
     @AfterClass
diff --git a/src/org/lttng/ust/agent/integration/jul/JulMultiSessionTest.java b/src/org/lttng/ust/agent/integration/jul/JulMultiSessionTest.java
new file mode 100644 (file)
index 0000000..667c354
--- /dev/null
@@ -0,0 +1,91 @@
+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);
+    }
+}
index 3f99b13d74a5fba99fed931da2eccc561505e0de..4ab5d22558025bf9146bbcb1a8c43f87c43b6b9c 100644 (file)
@@ -6,7 +6,8 @@ import org.junit.runners.Suite;
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
     Log4jEnabledEventsTest.class,
-    Log4jLegacyApiTest.class
+    Log4jLegacyApiTest.class,
+    Log4jMultiSessionTest.class
 })
 public class AllTests {
 
index a38d2e39f8e962bbf0b65123a4b2119990e4cf7b..771d94512db25cb59c61acb1f04221101eacd3dd 100644 (file)
@@ -27,21 +27,21 @@ public class Log4jEnabledEventsTest extends EnabledEventsTest {
     private Logger loggerD;
 
     @BeforeClass
-    public static void julClassSetup() {
+    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 julClassCleanup() {
+    public static void log4jClassCleanup() {
         LttngSession.deleteAllTracee();
     }
 
     @Before
-    public void julSetup() throws SecurityException, IOException {
-        // TODO Wipe all existing LTTng sessions?
-
+    public void log4jSetup() throws SecurityException, IOException {
         loggerA = Logger.getLogger(EVENT_NAME_A);
         loggerB = Logger.getLogger(EVENT_NAME_B);
         loggerC = Logger.getLogger(EVENT_NAME_C);
@@ -62,7 +62,7 @@ public class Log4jEnabledEventsTest extends EnabledEventsTest {
     }
 
     @After
-    public void julTeardown() {
+    public void log4jTeardown() {
         loggerA.removeAppender((Appender) handlerA);
         loggerB.removeAppender((Appender) handlerB);
         loggerC.removeAppender((Appender) handlerC);
index 1a240b037d4b8924eaf5f7f78de1172dcfd1bc07..aa847a43956c1447ab28cb4fc02ed61de6dd8a79 100644 (file)
@@ -40,6 +40,8 @@ public class Log4jLegacyApiTest {
         /* Skip tests if we can't find the JNI library or lttng-tools */
         assumeTrue(TestUtils.checkForLog4jLibrary());
         assumeTrue(TestUtils.checkForLttngTools(Domain.LOG4J));
+
+        LttngSession.destroyAllSessions();
     }
 
     @AfterClass
diff --git a/src/org/lttng/ust/agent/integration/log4j/Log4jMultiSessionTest.java b/src/org/lttng/ust/agent/integration/log4j/Log4jMultiSessionTest.java
new file mode 100644 (file)
index 0000000..4716ed6
--- /dev/null
@@ -0,0 +1,93 @@
+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);
+    }
+}
index d13e2fb53cbc5a268e6f95cbd9d5f3916f37df24..fa387fb2f3b67d724f14893e252e887460b5e933 100644 (file)
@@ -137,6 +137,14 @@ public class LttngSession implements AutoCloseable {
         return TestUtils.getOutputFromCommand(Arrays.asList("lttng", "view", sessionName));
     }
 
+    /**
+     * Utility method to destroy all existing sessions. Useful when first
+     * setting up a test to make sure no existing session interferes.
+     */
+    public static void destroyAllSessions() {
+        executeCommand(Arrays.asList("lttng", "destroy", "-a"));
+    }
+
     /**
      * Outside of the scope of lttng-tools, but this utility method can be used
      * to delete all traces currently under ~/lttng-traces/. This can be used by
This page took 0.030506 seconds and 4 git commands to generate.