Add 'log4j2' domain tests to the Log4j 2.x agent
[lttng-ust-java-tests.git] / lttng-ust-java-tests-common / src / main / java / org / lttng / ust / agent / integration / filter / FilterListenerITBase.java
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
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import java.io.IOException;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import java.util.stream.Stream;
29
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;
34 import org.lttng.tools.ILttngSession;
35 import org.lttng.ust.agent.ILttngHandler;
36 import org.lttng.ust.agent.filter.FilterChangeNotifier;
37 import org.lttng.ust.agent.filter.IFilterChangeListener;
38 import org.lttng.ust.agent.session.EventRule;
39 import org.lttng.ust.agent.session.LogLevelSelector;
40 import org.lttng.ust.agent.session.LogLevelSelector.LogLevelType;
41 import org.lttng.ust.agent.utils.EventRuleFactory;
42 import org.lttng.ust.agent.utils.ILogLevelStrings;
43 import org.lttng.ust.agent.utils.TestPrintExtension;
44
45 /**
46 * Base test class for {@link IFilterChangeListener} tests.
47 *
48 * @author Alexandre Montplaisir
49 */
50 @ExtendWith(TestPrintExtension.class)
51 public abstract class FilterListenerITBase {
52
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 EventRuleFactory eventRuleFactory;
62
63 protected abstract ILttngSession.Domain getSessionDomain();
64 protected abstract ILttngHandler getLogHandler() throws SecurityException, IOException;
65 protected abstract ILogLevelStrings getLogLevelStrings();
66
67 protected EventRuleFactory getEventRuleFactory() {
68 if (eventRuleFactory == null) {
69 eventRuleFactory = new EventRuleFactory(getSessionDomain());
70 }
71 return eventRuleFactory;
72 }
73
74 /**
75 * Test setup
76 *
77 * @throws SecurityException
78 * @throws IOException
79 */
80 @BeforeEach
81 public void setup() throws SecurityException, IOException {
82 handler = getLogHandler();
83 listener = new TestFilterListener();
84 FilterChangeNotifier.getInstance().registerListener(listener);
85 session = ILttngSession.createSession(null, getSessionDomain());
86
87 assertEquals(0, listener.getNbNotifications());
88 }
89
90 /**
91 * Test teardown
92 */
93 @AfterEach
94 public void teardown() {
95 session.close();
96 FilterChangeNotifier.getInstance().unregisterListener(listener);
97 listener = null;
98 handler.close();
99 }
100
101 /**
102 * Test not sending any commands.
103 */
104 @Test
105 public void testNoRules() {
106 assertEquals(0, listener.getNbNotifications());
107 assertEquals(Collections.EMPTY_SET, listener.getCurrentRules());
108 }
109
110 /**
111 * Test sending one event rule.
112 */
113 @Test
114 public void testOneRule() {
115 Set<EventRule> rules = Collections.singleton(
116 getEventRuleFactory().createRule(EVENT_NAME_A));
117
118 session.enableEvent(EVENT_NAME_A, null, false, null);
119
120 assertEquals(1, listener.getNbNotifications());
121 assertEquals(rules, listener.getCurrentRules());
122 }
123
124 /**
125 * Test sending many event rules.
126 */
127 @Test
128 public void testManyRules() {
129 Set<EventRule> rules = Stream.of(
130 getEventRuleFactory().createRule(EVENT_NAME_A),
131 getEventRuleFactory().createRule(EVENT_NAME_B),
132 getEventRuleFactory().createRule(EVENT_NAME_C))
133 .collect(Collectors.toSet());
134
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
139 assertEquals(3, listener.getNbNotifications());
140 assertEquals(rules, listener.getCurrentRules());
141 }
142
143 /**
144 * Test enabling then disabling some events.
145 */
146 @Test
147 public void testManyRulesDisableSome() {
148 Set<EventRule> rules = Collections.singleton(
149 getEventRuleFactory().createRule(EVENT_NAME_A));
150
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
157 assertEquals(5, listener.getNbNotifications());
158 assertEquals(rules, listener.getCurrentRules());
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
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
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());
179 }
180
181 /**
182 * Test enabling the same event name with various values of loglevels.
183 */
184 @Test
185 public void testSameEventsDiffLogLevels() {
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);
189
190 Set<EventRule> rules = Stream.of(
191 getEventRuleFactory().createRule(EVENT_NAME_A, lls1),
192 getEventRuleFactory().createRule(EVENT_NAME_A, lls2),
193 getEventRuleFactory().createRule(EVENT_NAME_A, lls3))
194 .collect(Collectors.toSet());
195
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
200 assertEquals(3, listener.getNbNotifications());
201 assertEquals(rules, listener.getCurrentRules());
202 }
203
204 /**
205 * Test enabling the same event name with various filters.
206 */
207 @Test
208 public void testSameEventsDiffFilters() {
209 String filterA = "filterA";
210 String filterB = "filterB";
211
212 Set<EventRule> rules = Stream.of(
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))
216 .collect(Collectors.toSet());
217
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
222 assertEquals(3, listener.getNbNotifications());
223 assertEquals(rules, listener.getCurrentRules());
224 }
225
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(
233 getEventRuleFactory().createRule(EVENT_NAME_A),
234 getEventRuleFactory().createRule(EVENT_NAME_B))
235 .collect(Collectors.toSet());
236
237 session.enableEvent(EVENT_NAME_A, null, false, null);
238 session.enableEvent(EVENT_NAME_B, null, false, null);
239 FilterChangeNotifier.getInstance().unregisterListener(listener);
240 session.enableEvent(EVENT_NAME_C, null, false, null);
241
242 assertEquals(2, listener.getNbNotifications());
243 assertEquals(rules, listener.getCurrentRules());
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() {
252 FilterChangeNotifier fcn = FilterChangeNotifier.getInstance();
253 TestFilterListener listener2 = new TestFilterListener();
254 TestFilterListener listener3 = new TestFilterListener();
255 fcn.registerListener(listener2);
256 fcn.registerListener(listener3);
257
258 Set<EventRule> rules = Stream.of(
259 getEventRuleFactory().createRule(EVENT_NAME_A),
260 getEventRuleFactory().createRule(EVENT_NAME_B))
261 .collect(Collectors.toSet());
262
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
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());
276
277 fcn.unregisterListener(listener2);
278 fcn.unregisterListener(listener3);
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() {
288 FilterChangeNotifier fcn = FilterChangeNotifier.getInstance();
289 TestFilterListener listener2 = new TestFilterListener();
290 TestFilterListener listener3 = new TestFilterListener();
291 /* We attach then detach listener2. We never attach listener3 */
292 fcn.registerListener(listener2);
293 fcn.unregisterListener(listener2);
294
295 Set<EventRule> rules = Stream.of(
296 getEventRuleFactory().createRule(EVENT_NAME_A),
297 getEventRuleFactory().createRule(EVENT_NAME_B))
298 .collect(Collectors.toSet());
299
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() {
319 FilterChangeNotifier fcn = FilterChangeNotifier.getInstance();
320 TestFilterListener listener2 = new TestFilterListener();
321
322 Set<EventRule> rules1 = Stream.of(
323 getEventRuleFactory().createRule(EVENT_NAME_A),
324 getEventRuleFactory().createRule(EVENT_NAME_B))
325 .collect(Collectors.toSet());
326 Set<EventRule> rules2 = Stream.of(
327 getEventRuleFactory().createRule(EVENT_NAME_A),
328 getEventRuleFactory().createRule(EVENT_NAME_C))
329 .collect(Collectors.toSet());
330
331 session.enableEvent(EVENT_NAME_A, null, false, null);
332 session.enableEvent(EVENT_NAME_B, null, false, null);
333 fcn.registerListener(listener2);
334
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
346 fcn.unregisterListener(listener2);
347 }
348
349 /**
350 * The filter listener used for tests.
351 */
352 static class TestFilterListener implements IFilterChangeListener {
353
354 private final Set<EventRule> currentRules = new HashSet<>();
355 private volatile int currentNotifications = 0;
356
357 public TestFilterListener() {}
358
359 @Override
360 public void eventRuleAdded(EventRule rule) {
361 currentRules.add(rule);
362 currentNotifications++;
363 }
364
365 @Override
366 public void eventRuleRemoved(EventRule rule) {
367 currentRules.remove(rule);
368 currentNotifications++;
369 }
370
371 public int getNbNotifications() {
372 return currentNotifications;
373 }
374
375 public Set<EventRule> getCurrentRules() {
376 return currentRules;
377 }
378 }
379
380 }
This page took 0.064358 seconds and 4 git commands to generate.