| 1 | package org.lttng.ust.agent.integration.common; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertNotNull; |
| 5 | import static org.junit.Assert.assertTrue; |
| 6 | |
| 7 | import java.util.List; |
| 8 | |
| 9 | import org.junit.After; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | import org.lttng.ust.agent.ILttngHandler; |
| 13 | import org.lttng.ust.agent.utils.LttngSession; |
| 14 | import org.lttng.ust.agent.utils.LttngSession.Domain; |
| 15 | |
| 16 | public abstract class EnabledEventsTest { |
| 17 | |
| 18 | protected static final String EVENT_NAME_A = "EventA"; |
| 19 | protected static final String EVENT_NAME_B = "EventAB"; |
| 20 | protected static final String EVENT_NAME_C = "EventABC"; |
| 21 | protected static final String EVENT_NAME_D = "EventABCD"; |
| 22 | |
| 23 | private LttngSession session; |
| 24 | |
| 25 | /* Fields defined by the sub-class */ |
| 26 | protected ILttngHandler handlerA; |
| 27 | protected ILttngHandler handlerB; |
| 28 | protected ILttngHandler handlerC; |
| 29 | |
| 30 | protected abstract Domain getDomain(); |
| 31 | |
| 32 | protected abstract void sendEventsToLoggers(); |
| 33 | |
| 34 | @Before |
| 35 | public void testSetup() { |
| 36 | session = new LttngSession(null, getDomain()); |
| 37 | } |
| 38 | |
| 39 | @After |
| 40 | public void testTeardown() { |
| 41 | session.close(); |
| 42 | |
| 43 | handlerA.close(); |
| 44 | handlerB.close(); |
| 45 | handlerC.close(); |
| 46 | |
| 47 | handlerA = null; |
| 48 | handlerB = null; |
| 49 | handlerC = null; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Test sending events on the Java side, but no events enabled in the |
| 54 | * tracing session. There should be nothing in the resulting trace, and |
| 55 | * handlers should not have logged anything. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testNoEvents() { |
| 59 | assertTrue(session.start()); |
| 60 | |
| 61 | sendEventsToLoggers(); |
| 62 | |
| 63 | assertTrue(session.stop()); |
| 64 | |
| 65 | List<String> output = session.view(); |
| 66 | assertNotNull(output); |
| 67 | assertTrue(output.isEmpty()); |
| 68 | |
| 69 | assertEquals(0, handlerA.getEventCount()); |
| 70 | assertEquals(0, handlerB.getEventCount()); |
| 71 | assertEquals(0, handlerC.getEventCount()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Test sending events on the Java side, and all events enabled in the |
| 76 | * tracing session. All handlers should have sent their events. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testAllEvents() { |
| 80 | assertTrue(session.enableAllEvents()); |
| 81 | assertTrue(session.start()); |
| 82 | |
| 83 | sendEventsToLoggers(); |
| 84 | |
| 85 | assertTrue(session.stop()); |
| 86 | |
| 87 | List<String> output = session.view(); |
| 88 | assertNotNull(output); |
| 89 | assertEquals(30, output.size()); // loggerD has no handler attached |
| 90 | |
| 91 | assertEquals(10, handlerA.getEventCount()); |
| 92 | assertEquals(10, handlerB.getEventCount()); |
| 93 | assertEquals(10, handlerC.getEventCount()); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Test sending events on the Java side, with only some of them enabled in |
| 98 | * the tracing session. Only the subset that is enabled should be received. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testSomeEvents() { |
| 102 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
| 103 | assertTrue(session.start()); |
| 104 | |
| 105 | sendEventsToLoggers(); |
| 106 | |
| 107 | assertTrue(session.stop()); |
| 108 | |
| 109 | List<String> output = session.view(); |
| 110 | assertNotNull(output); |
| 111 | assertEquals(20, output.size()); |
| 112 | |
| 113 | assertEquals(10, handlerA.getEventCount()); |
| 114 | assertEquals(0, handlerB.getEventCount()); |
| 115 | assertEquals(10, handlerC.getEventCount()); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Test with all events enabled (-a), plus some other events added manually. |
| 120 | * Events should still be retained, but there should be no duplicates. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testAllEventsAndSome() { |
| 124 | assertTrue(session.enableAllEvents()); |
| 125 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B)); |
| 126 | assertTrue(session.start()); |
| 127 | |
| 128 | sendEventsToLoggers(); |
| 129 | |
| 130 | assertTrue(session.stop()); |
| 131 | |
| 132 | List<String> output = session.view(); |
| 133 | assertNotNull(output); |
| 134 | assertEquals(30, output.size()); |
| 135 | |
| 136 | assertEquals(10, handlerA.getEventCount()); |
| 137 | assertEquals(10, handlerB.getEventCount()); |
| 138 | assertEquals(10, handlerC.getEventCount()); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Same as {@link #testSomeEvents()}, but some events were enabled first, |
| 143 | * then disabled. Makes sure the enabled-event refcounting works properly. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testSomeEventsAfterDisabling() { |
| 147 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
| 148 | assertTrue(session.disableEvents(EVENT_NAME_C)); |
| 149 | assertTrue(session.start()); |
| 150 | |
| 151 | sendEventsToLoggers(); |
| 152 | |
| 153 | assertTrue(session.stop()); |
| 154 | |
| 155 | List<String> output = session.view(); |
| 156 | assertNotNull(output); |
| 157 | assertEquals(10, output.size()); |
| 158 | |
| 159 | assertEquals(10, handlerA.getEventCount()); |
| 160 | assertEquals(0, handlerB.getEventCount()); |
| 161 | assertEquals(0, handlerC.getEventCount()); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Test enabling an event prefix, which means an event name ending with a *, |
| 166 | * to match all events starting with this name. |
| 167 | */ |
| 168 | @Test |
| 169 | public void testEventPrefix() { |
| 170 | // should match event/loggers B and C, but not A. |
| 171 | assertTrue(session.enableEvents("EventAB*")); |
| 172 | assertTrue(session.start()); |
| 173 | |
| 174 | sendEventsToLoggers(); |
| 175 | |
| 176 | assertTrue(session.stop()); |
| 177 | |
| 178 | List<String> output = session.view(); |
| 179 | assertNotNull(output); |
| 180 | assertEquals(20, output.size()); |
| 181 | |
| 182 | assertEquals(0, handlerA.getEventCount()); |
| 183 | assertEquals(10, handlerB.getEventCount()); |
| 184 | assertEquals(10, handlerC.getEventCount()); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Same as {@link #testEventPrefix()}, but with multiple prefixes that |
| 189 | * overlap. There should not be any duplicate events in the trace or in the |
| 190 | * handlers. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testEventPrefixOverlapping() { |
| 194 | // should still match B and C |
| 195 | assertTrue(session.enableEvents("EventAB*", "EventABC*")); |
| 196 | assertTrue(session.start()); |
| 197 | |
| 198 | sendEventsToLoggers(); |
| 199 | |
| 200 | assertTrue(session.stop()); |
| 201 | |
| 202 | List<String> output = session.view(); |
| 203 | assertNotNull(output); |
| 204 | assertEquals(20, output.size()); |
| 205 | |
| 206 | assertEquals(0, handlerA.getEventCount()); |
| 207 | assertEquals(10, handlerB.getEventCount()); |
| 208 | assertEquals(10, handlerC.getEventCount()); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Test with all events enabled (-a), plus an event prefix. Once again, |
| 213 | * there should be no duplicates. |
| 214 | */ |
| 215 | @Test |
| 216 | public void testAllEventsAndPrefix() { |
| 217 | assertTrue(session.enableAllEvents()); |
| 218 | assertTrue(session.enableEvents("EventAB*")); |
| 219 | assertTrue(session.start()); |
| 220 | |
| 221 | sendEventsToLoggers(); |
| 222 | |
| 223 | assertTrue(session.stop()); |
| 224 | |
| 225 | List<String> output = session.view(); |
| 226 | assertNotNull(output); |
| 227 | assertEquals(30, output.size()); |
| 228 | |
| 229 | assertEquals(10, handlerA.getEventCount()); |
| 230 | assertEquals(10, handlerB.getEventCount()); |
| 231 | assertEquals(10, handlerC.getEventCount()); |
| 232 | } |
| 233 | } |