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