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