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