4e344e05d3c891bb3b3871e7badab359ae0fad43
[lttng-ust-java-tests.git] /
1 /*
2 * Copyright (C) 2016, 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.filter;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import java.util.Collections;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import java.util.stream.Stream;
27
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;
37
38 /**
39 * For the filter change notifications to work, several setup steps are
40 * required:
41 *
42 * <ul>
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
45 * [Listener]</li>
46 * <li>Apply some event rule changes in the tracing session (lttng enable-event,
47 * etc.) [Session]</li>
48 * </ul>
49 *
50 * <p>
51 * Then on teardown, the following steps are expected:
52 * </p>
53 *
54 * <ul>
55 * <li>Dispose the Java agent, closing the connection to the sessiond [Agent]
56 * </li>
57 * <li>Destroy the tracing session, removing tracked events [Session]</li>
58 * </ul>
59 *
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).
63 *
64 * <p>
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
67 * possibilities.
68 * </p>
69 */
70 @ExtendWith(TestPrintExtension.class)
71 @SuppressWarnings("javadoc")
72 public abstract class FilterListenerOrderingITBase {
73
74 protected static final String EVENT_NAME_A = "EventA";
75 private static final String EVENT_NAME_B = "EventB";
76
77 private ILttngSession session;
78 private TestFilterListener listener;
79
80 /**
81 * Base class cleanup
82 */
83 @AfterEach
84 public void baseTeardown() {
85 /*
86 * Deregister the listener (should always be done after all the other
87 * steps).
88 */
89 FilterChangeNotifier.getInstance().unregisterListener(listener);
90 listener = null;
91 }
92
93 // ------------------------------------------------------------------------
94 // Utility methods
95 // ------------------------------------------------------------------------
96
97 protected abstract ILttngSession.Domain getDomain();
98
99 protected abstract void registerAgent();
100
101 private void registerListener() {
102 listener = new TestFilterListener();
103 FilterChangeNotifier.getInstance().registerListener(listener);
104 }
105
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);
110 }
111
112 protected abstract void deregisterAgent();
113
114 private void destroySession() {
115 session.close();
116 session = null;
117 }
118
119 // ------------------------------------------------------------------------
120 // Test methods
121 // ------------------------------------------------------------------------
122
123 /**
124 * Check that the expected event rules are present after setup but before
125 * teardown.
126 */
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());
132
133 assertEquals(2, listener.getNbNotifications());
134 assertEquals(exptectedRules, listener.getCurrentRules());
135 }
136
137 /**
138 * Check that the expected event rules are present after/during teardown.
139 */
140 private void checkFinalConditions() {
141 Set<EventRule> expectedRules = Collections.EMPTY_SET;
142
143 assertEquals(4, listener.getNbNotifications());
144 assertEquals(expectedRules, listener.getCurrentRules());
145 }
146
147 @Test
148 public void testAgentListenerSession_AgentSession() {
149 registerAgent();
150 registerListener();
151 enableRulesInSession();
152
153 checkOngoingConditions();
154
155 deregisterAgent();
156 destroySession();
157
158 checkFinalConditions();
159 }
160
161 @Test
162 public void testAgentSessionListener_AgentSession() {
163 registerAgent();
164 enableRulesInSession();
165 registerListener();
166
167 checkOngoingConditions();
168
169 deregisterAgent();
170 destroySession();
171
172 checkFinalConditions();
173 }
174
175 @Test
176 public void testListenerAgentSession_AgentSession() {
177 registerListener();
178 registerAgent();
179 enableRulesInSession();
180
181 checkOngoingConditions();
182
183 deregisterAgent();
184 destroySession();
185
186 checkFinalConditions();
187 }
188
189 @Test
190 public void testListenerSessionAgent_AgentSession() {
191 registerListener();
192 enableRulesInSession();
193 registerAgent();
194
195 checkOngoingConditions();
196
197 deregisterAgent();
198 destroySession();
199
200 checkFinalConditions();
201 }
202
203 @Test
204 public void testSessionAgentListener_AgentSession() {
205 enableRulesInSession();
206 registerAgent();
207 registerListener();
208
209 checkOngoingConditions();
210
211 deregisterAgent();
212 destroySession();
213
214 checkFinalConditions();
215 }
216
217 @Test
218 public void testSessionListenerAgent_AgentSession() {
219 enableRulesInSession();
220 registerListener();
221 registerAgent();
222
223 checkOngoingConditions();
224
225 deregisterAgent();
226 destroySession();
227
228 checkFinalConditions();
229 }
230
231
232
233 @Test
234 public void testAgentListenerSession_SessionAgent() {
235 registerAgent();
236 registerListener();
237 enableRulesInSession();
238
239 checkOngoingConditions();
240
241 destroySession();
242 checkFinalConditions();
243 deregisterAgent();
244 checkFinalConditions();
245 }
246
247 @Test
248 public void testAgentSessionListener_SessionAgent() {
249 registerAgent();
250 enableRulesInSession();
251 registerListener();
252
253 checkOngoingConditions();
254
255 destroySession();
256 checkFinalConditions();
257 deregisterAgent();
258 checkFinalConditions();
259 }
260
261 @Test
262 public void testListenerAgentSession_SessionAgent() {
263 registerListener();
264 registerAgent();
265 enableRulesInSession();
266
267 checkOngoingConditions();
268
269 destroySession();
270 checkFinalConditions();
271 deregisterAgent();
272 checkFinalConditions();
273 }
274
275 @Test
276 public void testListenerSessionAgent_SessionAgent() {
277 registerListener();
278 enableRulesInSession();
279 registerAgent();
280
281 checkOngoingConditions();
282
283 destroySession();
284 checkFinalConditions();
285 deregisterAgent();
286 checkFinalConditions();
287 }
288
289 @Test
290 public void testSessionAgentListener_SessionAgent() {
291 enableRulesInSession();
292 registerAgent();
293 registerListener();
294
295 checkOngoingConditions();
296
297 destroySession();
298 checkFinalConditions();
299 deregisterAgent();
300 checkFinalConditions();
301 }
302
303 @Test
304 public void testSessionListenerAgent_SessionAgent() {
305 enableRulesInSession();
306 registerListener();
307 registerAgent();
308
309 checkOngoingConditions();
310
311 destroySession();
312 checkFinalConditions();
313 deregisterAgent();
314 checkFinalConditions();
315 }
316
317 }
This page took 0.035019 seconds and 3 git commands to generate.