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