2 * Copyright (C) 2016, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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.
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.
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.
19 package org.lttng.ust.agent.integration.filter;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import java.util.Collections;
25 import java.util.stream.Collectors;
26 import java.util.stream.Stream;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.junit.jupiter.api.AfterEach;
31 import org.lttng.tools.ILttngSession;
32 import org.lttng.ust.agent.filter.FilterChangeNotifier;
33 import org.lttng.ust.agent.integration.filter.FilterListenerITBase.TestFilterListener;
34 import org.lttng.ust.agent.session.EventRule;
35 import org.lttng.ust.agent.utils.EventRuleFactory;
36 import org.lttng.ust.agent.utils.TestPrintExtension;
39 * For the filter change notifications to work, several setup steps are
43 * <li>Initialize the Java agent register it to the sessiond [Agent]</li>
44 * <li>Instantiate a filer change listener, and register it to the notifier
46 * <li>Apply some event rule changes in the tracing session (lttng enable-event,
47 * etc.) [Session]</li>
51 * Then on teardown, the following steps are expected:
55 * <li>Dispose the Java agent, closing the connection to the sessiond [Agent]
57 * <li>Destroy the tracing session, removing tracked events [Session]</li>
60 * (and then the filter change listener should be de-registered from the
61 * notifier. If it is deregistered earlier, then obviously no notifications
62 * would be received thereafter).
65 * Within these two sets, each step can happen in any order. This results in 6 x
66 * 2 = 12 possibilities. The goal of this test class it to test these 12
70 @ExtendWith(TestPrintExtension.class)
71 @SuppressWarnings("javadoc")
72 public abstract class FilterListenerOrderingITBase {
74 protected static final String EVENT_NAME_A = "EventA";
75 private static final String EVENT_NAME_B = "EventB";
77 private ILttngSession session;
78 private TestFilterListener listener;
84 public void baseTeardown() {
86 * Deregister the listener (should always be done after all the other
89 FilterChangeNotifier.getInstance().unregisterListener(listener);
93 // ------------------------------------------------------------------------
95 // ------------------------------------------------------------------------
97 protected abstract ILttngSession.Domain getDomain();
99 protected abstract void registerAgent();
101 private void registerListener() {
102 listener = new TestFilterListener();
103 FilterChangeNotifier.getInstance().registerListener(listener);
106 private void enableRulesInSession() {
107 session = ILttngSession.createCommandLineSession(null, getDomain());
108 session.enableEvent(EVENT_NAME_A, null, false, null);
109 session.enableEvent(EVENT_NAME_B, null, false, null);
112 protected abstract void deregisterAgent();
114 private void destroySession() {
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
124 * Check that the expected event rules are present after setup but before
127 private void checkOngoingConditions() {
128 Set<EventRule> exptectedRules = Stream.of(
129 EventRuleFactory.createRule(EVENT_NAME_A),
130 EventRuleFactory.createRule(EVENT_NAME_B))
131 .collect(Collectors.toSet());
133 assertEquals(2, listener.getNbNotifications());
134 assertEquals(exptectedRules, listener.getCurrentRules());
138 * Check that the expected event rules are present after/during teardown.
140 private void checkFinalConditions() {
141 Set<EventRule> expectedRules = Collections.EMPTY_SET;
143 assertEquals(4, listener.getNbNotifications());
144 assertEquals(expectedRules, listener.getCurrentRules());
148 public void testAgentListenerSession_AgentSession() {
151 enableRulesInSession();
153 checkOngoingConditions();
158 checkFinalConditions();
162 public void testAgentSessionListener_AgentSession() {
164 enableRulesInSession();
167 checkOngoingConditions();
172 checkFinalConditions();
176 public void testListenerAgentSession_AgentSession() {
179 enableRulesInSession();
181 checkOngoingConditions();
186 checkFinalConditions();
190 public void testListenerSessionAgent_AgentSession() {
192 enableRulesInSession();
195 checkOngoingConditions();
200 checkFinalConditions();
204 public void testSessionAgentListener_AgentSession() {
205 enableRulesInSession();
209 checkOngoingConditions();
214 checkFinalConditions();
218 public void testSessionListenerAgent_AgentSession() {
219 enableRulesInSession();
223 checkOngoingConditions();
228 checkFinalConditions();
234 public void testAgentListenerSession_SessionAgent() {
237 enableRulesInSession();
239 checkOngoingConditions();
242 checkFinalConditions();
244 checkFinalConditions();
248 public void testAgentSessionListener_SessionAgent() {
250 enableRulesInSession();
253 checkOngoingConditions();
256 checkFinalConditions();
258 checkFinalConditions();
262 public void testListenerAgentSession_SessionAgent() {
265 enableRulesInSession();
267 checkOngoingConditions();
270 checkFinalConditions();
272 checkFinalConditions();
276 public void testListenerSessionAgent_SessionAgent() {
278 enableRulesInSession();
281 checkOngoingConditions();
284 checkFinalConditions();
286 checkFinalConditions();
290 public void testSessionAgentListener_SessionAgent() {
291 enableRulesInSession();
295 checkOngoingConditions();
298 checkFinalConditions();
300 checkFinalConditions();
304 public void testSessionListenerAgent_SessionAgent() {
305 enableRulesInSession();
309 checkOngoingConditions();
312 checkFinalConditions();
314 checkFinalConditions();