| 1 | /* |
| 2 | * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | */ |
| 18 | |
| 19 | package org.lttng.ust.agent.integration; |
| 20 | |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertNotNull; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | |
| 25 | import java.util.List; |
| 26 | |
| 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | import org.lttng.tools.ILttngSession; |
| 32 | import org.lttng.tools.ILttngSession.Domain; |
| 33 | import org.lttng.ust.agent.ILttngHandler; |
| 34 | import org.lttng.ust.agent.utils.TestPrintRunner; |
| 35 | |
| 36 | /** |
| 37 | * Base abstract class to implement all sorts of integration tests verifying the |
| 38 | * presence of enabled events in resulting traces. |
| 39 | */ |
| 40 | @RunWith(TestPrintRunner.class) |
| 41 | public abstract class EnabledEventsTestBase { |
| 42 | |
| 43 | protected static final String EVENT_NAME_A = "EventA"; |
| 44 | protected static final String EVENT_NAME_B = "EventAB"; |
| 45 | protected static final String EVENT_NAME_C = "EventABC"; |
| 46 | protected static final String EVENT_NAME_D = "EventABCD"; |
| 47 | |
| 48 | private ILttngSession session; |
| 49 | |
| 50 | /* Fields defined by the sub-class */ |
| 51 | protected ILttngHandler handlerA; |
| 52 | protected ILttngHandler handlerB; |
| 53 | protected ILttngHandler handlerC; |
| 54 | |
| 55 | protected abstract Domain getDomain(); |
| 56 | |
| 57 | protected abstract void sendEventsToLoggers(); |
| 58 | |
| 59 | /** |
| 60 | * Base test setup |
| 61 | */ |
| 62 | @Before |
| 63 | public void testSetup() { |
| 64 | session = ILttngSession.createSession(null, getDomain()); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Base test teardown |
| 69 | */ |
| 70 | @After |
| 71 | public void testTeardown() { |
| 72 | session.close(); |
| 73 | |
| 74 | handlerA.close(); |
| 75 | handlerB.close(); |
| 76 | handlerC.close(); |
| 77 | |
| 78 | handlerA = null; |
| 79 | handlerB = null; |
| 80 | handlerC = null; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Test sending events on the Java side, but no events enabled in the |
| 85 | * tracing session. There should be nothing in the resulting trace, and |
| 86 | * handlers should not have logged anything. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testNoEvents() { |
| 90 | assertTrue(session.start()); |
| 91 | |
| 92 | sendEventsToLoggers(); |
| 93 | |
| 94 | assertTrue(session.stop()); |
| 95 | |
| 96 | List<String> output = session.view(); |
| 97 | assertNotNull(output); |
| 98 | assertTrue(output.isEmpty()); |
| 99 | |
| 100 | assertEquals(0, handlerA.getEventCount()); |
| 101 | assertEquals(0, handlerB.getEventCount()); |
| 102 | assertEquals(0, handlerC.getEventCount()); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Test sending events on the Java side, and all events enabled in the |
| 107 | * tracing session. All handlers should have sent their events. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testAllEvents() { |
| 111 | assertTrue(session.enableAllEvents()); |
| 112 | assertTrue(session.start()); |
| 113 | |
| 114 | sendEventsToLoggers(); |
| 115 | |
| 116 | assertTrue(session.stop()); |
| 117 | |
| 118 | List<String> output = session.view(); |
| 119 | assertNotNull(output); |
| 120 | assertEquals(30, output.size()); // loggerD has no handler attached |
| 121 | |
| 122 | assertEquals(10, handlerA.getEventCount()); |
| 123 | assertEquals(10, handlerB.getEventCount()); |
| 124 | assertEquals(10, handlerC.getEventCount()); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Test sending events on the Java side, with only some of them enabled in |
| 129 | * the tracing session. Only the subset that is enabled should be received. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testSomeEvents() { |
| 133 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
| 134 | assertTrue(session.start()); |
| 135 | |
| 136 | sendEventsToLoggers(); |
| 137 | |
| 138 | assertTrue(session.stop()); |
| 139 | |
| 140 | List<String> output = session.view(); |
| 141 | assertNotNull(output); |
| 142 | assertEquals(20, output.size()); |
| 143 | |
| 144 | assertEquals(10, handlerA.getEventCount()); |
| 145 | assertEquals(0, handlerB.getEventCount()); |
| 146 | assertEquals(10, handlerC.getEventCount()); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Test with all events enabled (-a), plus some other events added manually. |
| 151 | * Events should still be retained, but there should be no duplicates. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testAllEventsAndSome() { |
| 155 | assertTrue(session.enableAllEvents()); |
| 156 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B)); |
| 157 | assertTrue(session.start()); |
| 158 | |
| 159 | sendEventsToLoggers(); |
| 160 | |
| 161 | assertTrue(session.stop()); |
| 162 | |
| 163 | List<String> output = session.view(); |
| 164 | assertNotNull(output); |
| 165 | assertEquals(30, output.size()); |
| 166 | |
| 167 | assertEquals(10, handlerA.getEventCount()); |
| 168 | assertEquals(10, handlerB.getEventCount()); |
| 169 | assertEquals(10, handlerC.getEventCount()); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Same as {@link #testSomeEvents()}, but some events were enabled first, |
| 174 | * then disabled. Makes sure the enabled-event refcounting works properly. |
| 175 | */ |
| 176 | @Test |
| 177 | public void testSomeEventsAfterDisabling() { |
| 178 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
| 179 | assertTrue(session.disableEvents(EVENT_NAME_C)); |
| 180 | assertTrue(session.start()); |
| 181 | |
| 182 | sendEventsToLoggers(); |
| 183 | |
| 184 | assertTrue(session.stop()); |
| 185 | |
| 186 | List<String> output = session.view(); |
| 187 | assertNotNull(output); |
| 188 | assertEquals(10, output.size()); |
| 189 | |
| 190 | assertEquals(10, handlerA.getEventCount()); |
| 191 | assertEquals(0, handlerB.getEventCount()); |
| 192 | assertEquals(0, handlerC.getEventCount()); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Test enabling an event prefix, which means an event name ending with a *, |
| 197 | * to match all events starting with this name. |
| 198 | */ |
| 199 | @Test |
| 200 | public void testEventPrefix() { |
| 201 | // should match event/loggers B and C, but not A. |
| 202 | assertTrue(session.enableEvents("EventAB*")); |
| 203 | assertTrue(session.start()); |
| 204 | |
| 205 | sendEventsToLoggers(); |
| 206 | |
| 207 | assertTrue(session.stop()); |
| 208 | |
| 209 | List<String> output = session.view(); |
| 210 | assertNotNull(output); |
| 211 | assertEquals(20, output.size()); |
| 212 | |
| 213 | assertEquals(0, handlerA.getEventCount()); |
| 214 | assertEquals(10, handlerB.getEventCount()); |
| 215 | assertEquals(10, handlerC.getEventCount()); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Same as {@link #testEventPrefix()}, but with multiple prefixes that |
| 220 | * overlap. There should not be any duplicate events in the trace or in the |
| 221 | * handlers. |
| 222 | */ |
| 223 | @Test |
| 224 | public void testEventPrefixOverlapping() { |
| 225 | // should still match B and C |
| 226 | assertTrue(session.enableEvents("EventAB*", "EventABC*")); |
| 227 | assertTrue(session.start()); |
| 228 | |
| 229 | sendEventsToLoggers(); |
| 230 | |
| 231 | assertTrue(session.stop()); |
| 232 | |
| 233 | List<String> output = session.view(); |
| 234 | assertNotNull(output); |
| 235 | assertEquals(20, output.size()); |
| 236 | |
| 237 | assertEquals(0, handlerA.getEventCount()); |
| 238 | assertEquals(10, handlerB.getEventCount()); |
| 239 | assertEquals(10, handlerC.getEventCount()); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Test with all events enabled (-a), plus an event prefix. Once again, |
| 244 | * there should be no duplicates. |
| 245 | */ |
| 246 | @Test |
| 247 | public void testAllEventsAndPrefix() { |
| 248 | assertTrue(session.enableAllEvents()); |
| 249 | assertTrue(session.enableEvents("EventAB*")); |
| 250 | assertTrue(session.start()); |
| 251 | |
| 252 | sendEventsToLoggers(); |
| 253 | |
| 254 | assertTrue(session.stop()); |
| 255 | |
| 256 | List<String> output = session.view(); |
| 257 | assertNotNull(output); |
| 258 | assertEquals(30, output.size()); |
| 259 | |
| 260 | assertEquals(10, handlerA.getEventCount()); |
| 261 | assertEquals(10, handlerB.getEventCount()); |
| 262 | assertEquals(10, handlerC.getEventCount()); |
| 263 | } |
| 264 | } |