| 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.client; |
| 20 | |
| 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | import static org.junit.jupiter.api.Assertions.fail; |
| 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Arrays; |
| 27 | import java.util.Collections; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import org.junit.jupiter.api.Test; |
| 31 | import org.junit.jupiter.api.extension.ExtendWith; |
| 32 | import org.junit.jupiter.api.AfterAll; |
| 33 | import org.junit.jupiter.api.AfterEach; |
| 34 | import org.junit.jupiter.api.BeforeAll; |
| 35 | import org.junit.jupiter.api.BeforeEach; |
| 36 | import org.lttng.tools.ILttngSession; |
| 37 | import org.lttng.tools.LttngToolsHelper; |
| 38 | import org.lttng.ust.agent.ILttngAgent; |
| 39 | import org.lttng.ust.agent.client.LttngTcpSessiondClient; |
| 40 | import org.lttng.ust.agent.session.EventRule; |
| 41 | import org.lttng.ust.agent.session.LogLevelSelector; |
| 42 | import org.lttng.ust.agent.session.LogLevelSelector.LogLevelType; |
| 43 | import org.lttng.ust.agent.utils.EventRuleFactory; |
| 44 | import org.lttng.ust.agent.utils.ILogLevelStrings; |
| 45 | import org.lttng.ust.agent.utils.TestPrintExtension; |
| 46 | |
| 47 | /** |
| 48 | * Tests for the TCP client only, without using an agent. |
| 49 | * |
| 50 | * This test suite requires that a *root* session daemon is running on the |
| 51 | * system. Since we have to explicitly tell the TCP client which sessiond to |
| 52 | * connect to, we have to hard-code it in here. |
| 53 | * |
| 54 | * @author Alexandre Montplaisir |
| 55 | */ |
| 56 | @ExtendWith(TestPrintExtension.class) |
| 57 | public class TcpClientIT { |
| 58 | |
| 59 | // ------------------------------------------------------------------------ |
| 60 | // Attributes |
| 61 | // ------------------------------------------------------------------------ |
| 62 | |
| 63 | private static final String EVENT_NAME_A = "eventA"; |
| 64 | private static final String EVENT_NAME_B = "eventB"; |
| 65 | private static final String EVENT_NAME_C = "eventC"; |
| 66 | |
| 67 | private static final String CONTEXT_RETRIEVER_NAME_A = "retrieverA"; |
| 68 | private static final String CONTEXT_RETRIEVER_NAME_B = "retrieverB"; |
| 69 | private static final String CONTEXT_NAME_A = "contextA"; |
| 70 | private static final String CONTEXT_NAME_B = "contextB"; |
| 71 | |
| 72 | /* Test configuration */ |
| 73 | private static final int DOMAIN_VALUE = ILttngAgent.Domain.JUL.value(); |
| 74 | private static final ILttngSession.Domain SESSION_DOMAIN = ILttngSession.Domain.JUL; |
| 75 | |
| 76 | private static TcpClientDebugListener clientListener; |
| 77 | private static LttngTcpSessiondClient client; |
| 78 | private static Thread clientThread; |
| 79 | |
| 80 | private static EventRuleFactory eventRuleFactory = new EventRuleFactory(SESSION_DOMAIN); |
| 81 | |
| 82 | private ILttngSession session; |
| 83 | |
| 84 | // ------------------------------------------------------------------------ |
| 85 | // Maintenance |
| 86 | // ------------------------------------------------------------------------ |
| 87 | |
| 88 | /** |
| 89 | * Class setup |
| 90 | */ |
| 91 | @BeforeAll |
| 92 | public static void setupClass() { |
| 93 | LttngToolsHelper.destroyAllSessions(); |
| 94 | |
| 95 | clientListener = new TcpClientDebugListener(); |
| 96 | |
| 97 | /* Try connecting to a root sessiond first */ |
| 98 | client = new LttngTcpSessiondClient(clientListener, DOMAIN_VALUE, true); |
| 99 | clientThread = new Thread(client); |
| 100 | clientThread.start(); |
| 101 | |
| 102 | if (client.waitForConnection(5)) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | /* Connection was not established, try a user sessiond instead */ |
| 107 | client.close(); |
| 108 | try { |
| 109 | clientThread.join(); |
| 110 | } catch (InterruptedException e) { |
| 111 | fail(e.getMessage()); |
| 112 | } |
| 113 | |
| 114 | client = new LttngTcpSessiondClient(clientListener, DOMAIN_VALUE, false); |
| 115 | clientThread = new Thread(client); |
| 116 | clientThread.start(); |
| 117 | |
| 118 | assertTrue(client.waitForConnection(5), "Timed out waiting for a sessiond"); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Class teardown |
| 123 | */ |
| 124 | @AfterAll |
| 125 | public static void teardownClass() { |
| 126 | if (client != null) { |
| 127 | client.close(); |
| 128 | } |
| 129 | if (clientThread != null) { |
| 130 | try { |
| 131 | clientThread.join(); |
| 132 | } catch (InterruptedException e) { |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Test setup |
| 139 | */ |
| 140 | @BeforeEach |
| 141 | public void setup() { |
| 142 | session = ILttngSession.createSession(null, SESSION_DOMAIN); |
| 143 | clientListener.clearAllCommands(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Test teardown |
| 148 | */ |
| 149 | @AfterEach |
| 150 | public void teardown() { |
| 151 | session.close(); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | private static ILogLevelStrings getLogLevelStrings() { |
| 156 | return ILogLevelStrings.JUL_LOGLEVEL_STRINGS; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Check that two lists contain the exact same element (including |
| 161 | * duplicates), but their order does not matter. |
| 162 | */ |
| 163 | private static <T extends Comparable<T>> boolean containSameElements(List<T> list1, List<T> list2) { |
| 164 | List<T> newlist1 = new ArrayList<>(list1); |
| 165 | List<T> newlist2 = new ArrayList<>(list2); |
| 166 | Collections.sort(newlist1); |
| 167 | Collections.sort(newlist2); |
| 168 | return (newlist1.equals(newlist2)); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | // ------------------------------------------------------------------------ |
| 173 | // Event enabling/disabling test cases |
| 174 | // ------------------------------------------------------------------------ |
| 175 | |
| 176 | /** |
| 177 | * Test enabling one event. |
| 178 | */ |
| 179 | @Test |
| 180 | public void testEnableEvent() { |
| 181 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 182 | |
| 183 | List<EventRule> expectedCommands = Collections.singletonList( |
| 184 | eventRuleFactory.createRule(EVENT_NAME_A)); |
| 185 | |
| 186 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 187 | assertEquals(expectedCommands, actualCommands); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Test an "enable-event -a" command. |
| 192 | */ |
| 193 | @Test |
| 194 | public void testEnableAllEvents() { |
| 195 | session.enableAllEvents(); |
| 196 | |
| 197 | List<EventRule> expectedCommands = Collections.singletonList( |
| 198 | eventRuleFactory.createRuleAllEvents()); |
| 199 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 200 | |
| 201 | assertEquals(expectedCommands, actualCommands); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Test enabling then disabling one event. |
| 206 | */ |
| 207 | @Test |
| 208 | public void testEnableThenDisableOneEvent() { |
| 209 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 210 | session.disableEvents(EVENT_NAME_A); |
| 211 | |
| 212 | List<EventRule> expectedEnableCommands = Collections.singletonList( |
| 213 | eventRuleFactory.createRule(EVENT_NAME_A)); |
| 214 | List<String> expectedDisableCommands = Collections.singletonList(EVENT_NAME_A); |
| 215 | |
| 216 | assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); |
| 217 | assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands())); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Test enabling some events manually, then disabling all events (-a). |
| 222 | */ |
| 223 | @Test |
| 224 | public void testEnableSomeThenDisableAll() { |
| 225 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 226 | session.enableEvent(EVENT_NAME_B, null, false, null); |
| 227 | session.enableEvent(EVENT_NAME_C, null, false, null); |
| 228 | session.disableAllEvents(); |
| 229 | |
| 230 | List<EventRule> expectedEnableCommands = Arrays.asList( |
| 231 | eventRuleFactory.createRule(EVENT_NAME_A), |
| 232 | eventRuleFactory.createRule(EVENT_NAME_B), |
| 233 | eventRuleFactory.createRule(EVENT_NAME_C)); |
| 234 | /* |
| 235 | * A "disable-event -a" will send one command for each enabled event. |
| 236 | * The order may be different though. |
| 237 | */ |
| 238 | List<String> expectedDisableCommands = Arrays.asList( |
| 239 | EVENT_NAME_A, EVENT_NAME_B, EVENT_NAME_C); |
| 240 | |
| 241 | assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); |
| 242 | assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands())); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Test enabling then (enable-event -a) then disabling all (disable-event -a) events. |
| 247 | */ |
| 248 | @Test |
| 249 | public void testEnableAllThenDisableAll() { |
| 250 | session.enableAllEvents(); |
| 251 | session.disableAllEvents(); |
| 252 | |
| 253 | List<EventRule> expectedEnableCommands = Arrays.asList(eventRuleFactory.createRuleAllEvents()); |
| 254 | List<String> expectedDisableCommands = Arrays.asList(EventRuleFactory.EVENT_NAME_ALL); |
| 255 | |
| 256 | assertEquals(expectedEnableCommands, clientListener.getEnabledEventCommands()); |
| 257 | assertTrue(containSameElements(expectedDisableCommands, clientListener.getDisabledEventCommands())); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Test enabling then destroying the session (should send corresponding |
| 262 | * disable event messages). |
| 263 | */ |
| 264 | @SuppressWarnings("static-method") |
| 265 | @Test |
| 266 | public void testEnableEventThenDestroy() { |
| 267 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 268 | session2.enableEvent(EVENT_NAME_A, null, false, null); |
| 269 | session2.enableEvent(EVENT_NAME_B, null, false, null); |
| 270 | } // close(), aka destroy the session, sending "disable event" messages |
| 271 | |
| 272 | List<EventRule> expectedEnabledCommands = Arrays.asList(eventRuleFactory.createRule(EVENT_NAME_A), eventRuleFactory.createRule(EVENT_NAME_B)); |
| 273 | List<String> expectedDisabledCommands = Arrays.asList(EVENT_NAME_A, EVENT_NAME_B); |
| 274 | |
| 275 | assertEquals(expectedEnabledCommands, clientListener.getEnabledEventCommands()); |
| 276 | assertTrue(clientListener.getDisabledEventCommands().containsAll(expectedDisabledCommands)); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Test specifying an event with a --loglevel option. |
| 281 | */ |
| 282 | @Test |
| 283 | public void testEnableEventLogLevelRange() { |
| 284 | LogLevelSelector lls = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 285 | |
| 286 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 287 | |
| 288 | List<EventRule> expectedCommands = Collections.singletonList( |
| 289 | eventRuleFactory.createRule(EVENT_NAME_A, lls)); |
| 290 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 291 | |
| 292 | assertEquals(expectedCommands, actualCommands); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Test enabling an event with a --loglevel-only option. |
| 297 | */ |
| 298 | @Test |
| 299 | public void testEnableEventLogLevelSingle() { |
| 300 | LogLevelSelector lls = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 301 | |
| 302 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 303 | |
| 304 | List<EventRule> expectedCommands = Collections.singletonList( |
| 305 | eventRuleFactory.createRule(EVENT_NAME_A, lls)); |
| 306 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 307 | |
| 308 | assertEquals(expectedCommands, actualCommands); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Test enabling an event twice, for the same loglevel, with --loglevel followed by --loglevel-only. |
| 313 | */ |
| 314 | @Test |
| 315 | public void testEnableEventsLogLevelRangeAndSingle() { |
| 316 | LogLevelSelector lls1 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 317 | LogLevelSelector lls2 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 318 | |
| 319 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 320 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 321 | |
| 322 | List<EventRule> expectedCommands = Arrays.asList( |
| 323 | eventRuleFactory.createRule(EVENT_NAME_A, lls1), |
| 324 | eventRuleFactory.createRule(EVENT_NAME_A, lls2) |
| 325 | ); |
| 326 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 327 | |
| 328 | assertEquals(expectedCommands, actualCommands); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Test enabling an event twice, for the same loglevel, with --loglevel-only followed by --loglevel. |
| 333 | */ |
| 334 | @Test |
| 335 | public void testEnableEventsLogLevelSingleAndRange() { |
| 336 | LogLevelSelector lls1 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 337 | LogLevelSelector lls2 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 338 | |
| 339 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 340 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 341 | |
| 342 | List<EventRule> expectedCommands = Arrays.asList( |
| 343 | eventRuleFactory.createRule(EVENT_NAME_A, lls1), |
| 344 | eventRuleFactory.createRule(EVENT_NAME_A, lls2) |
| 345 | ); |
| 346 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 347 | |
| 348 | assertEquals(expectedCommands, actualCommands); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Test enabling the same event, same loglevel, but different loglevel types |
| 353 | * (--loglevel vs --loglevel-only) in two separate sessions. |
| 354 | */ |
| 355 | @Test |
| 356 | public void testEnableEventsLogLevelRangeAndSingleDiffSessions() { |
| 357 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 358 | |
| 359 | LogLevelSelector lls1 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 360 | LogLevelSelector lls2 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); |
| 361 | |
| 362 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 363 | session2.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); |
| 364 | |
| 365 | List<EventRule> expectedCommands = Arrays.asList( |
| 366 | eventRuleFactory.createRule(EVENT_NAME_A, lls1), |
| 367 | eventRuleFactory.createRule(EVENT_NAME_A, lls2)); |
| 368 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 369 | |
| 370 | assertEquals(expectedCommands, actualCommands); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Enable the same event multiple times with different filter strings. |
| 376 | */ |
| 377 | @Test |
| 378 | public void testEnableEventsDiffFilters() { |
| 379 | final String filter1 = "filter1"; |
| 380 | final String filter2 = "filter2"; |
| 381 | |
| 382 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 383 | session.enableEvent(EVENT_NAME_A, null, false, filter1); |
| 384 | session.enableEvent(EVENT_NAME_A, null, false, filter2); |
| 385 | |
| 386 | List<EventRule> expectedCommands = Arrays.asList( |
| 387 | eventRuleFactory.createRule(EVENT_NAME_A), |
| 388 | eventRuleFactory.createRule(EVENT_NAME_A, eventRuleFactory.LOG_LEVEL_UNSPECIFIED, filter1), |
| 389 | eventRuleFactory.createRule(EVENT_NAME_A, eventRuleFactory.LOG_LEVEL_UNSPECIFIED, filter2)); |
| 390 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 391 | |
| 392 | assertEquals(expectedCommands, actualCommands); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Enable the same event multiple times with different log levels and |
| 397 | * filters. |
| 398 | */ |
| 399 | @Test |
| 400 | public void testEnableEventsLogLevelAndFilters() { |
| 401 | final LogLevelSelector lls = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
| 402 | final String filter = "filter1"; |
| 403 | |
| 404 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 405 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
| 406 | session.enableEvent(EVENT_NAME_A, null, false, filter); |
| 407 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, filter); |
| 408 | |
| 409 | List<EventRule> expectedCommands = Arrays.asList( |
| 410 | eventRuleFactory.createRule(EVENT_NAME_A), |
| 411 | eventRuleFactory.createRule(EVENT_NAME_A, lls), |
| 412 | eventRuleFactory.createRule(EVENT_NAME_A, eventRuleFactory.LOG_LEVEL_UNSPECIFIED, filter), |
| 413 | eventRuleFactory.createRule(EVENT_NAME_A, lls, filter)); |
| 414 | List<EventRule> actualCommands = clientListener.getEnabledEventCommands(); |
| 415 | |
| 416 | assertEquals(expectedCommands, actualCommands); |
| 417 | } |
| 418 | |
| 419 | // ------------------------------------------------------------------------ |
| 420 | // Application context enabling/disabling test cases |
| 421 | // ------------------------------------------------------------------------ |
| 422 | |
| 423 | /** |
| 424 | * Test enabling one application context. |
| 425 | */ |
| 426 | @Test |
| 427 | public void testEnableAppContext() { |
| 428 | session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 429 | |
| 430 | List<String> expectedCommands = Collections.singletonList( |
| 431 | CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 432 | |
| 433 | List<String> actualCommands = clientListener.getEnabledAppContextCommands(); |
| 434 | assertEquals(expectedCommands, actualCommands); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Test enabling two application contexts sharing the same retriever name. |
| 439 | */ |
| 440 | @Test |
| 441 | public void testEnableAppContextsSameRetriever() { |
| 442 | session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 443 | session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_B); |
| 444 | |
| 445 | List<String> expectedCommands = Arrays.asList( |
| 446 | CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A, |
| 447 | CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_B); |
| 448 | |
| 449 | List<String> actualCommands = clientListener.getEnabledAppContextCommands(); |
| 450 | assertEquals(expectedCommands, actualCommands); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Test enabling two application contexts sharing the same context name, but |
| 455 | * with different retrievers. Unusual, but they should still be recognized |
| 456 | * separately. |
| 457 | */ |
| 458 | @Test |
| 459 | public void testEnableAppContextsSameContext() { |
| 460 | session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 461 | session.enableAppContext(CONTEXT_RETRIEVER_NAME_B, CONTEXT_NAME_A); |
| 462 | |
| 463 | List<String> expectedCommands = Arrays.asList( |
| 464 | CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A, |
| 465 | CONTEXT_RETRIEVER_NAME_B + ':' + CONTEXT_NAME_A); |
| 466 | |
| 467 | List<String> actualCommands = clientListener.getEnabledAppContextCommands(); |
| 468 | assertEquals(expectedCommands, actualCommands); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Test enabling one application context, then destroying the session. We |
| 473 | * should receive the corresponding "context removed" message. |
| 474 | */ |
| 475 | @Test |
| 476 | @SuppressWarnings("static-method") |
| 477 | public void testEnableAppContextThenDestroy() { |
| 478 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 479 | session2.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 480 | } // close(), aka destroy the session, sending "disable context" messages |
| 481 | |
| 482 | List<String> expectedEnabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 483 | List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 484 | List<String> actualEnabledCommands = clientListener.getEnabledAppContextCommands(); |
| 485 | List<String> actualDisabledCommands = clientListener.getDisabledAppContextCommands(); |
| 486 | |
| 487 | assertEquals(expectedEnabledCommands, actualEnabledCommands); |
| 488 | assertEquals(expectedDisabledCommands, actualDisabledCommands); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Test enabling the same application context in two different sessions. |
| 493 | * Upon destroying one, we should only receive one "destroyed" message. |
| 494 | */ |
| 495 | @Test |
| 496 | public void testEnableSameAppContextTwoSessions() { |
| 497 | List<String> expectedEnabledCommands = Arrays.asList( |
| 498 | CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A, |
| 499 | CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 500 | List<String> actualEnabledCommands; |
| 501 | |
| 502 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 503 | session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 504 | session2.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 505 | |
| 506 | actualEnabledCommands = clientListener.getEnabledAppContextCommands(); |
| 507 | assertEquals(expectedEnabledCommands, actualEnabledCommands); |
| 508 | } // close/destroy session2 |
| 509 | |
| 510 | actualEnabledCommands = clientListener.getEnabledAppContextCommands(); |
| 511 | assertEquals(expectedEnabledCommands, actualEnabledCommands); |
| 512 | |
| 513 | List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 514 | List<String> actualDisabledCommands = clientListener.getDisabledAppContextCommands(); |
| 515 | |
| 516 | assertEquals(expectedDisabledCommands, actualDisabledCommands); |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Test enabling two different application context in two different |
| 521 | * sessions. Upon destroying one, we should receive the correct "destroyed" |
| 522 | * message. |
| 523 | */ |
| 524 | @Test |
| 525 | public void testEnableDiffAppContextTwoSessions() { |
| 526 | List<String> expectedEnabledCommands = Arrays.asList( |
| 527 | CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A, |
| 528 | CONTEXT_RETRIEVER_NAME_B + ':' + CONTEXT_NAME_B); |
| 529 | List<String> actualEnabledCommands; |
| 530 | |
| 531 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 532 | session.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 533 | session2.enableAppContext(CONTEXT_RETRIEVER_NAME_B, CONTEXT_NAME_B); |
| 534 | |
| 535 | actualEnabledCommands = clientListener.getEnabledAppContextCommands(); |
| 536 | assertEquals(expectedEnabledCommands, actualEnabledCommands); |
| 537 | } // close/destroy session2 |
| 538 | |
| 539 | actualEnabledCommands = clientListener.getEnabledAppContextCommands(); |
| 540 | assertEquals(expectedEnabledCommands, actualEnabledCommands); |
| 541 | |
| 542 | List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_B + ':' + CONTEXT_NAME_B); |
| 543 | List<String> actualDisabledCommands = clientListener.getDisabledAppContextCommands(); |
| 544 | |
| 545 | assertEquals(expectedDisabledCommands, actualDisabledCommands); |
| 546 | } |
| 547 | |
| 548 | // ------------------------------------------------------------------------ |
| 549 | // Application context filtering |
| 550 | // ------------------------------------------------------------------------ |
| 551 | |
| 552 | /** |
| 553 | * Test that enabling an event with a filter string referring to a context |
| 554 | * should send an agent message about this context now being "enabled". |
| 555 | * |
| 556 | * This is because we will pass the context information to UST for the |
| 557 | * filtering step, even if the actual context won't be present in the trace. |
| 558 | */ |
| 559 | @SuppressWarnings("static-method") |
| 560 | @Test |
| 561 | public void testContextInFilterString() { |
| 562 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 563 | session2.enableEvent(EVENT_NAME_A, null, false, "$app." + CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A + "==\"bozo\""); |
| 564 | |
| 565 | List<String> expectedEnabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 566 | assertEquals(expectedEnabledCommands, clientListener.getEnabledAppContextCommands()); |
| 567 | } // close(), aka destroy the session, sending "disable context" messages |
| 568 | |
| 569 | List<String> expectedDisabledCommands = Collections.singletonList(CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 570 | assertEquals(expectedDisabledCommands, clientListener.getDisabledAppContextCommands()); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Test that if we the context is both referred to by a filter string *and* |
| 575 | * enabled directly, we receive *2* messages about this context being |
| 576 | * enabled (and disabled on session teardown). |
| 577 | */ |
| 578 | @SuppressWarnings("static-method") |
| 579 | @Test |
| 580 | public void testContextEnabledAndInFilterString() { |
| 581 | try (ILttngSession session2 = ILttngSession.createSession(null, SESSION_DOMAIN);) { |
| 582 | session2.enableEvent(EVENT_NAME_A, null, false, "$app." + CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A + "==\"bozo\""); |
| 583 | session2.enableAppContext(CONTEXT_RETRIEVER_NAME_A, CONTEXT_NAME_A); |
| 584 | |
| 585 | List<String> expectedEnabledCommands = Collections.nCopies(2, CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 586 | assertEquals(expectedEnabledCommands, clientListener.getEnabledAppContextCommands()); |
| 587 | } // close(), aka destroy the session, sending "disable context" messages |
| 588 | |
| 589 | List<String> expectedDisabledCommands = Collections.nCopies(2, CONTEXT_RETRIEVER_NAME_A + ':' + CONTEXT_NAME_A); |
| 590 | assertEquals(expectedDisabledCommands, clientListener.getDisabledAppContextCommands()); |
| 591 | } |
| 592 | } |