| 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.events; |
| 20 | |
| 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | |
| 25 | import java.util.Arrays; |
| 26 | import java.util.List; |
| 27 | |
| 28 | import org.junit.jupiter.api.AfterEach; |
| 29 | import org.junit.jupiter.api.BeforeEach; |
| 30 | import org.junit.jupiter.api.Test; |
| 31 | import org.junit.jupiter.api.extension.ExtendWith; |
| 32 | import org.lttng.tools.ILttngSession; |
| 33 | import org.lttng.tools.ILttngSession.Domain; |
| 34 | import org.lttng.ust.agent.ILttngHandler; |
| 35 | import org.lttng.ust.agent.utils.TestPrintExtension; |
| 36 | |
| 37 | /** |
| 38 | * Base abstract class to implement all sorts of integration tests verifying the |
| 39 | * presence of enabled events in resulting traces. |
| 40 | */ |
| 41 | @ExtendWith(TestPrintExtension.class) |
| 42 | public abstract class EnabledEventsITBase { |
| 43 | |
| 44 | protected static final String EVENT_NAME_A = "EventA"; |
| 45 | protected static final String EVENT_NAME_B = "EventAB"; |
| 46 | protected static final String EVENT_NAME_C = "EventABC"; |
| 47 | protected static final String EVENT_NAME_D = "EventABCDÉ"; |
| 48 | |
| 49 | private ILttngSession session; |
| 50 | |
| 51 | /* Fields defined by the sub-class */ |
| 52 | protected ILttngHandler handlerA; |
| 53 | protected ILttngHandler handlerB; |
| 54 | protected ILttngHandler handlerC; |
| 55 | |
| 56 | protected abstract Domain getDomain(); |
| 57 | |
| 58 | protected abstract boolean closeHandlers(); |
| 59 | |
| 60 | protected abstract void sendEventsToLoggers(); |
| 61 | |
| 62 | /** |
| 63 | * Send one event using a localized API to logger/handler A. |
| 64 | */ |
| 65 | protected abstract void sendLocalizedEvent(String rawString, Object[] params); |
| 66 | |
| 67 | /** |
| 68 | * Base test setup |
| 69 | */ |
| 70 | @BeforeEach |
| 71 | public void testSetup() { |
| 72 | session = ILttngSession.createSession(null, getDomain()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Base test teardown |
| 77 | */ |
| 78 | @AfterEach |
| 79 | public void testTeardown() { |
| 80 | session.close(); |
| 81 | |
| 82 | if (closeHandlers()) { |
| 83 | handlerA.close(); |
| 84 | handlerB.close(); |
| 85 | handlerC.close(); |
| 86 | } |
| 87 | |
| 88 | handlerA = null; |
| 89 | handlerB = null; |
| 90 | handlerC = null; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Test sending events on the Java side, but no events enabled in the |
| 95 | * tracing session. There should be nothing in the resulting trace, and |
| 96 | * handlers should not have logged anything. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testNoEvents() { |
| 100 | assertTrue(session.start()); |
| 101 | |
| 102 | sendEventsToLoggers(); |
| 103 | |
| 104 | assertTrue(session.stop()); |
| 105 | |
| 106 | List<String> output = session.view(); |
| 107 | assertNotNull(output); |
| 108 | assertTrue(output.isEmpty()); |
| 109 | |
| 110 | assertEquals(0, handlerA.getEventCount()); |
| 111 | assertEquals(0, handlerB.getEventCount()); |
| 112 | assertEquals(0, handlerC.getEventCount()); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Test sending events on the Java side, and all events enabled in the |
| 117 | * tracing session. All handlers should have sent their events. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testAllEvents() { |
| 121 | assertTrue(session.enableAllEvents()); |
| 122 | assertTrue(session.start()); |
| 123 | |
| 124 | sendEventsToLoggers(); |
| 125 | |
| 126 | assertTrue(session.stop()); |
| 127 | |
| 128 | List<String> output = session.view(); |
| 129 | assertNotNull(output); |
| 130 | assertEquals(30, output.size()); // loggerD has no handler attached |
| 131 | |
| 132 | assertEquals(10, handlerA.getEventCount()); |
| 133 | assertEquals(10, handlerB.getEventCount()); |
| 134 | assertEquals(10, handlerC.getEventCount()); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Test sending events on the Java side, with only some of them enabled in |
| 139 | * the tracing session. Only the subset that is enabled should be received. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testSomeEvents() { |
| 143 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
| 144 | assertTrue(session.start()); |
| 145 | |
| 146 | sendEventsToLoggers(); |
| 147 | |
| 148 | assertTrue(session.stop()); |
| 149 | |
| 150 | List<String> output = session.view(); |
| 151 | assertNotNull(output); |
| 152 | assertEquals(20, output.size()); |
| 153 | |
| 154 | assertEquals(10, handlerA.getEventCount()); |
| 155 | assertEquals(0, handlerB.getEventCount()); |
| 156 | assertEquals(10, handlerC.getEventCount()); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Test with all events enabled (-a), plus some other events added manually. |
| 161 | * Events should still be retained, but there should be no duplicates. |
| 162 | */ |
| 163 | @Test |
| 164 | public void testAllEventsAndSome() { |
| 165 | assertTrue(session.enableAllEvents()); |
| 166 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B)); |
| 167 | assertTrue(session.start()); |
| 168 | |
| 169 | sendEventsToLoggers(); |
| 170 | |
| 171 | assertTrue(session.stop()); |
| 172 | |
| 173 | List<String> output = session.view(); |
| 174 | assertNotNull(output); |
| 175 | assertEquals(30, output.size()); |
| 176 | |
| 177 | assertEquals(10, handlerA.getEventCount()); |
| 178 | assertEquals(10, handlerB.getEventCount()); |
| 179 | assertEquals(10, handlerC.getEventCount()); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Same as {@link #testSomeEvents()}, but some events were enabled first, |
| 184 | * then disabled. Makes sure the enabled-event refcounting works properly. |
| 185 | */ |
| 186 | @Test |
| 187 | public void testSomeEventsAfterDisabling() { |
| 188 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
| 189 | assertTrue(session.disableEvents(EVENT_NAME_C)); |
| 190 | assertTrue(session.start()); |
| 191 | |
| 192 | sendEventsToLoggers(); |
| 193 | |
| 194 | assertTrue(session.stop()); |
| 195 | |
| 196 | List<String> output = session.view(); |
| 197 | assertNotNull(output); |
| 198 | assertEquals(10, output.size()); |
| 199 | |
| 200 | assertEquals(10, handlerA.getEventCount()); |
| 201 | assertEquals(0, handlerB.getEventCount()); |
| 202 | assertEquals(0, handlerC.getEventCount()); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Test enabling an event prefix, which means an event name ending with a *, |
| 207 | * to match all events starting with this name. |
| 208 | */ |
| 209 | @Test |
| 210 | public void testEventPrefix() { |
| 211 | // should match event/loggers B and C, but not A. |
| 212 | assertTrue(session.enableEvents("EventAB*")); |
| 213 | assertTrue(session.start()); |
| 214 | |
| 215 | sendEventsToLoggers(); |
| 216 | |
| 217 | assertTrue(session.stop()); |
| 218 | |
| 219 | List<String> output = session.view(); |
| 220 | assertNotNull(output); |
| 221 | assertEquals(20, output.size()); |
| 222 | |
| 223 | assertEquals(0, handlerA.getEventCount()); |
| 224 | assertEquals(10, handlerB.getEventCount()); |
| 225 | assertEquals(10, handlerC.getEventCount()); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Same as {@link #testEventPrefix()}, but with multiple prefixes that |
| 230 | * overlap. There should not be any duplicate events in the trace or in the |
| 231 | * handlers. |
| 232 | */ |
| 233 | @Test |
| 234 | public void testEventPrefixOverlapping() { |
| 235 | // should still match B and C |
| 236 | assertTrue(session.enableEvents("EventAB*", "EventABC*")); |
| 237 | assertTrue(session.start()); |
| 238 | |
| 239 | sendEventsToLoggers(); |
| 240 | |
| 241 | assertTrue(session.stop()); |
| 242 | |
| 243 | List<String> output = session.view(); |
| 244 | assertNotNull(output); |
| 245 | assertEquals(20, output.size()); |
| 246 | |
| 247 | assertEquals(0, handlerA.getEventCount()); |
| 248 | assertEquals(10, handlerB.getEventCount()); |
| 249 | assertEquals(10, handlerC.getEventCount()); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Test with all events enabled (-a), plus an event prefix. Once again, |
| 254 | * there should be no duplicates. |
| 255 | */ |
| 256 | @Test |
| 257 | public void testAllEventsAndPrefix() { |
| 258 | assertTrue(session.enableAllEvents()); |
| 259 | assertTrue(session.enableEvents("EventAB*")); |
| 260 | assertTrue(session.start()); |
| 261 | |
| 262 | sendEventsToLoggers(); |
| 263 | |
| 264 | assertTrue(session.stop()); |
| 265 | |
| 266 | List<String> output = session.view(); |
| 267 | assertNotNull(output); |
| 268 | assertEquals(30, output.size()); |
| 269 | |
| 270 | assertEquals(10, handlerA.getEventCount()); |
| 271 | assertEquals(10, handlerB.getEventCount()); |
| 272 | assertEquals(10, handlerC.getEventCount()); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Test sending a localized message. |
| 277 | */ |
| 278 | @Test |
| 279 | public void testLocalizedMessage() { |
| 280 | Integer value1 = Integer.valueOf(10); |
| 281 | List<Integer> value2 = Arrays.asList(Integer.valueOf(1000), Integer.valueOf(1001), Integer.valueOf(1002)); |
| 282 | |
| 283 | assertTrue(session.enableAllEvents()); |
| 284 | assertTrue(session.start()); |
| 285 | |
| 286 | sendLocalizedEvent("Message with a localized value: {0} and some others: {1}", |
| 287 | new Object[] { value1, value2 }); |
| 288 | |
| 289 | assertTrue(session.stop()); |
| 290 | assertEquals(1, handlerA.getEventCount()); |
| 291 | |
| 292 | List<String> output = session.view(); |
| 293 | |
| 294 | assertNotNull(output); |
| 295 | assertEquals(1, output.size()); |
| 296 | assertTrue(output.get(0).contains("msg = \"Message with a localized value: 10 and some others: [1000, 1001, 1002]\"")); |
| 297 | } |
| 298 | } |