Commit | Line | Data |
---|---|---|
f37120c3 AM |
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.filter; | |
20 | ||
7a4f0255 | 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
f37120c3 AM |
22 | |
23 | import java.io.IOException; | |
24 | import java.util.Collections; | |
25 | import java.util.HashSet; | |
26 | import java.util.Set; | |
f37120c3 AM |
27 | import java.util.stream.Collectors; |
28 | import java.util.stream.Stream; | |
29 | ||
7a4f0255 MJ |
30 | import org.junit.jupiter.api.Test; |
31 | import org.junit.jupiter.api.extension.ExtendWith; | |
32 | import org.junit.jupiter.api.AfterEach; | |
33 | import org.junit.jupiter.api.BeforeEach; | |
f37120c3 AM |
34 | import org.lttng.tools.ILttngSession; |
35 | import org.lttng.ust.agent.ILttngHandler; | |
329f5794 | 36 | import org.lttng.ust.agent.filter.FilterChangeNotifier; |
f37120c3 AM |
37 | import org.lttng.ust.agent.filter.IFilterChangeListener; |
38 | import org.lttng.ust.agent.session.EventRule; | |
329f5794 AM |
39 | import org.lttng.ust.agent.session.LogLevelSelector; |
40 | import org.lttng.ust.agent.session.LogLevelSelector.LogLevelType; | |
1df8e5d7 | 41 | import org.lttng.ust.agent.utils.EventRuleFactory; |
f37120c3 | 42 | import org.lttng.ust.agent.utils.ILogLevelStrings; |
7a4f0255 | 43 | import org.lttng.ust.agent.utils.TestPrintExtension; |
f37120c3 AM |
44 | |
45 | /** | |
46 | * Base test class for {@link IFilterChangeListener} tests. | |
47 | * | |
48 | * @author Alexandre Montplaisir | |
49 | */ | |
7a4f0255 | 50 | @ExtendWith(TestPrintExtension.class) |
f37120c3 AM |
51 | public abstract class FilterListenerITBase { |
52 | ||
f37120c3 AM |
53 | private static final String EVENT_NAME_A = "eventA"; |
54 | private static final String EVENT_NAME_B = "eventB"; | |
55 | private static final String EVENT_NAME_C = "eventC"; | |
56 | ||
57 | private ILttngSession session; | |
58 | private TestFilterListener listener; | |
59 | private ILttngHandler handler; | |
60 | ||
9db2c69a MJ |
61 | protected EventRuleFactory eventRuleFactory; |
62 | ||
f37120c3 AM |
63 | protected abstract ILttngSession.Domain getSessionDomain(); |
64 | protected abstract ILttngHandler getLogHandler() throws SecurityException, IOException; | |
65 | protected abstract ILogLevelStrings getLogLevelStrings(); | |
66 | ||
9db2c69a MJ |
67 | protected EventRuleFactory getEventRuleFactory() { |
68 | if (eventRuleFactory == null) { | |
69 | eventRuleFactory = new EventRuleFactory(getSessionDomain()); | |
70 | } | |
71 | return eventRuleFactory; | |
72 | } | |
73 | ||
f37120c3 AM |
74 | /** |
75 | * Test setup | |
76 | * | |
77 | * @throws SecurityException | |
78 | * @throws IOException | |
79 | */ | |
7a4f0255 | 80 | @BeforeEach |
f37120c3 AM |
81 | public void setup() throws SecurityException, IOException { |
82 | handler = getLogHandler(); | |
83 | listener = new TestFilterListener(); | |
329f5794 | 84 | FilterChangeNotifier.getInstance().registerListener(listener); |
f37120c3 | 85 | session = ILttngSession.createSession(null, getSessionDomain()); |
29c0b505 AM |
86 | |
87 | assertEquals(0, listener.getNbNotifications()); | |
f37120c3 AM |
88 | } |
89 | ||
90 | /** | |
91 | * Test teardown | |
92 | */ | |
7a4f0255 | 93 | @AfterEach |
f37120c3 AM |
94 | public void teardown() { |
95 | session.close(); | |
329f5794 | 96 | FilterChangeNotifier.getInstance().unregisterListener(listener); |
29c0b505 | 97 | listener = null; |
f37120c3 AM |
98 | handler.close(); |
99 | } | |
100 | ||
101 | /** | |
102 | * Test not sending any commands. | |
103 | */ | |
104 | @Test | |
105 | public void testNoRules() { | |
c1212ca0 AM |
106 | assertEquals(0, listener.getNbNotifications()); |
107 | assertEquals(Collections.EMPTY_SET, listener.getCurrentRules()); | |
f37120c3 AM |
108 | } |
109 | ||
110 | /** | |
111 | * Test sending one event rule. | |
112 | */ | |
113 | @Test | |
114 | public void testOneRule() { | |
115 | Set<EventRule> rules = Collections.singleton( | |
9db2c69a | 116 | getEventRuleFactory().createRule(EVENT_NAME_A)); |
f37120c3 | 117 | |
f37120c3 AM |
118 | session.enableEvent(EVENT_NAME_A, null, false, null); |
119 | ||
c1212ca0 AM |
120 | assertEquals(1, listener.getNbNotifications()); |
121 | assertEquals(rules, listener.getCurrentRules()); | |
f37120c3 AM |
122 | } |
123 | ||
124 | /** | |
125 | * Test sending many event rules. | |
126 | */ | |
127 | @Test | |
128 | public void testManyRules() { | |
1df8e5d7 | 129 | Set<EventRule> rules = Stream.of( |
9db2c69a MJ |
130 | getEventRuleFactory().createRule(EVENT_NAME_A), |
131 | getEventRuleFactory().createRule(EVENT_NAME_B), | |
132 | getEventRuleFactory().createRule(EVENT_NAME_C)) | |
f37120c3 AM |
133 | .collect(Collectors.toSet()); |
134 | ||
f37120c3 AM |
135 | session.enableEvent(EVENT_NAME_A, null, false, null); |
136 | session.enableEvent(EVENT_NAME_B, null, false, null); | |
137 | session.enableEvent(EVENT_NAME_C, null, false, null); | |
138 | ||
c1212ca0 AM |
139 | assertEquals(3, listener.getNbNotifications()); |
140 | assertEquals(rules, listener.getCurrentRules()); | |
f37120c3 AM |
141 | } |
142 | ||
143 | /** | |
144 | * Test enabling then disabling some events. | |
145 | */ | |
146 | @Test | |
147 | public void testManyRulesDisableSome() { | |
148 | Set<EventRule> rules = Collections.singleton( | |
9db2c69a | 149 | getEventRuleFactory().createRule(EVENT_NAME_A)); |
f37120c3 | 150 | |
f37120c3 AM |
151 | session.enableEvent(EVENT_NAME_A, null, false, null); |
152 | session.enableEvent(EVENT_NAME_B, null, false, null); | |
153 | session.enableEvent(EVENT_NAME_C, null, false, null); | |
154 | session.disableEvents(EVENT_NAME_B); | |
155 | session.disableEvents(EVENT_NAME_C); | |
156 | ||
c1212ca0 AM |
157 | assertEquals(5, listener.getNbNotifications()); |
158 | assertEquals(rules, listener.getCurrentRules()); | |
f37120c3 AM |
159 | } |
160 | ||
161 | /** | |
162 | * Test enabling some rules, then calling disable-event -a. | |
163 | */ | |
164 | @Test | |
165 | public void testManyRulesDisableAll() { | |
166 | Set<EventRule> rules = Collections.EMPTY_SET; | |
167 | ||
f37120c3 AM |
168 | session.enableEvent(EVENT_NAME_A, null, false, null); |
169 | session.enableEvent(EVENT_NAME_B, null, false, null); | |
170 | session.enableEvent(EVENT_NAME_C, null, false, null); | |
171 | session.disableAllEvents(); | |
172 | ||
c1212ca0 AM |
173 | /* |
174 | * We should receive 6 notifications, because a "disable-event -a" sends | |
175 | * one for each event that was enabled. | |
176 | */ | |
177 | assertEquals(6, listener.getNbNotifications()); | |
178 | assertEquals(rules, listener.getCurrentRules()); | |
f37120c3 AM |
179 | } |
180 | ||
181 | /** | |
182 | * Test enabling the same event name with various values of loglevels. | |
183 | */ | |
f37120c3 AM |
184 | @Test |
185 | public void testSameEventsDiffLogLevels() { | |
329f5794 AM |
186 | LogLevelSelector lls1 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); |
187 | LogLevelSelector lls2 = new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE); | |
188 | LogLevelSelector lls3 = new LogLevelSelector(getLogLevelStrings().infoInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE); | |
f37120c3 AM |
189 | |
190 | Set<EventRule> rules = Stream.of( | |
9db2c69a MJ |
191 | getEventRuleFactory().createRule(EVENT_NAME_A, lls1), |
192 | getEventRuleFactory().createRule(EVENT_NAME_A, lls2), | |
193 | getEventRuleFactory().createRule(EVENT_NAME_A, lls3)) | |
f37120c3 AM |
194 | .collect(Collectors.toSet()); |
195 | ||
f37120c3 AM |
196 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), false, null); |
197 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().warningName(), true, null); | |
198 | session.enableEvent(EVENT_NAME_A, getLogLevelStrings().infoName(), false, null); | |
199 | ||
c1212ca0 AM |
200 | assertEquals(3, listener.getNbNotifications()); |
201 | assertEquals(rules, listener.getCurrentRules()); | |
f37120c3 AM |
202 | } |
203 | ||
204 | /** | |
205 | * Test enabling the same event name with various filters. | |
206 | */ | |
f37120c3 AM |
207 | @Test |
208 | public void testSameEventsDiffFilters() { | |
209 | String filterA = "filterA"; | |
210 | String filterB = "filterB"; | |
211 | ||
212 | Set<EventRule> rules = Stream.of( | |
9db2c69a MJ |
213 | getEventRuleFactory().createRule(EVENT_NAME_A), |
214 | getEventRuleFactory().createRule(EVENT_NAME_B, getEventRuleFactory().LOG_LEVEL_UNSPECIFIED, filterA), | |
215 | getEventRuleFactory().createRule(EVENT_NAME_C, getEventRuleFactory().LOG_LEVEL_UNSPECIFIED, filterB)) | |
f37120c3 AM |
216 | .collect(Collectors.toSet()); |
217 | ||
f37120c3 AM |
218 | session.enableEvent(EVENT_NAME_A, null, false, null); |
219 | session.enableEvent(EVENT_NAME_B, null, false, filterA); | |
220 | session.enableEvent(EVENT_NAME_C, null, false, filterB); | |
221 | ||
c1212ca0 AM |
222 | assertEquals(3, listener.getNbNotifications()); |
223 | assertEquals(rules, listener.getCurrentRules()); | |
f37120c3 AM |
224 | } |
225 | ||
76afd4c7 AM |
226 | /** |
227 | * Test sending some notifications then detaching a listener. Subsequent | |
228 | * notifications should not be sent. | |
229 | */ | |
230 | @Test | |
231 | public void testDetachingListener() { | |
232 | Set<EventRule> rules = Stream.of( | |
9db2c69a MJ |
233 | getEventRuleFactory().createRule(EVENT_NAME_A), |
234 | getEventRuleFactory().createRule(EVENT_NAME_B)) | |
76afd4c7 AM |
235 | .collect(Collectors.toSet()); |
236 | ||
76afd4c7 AM |
237 | session.enableEvent(EVENT_NAME_A, null, false, null); |
238 | session.enableEvent(EVENT_NAME_B, null, false, null); | |
329f5794 | 239 | FilterChangeNotifier.getInstance().unregisterListener(listener); |
76afd4c7 AM |
240 | session.enableEvent(EVENT_NAME_C, null, false, null); |
241 | ||
c1212ca0 AM |
242 | assertEquals(2, listener.getNbNotifications()); |
243 | assertEquals(rules, listener.getCurrentRules()); | |
76afd4c7 AM |
244 | } |
245 | ||
246 | /** | |
247 | * Run a test with multiple listeners attached to the manager. All listeners | |
248 | * should receive all the data. | |
249 | */ | |
250 | @Test | |
251 | public void testMultipleListeners() { | |
329f5794 | 252 | FilterChangeNotifier fcn = FilterChangeNotifier.getInstance(); |
76afd4c7 AM |
253 | TestFilterListener listener2 = new TestFilterListener(); |
254 | TestFilterListener listener3 = new TestFilterListener(); | |
329f5794 AM |
255 | fcn.registerListener(listener2); |
256 | fcn.registerListener(listener3); | |
76afd4c7 AM |
257 | |
258 | Set<EventRule> rules = Stream.of( | |
9db2c69a MJ |
259 | getEventRuleFactory().createRule(EVENT_NAME_A), |
260 | getEventRuleFactory().createRule(EVENT_NAME_B)) | |
76afd4c7 AM |
261 | .collect(Collectors.toSet()); |
262 | ||
76afd4c7 AM |
263 | session.enableEvent(EVENT_NAME_A, null, false, null); |
264 | session.enableEvent(EVENT_NAME_B, null, false, null); | |
265 | session.enableEvent(EVENT_NAME_C, null, false, null); | |
266 | session.disableEvents(EVENT_NAME_C); | |
267 | ||
c1212ca0 AM |
268 | assertEquals(4, listener.getNbNotifications()); |
269 | assertEquals(rules, listener.getCurrentRules()); | |
270 | ||
271 | assertEquals(4, listener2.getNbNotifications()); | |
272 | assertEquals(rules, listener2.getCurrentRules()); | |
273 | ||
274 | assertEquals(4, listener3.getNbNotifications()); | |
275 | assertEquals(rules, listener3.getCurrentRules()); | |
76afd4c7 | 276 | |
329f5794 AM |
277 | fcn.unregisterListener(listener2); |
278 | fcn.unregisterListener(listener3); | |
76afd4c7 AM |
279 | } |
280 | ||
281 | /** | |
282 | * Test with both attached and unattached listeners. The unattached ones | |
283 | * should not receive anything, but should not interfere with the other | |
284 | * ones. | |
285 | */ | |
286 | @Test | |
287 | public void testUnattachedListeners() { | |
329f5794 | 288 | FilterChangeNotifier fcn = FilterChangeNotifier.getInstance(); |
76afd4c7 AM |
289 | TestFilterListener listener2 = new TestFilterListener(); |
290 | TestFilterListener listener3 = new TestFilterListener(); | |
291 | /* We attach then detach listener2. We never attach listener3 */ | |
329f5794 AM |
292 | fcn.registerListener(listener2); |
293 | fcn.unregisterListener(listener2); | |
76afd4c7 AM |
294 | |
295 | Set<EventRule> rules = Stream.of( | |
9db2c69a MJ |
296 | getEventRuleFactory().createRule(EVENT_NAME_A), |
297 | getEventRuleFactory().createRule(EVENT_NAME_B)) | |
76afd4c7 AM |
298 | .collect(Collectors.toSet()); |
299 | ||
c1212ca0 AM |
300 | session.enableEvent(EVENT_NAME_A, null, false, null); |
301 | session.enableEvent(EVENT_NAME_B, null, false, null); | |
302 | ||
303 | assertEquals(2, listener.getNbNotifications()); | |
304 | assertEquals(rules, listener.getCurrentRules()); | |
305 | ||
306 | assertEquals(0, listener2.getNbNotifications()); | |
307 | assertEquals(Collections.EMPTY_SET, listener2.getCurrentRules()); | |
308 | ||
309 | assertEquals(0, listener3.getNbNotifications()); | |
310 | assertEquals(Collections.EMPTY_SET, listener3.getCurrentRules()); | |
311 | } | |
312 | ||
313 | /** | |
314 | * Test that a newly-registered listener correctly receives the "statedump", | |
315 | * which means all the rules currently active, upon registration. | |
316 | */ | |
317 | @Test | |
318 | public void testStatedump() { | |
329f5794 | 319 | FilterChangeNotifier fcn = FilterChangeNotifier.getInstance(); |
c1212ca0 AM |
320 | TestFilterListener listener2 = new TestFilterListener(); |
321 | ||
322 | Set<EventRule> rules1 = Stream.of( | |
9db2c69a MJ |
323 | getEventRuleFactory().createRule(EVENT_NAME_A), |
324 | getEventRuleFactory().createRule(EVENT_NAME_B)) | |
c1212ca0 AM |
325 | .collect(Collectors.toSet()); |
326 | Set<EventRule> rules2 = Stream.of( | |
9db2c69a MJ |
327 | getEventRuleFactory().createRule(EVENT_NAME_A), |
328 | getEventRuleFactory().createRule(EVENT_NAME_C)) | |
c1212ca0 | 329 | .collect(Collectors.toSet()); |
76afd4c7 AM |
330 | |
331 | session.enableEvent(EVENT_NAME_A, null, false, null); | |
332 | session.enableEvent(EVENT_NAME_B, null, false, null); | |
329f5794 | 333 | fcn.registerListener(listener2); |
76afd4c7 | 334 | |
c1212ca0 AM |
335 | /* We should have received the "statedump" when registering */ |
336 | assertEquals(2, listener2.getNbNotifications()); | |
337 | assertEquals(rules1, listener2.getCurrentRules()); | |
338 | ||
339 | session.enableEvent(EVENT_NAME_C, null, false, null); | |
340 | session.disableEvents(EVENT_NAME_B); | |
341 | ||
342 | /* Subsequent changes should also be received */ | |
343 | assertEquals(4, listener2.getNbNotifications()); | |
344 | assertEquals(rules2, listener2.getCurrentRules()); | |
345 | ||
329f5794 | 346 | fcn.unregisterListener(listener2); |
76afd4c7 AM |
347 | } |
348 | ||
f37120c3 AM |
349 | /** |
350 | * The filter listener used for tests. | |
f37120c3 | 351 | */ |
29c0b505 | 352 | static class TestFilterListener implements IFilterChangeListener { |
f37120c3 AM |
353 | |
354 | private final Set<EventRule> currentRules = new HashSet<>(); | |
c1212ca0 | 355 | private volatile int currentNotifications = 0; |
f37120c3 AM |
356 | |
357 | public TestFilterListener() {} | |
358 | ||
359 | @Override | |
360 | public void eventRuleAdded(EventRule rule) { | |
361 | currentRules.add(rule); | |
c1212ca0 | 362 | currentNotifications++; |
f37120c3 AM |
363 | } |
364 | ||
365 | @Override | |
366 | public void eventRuleRemoved(EventRule rule) { | |
367 | currentRules.remove(rule); | |
c1212ca0 | 368 | currentNotifications++; |
f37120c3 AM |
369 | } |
370 | ||
c1212ca0 AM |
371 | public int getNbNotifications() { |
372 | return currentNotifications; | |
f37120c3 AM |
373 | } |
374 | ||
c1212ca0 AM |
375 | public Set<EventRule> getCurrentRules() { |
376 | return currentRules; | |
f37120c3 AM |
377 | } |
378 | } | |
379 | ||
380 | } |