Rework filter tests to use in-same-thread notifications
[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;
32import org.junit.Ignore;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.lttng.tools.ILttngSession;
36import org.lttng.ust.agent.ILttngHandler;
37import org.lttng.ust.agent.filter.FilterNotificationManager;
38import org.lttng.ust.agent.filter.IFilterChangeListener;
39import org.lttng.ust.agent.session.EventRule;
40import org.lttng.ust.agent.session.LogLevelFilter;
41import org.lttng.ust.agent.session.LogLevelFilter.LogLevelType;
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
53 protected static final LogLevelFilter LOG_LEVEL_UNSPECIFIED = new LogLevelFilter(Integer.MIN_VALUE, 0);
54
55 private static final String EVENT_NAME_A = "eventA";
56 private static final String EVENT_NAME_B = "eventB";
57 private static final String EVENT_NAME_C = "eventC";
58
59 private ILttngSession session;
60 private TestFilterListener listener;
61 private ILttngHandler handler;
62
63 protected abstract ILttngSession.Domain getSessionDomain();
64 protected abstract ILttngHandler getLogHandler() throws SecurityException, IOException;
65 protected abstract ILogLevelStrings getLogLevelStrings();
66
67 /**
68 * Test setup
69 *
70 * @throws SecurityException
71 * @throws IOException
72 */
73 @Before
74 public void setup() throws SecurityException, IOException {
75 handler = getLogHandler();
76 listener = new TestFilterListener();
77 FilterNotificationManager.getInstance().registerListener(listener);
78 session = ILttngSession.createSession(null, getSessionDomain());
79 }
80
81 /**
82 * Test teardown
83 */
84 @After
85 public void teardown() {
86 session.close();
87 FilterNotificationManager.getInstance().unregisterListener(listener);
88 handler.close();
89 }
90
91 /**
92 * Test not sending any commands.
93 */
94 @Test
95 public void testNoRules() {
c1212ca0
AM
96 assertEquals(0, listener.getNbNotifications());
97 assertEquals(Collections.EMPTY_SET, listener.getCurrentRules());
f37120c3
AM
98 }
99
100 /**
101 * Test sending one event rule.
102 */
103 @Test
104 public void testOneRule() {
105 Set<EventRule> rules = Collections.singleton(
106 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null));
107
f37120c3
AM
108 session.enableEvent(EVENT_NAME_A, null, false, null);
109
c1212ca0
AM
110 assertEquals(1, listener.getNbNotifications());
111 assertEquals(rules, listener.getCurrentRules());
f37120c3
AM
112 }
113
114 /**
115 * Test sending many event rules.
116 */
117 @Test
118 public void testManyRules() {
119 Set<EventRule> rules = Stream
120 .of(new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null),
121 new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null),
122 new EventRule(EVENT_NAME_C, LOG_LEVEL_UNSPECIFIED, null))
123 .collect(Collectors.toSet());
124
f37120c3
AM
125 session.enableEvent(EVENT_NAME_A, null, false, null);
126 session.enableEvent(EVENT_NAME_B, null, false, null);
127 session.enableEvent(EVENT_NAME_C, null, false, null);
128
c1212ca0
AM
129 assertEquals(3, listener.getNbNotifications());
130 assertEquals(rules, listener.getCurrentRules());
f37120c3
AM
131 }
132
133 /**
134 * Test enabling then disabling some events.
135 */
136 @Test
137 public void testManyRulesDisableSome() {
138 Set<EventRule> rules = Collections.singleton(
139 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null));
140
f37120c3
AM
141 session.enableEvent(EVENT_NAME_A, null, false, null);
142 session.enableEvent(EVENT_NAME_B, null, false, null);
143 session.enableEvent(EVENT_NAME_C, null, false, null);
144 session.disableEvents(EVENT_NAME_B);
145 session.disableEvents(EVENT_NAME_C);
146
c1212ca0
AM
147 assertEquals(5, listener.getNbNotifications());
148 assertEquals(rules, listener.getCurrentRules());
f37120c3
AM
149 }
150
151 /**
152 * Test enabling some rules, then calling disable-event -a.
153 */
154 @Test
155 public void testManyRulesDisableAll() {
156 Set<EventRule> rules = Collections.EMPTY_SET;
157
f37120c3
AM
158 session.enableEvent(EVENT_NAME_A, null, false, null);
159 session.enableEvent(EVENT_NAME_B, null, false, null);
160 session.enableEvent(EVENT_NAME_C, null, false, null);
161 session.disableAllEvents();
162
c1212ca0
AM
163 /*
164 * We should receive 6 notifications, because a "disable-event -a" sends
165 * one for each event that was enabled.
166 */
167 assertEquals(6, listener.getNbNotifications());
168 assertEquals(rules, listener.getCurrentRules());
f37120c3
AM
169 }
170
171 /**
172 * Test enabling the same event name with various values of loglevels.
173 */
174 @Ignore("Does not work as expected atm, see http://bugs.lttng.org/issues/913")
175 @Test
176 public void testSameEventsDiffLogLevels() {
177 LogLevelFilter llf1 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE);
178 LogLevelFilter llf2 = new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_SINGLE);
179 LogLevelFilter llf3 = new LogLevelFilter(getLogLevelStrings().infoInt(), LogLevelType.LTTNG_EVENT_LOGLEVEL_RANGE);
180
181 Set<EventRule> rules = Stream.of(
182 new EventRule(EVENT_NAME_A, llf1, null),
183 new EventRule(EVENT_NAME_A, llf2, null),
184 new EventRule(EVENT_NAME_A, llf3, null))
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 */
198 @Ignore("Filters are not tracked yet")
199 @Test
200 public void testSameEventsDiffFilters() {
201 String filterA = "filterA";
202 String filterB = "filterB";
203
204 Set<EventRule> rules = Stream.of(
205 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null),
206 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, filterA),
207 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, filterB))
208 .collect(Collectors.toSet());
209
f37120c3
AM
210 session.enableEvent(EVENT_NAME_A, null, false, null);
211 session.enableEvent(EVENT_NAME_B, null, false, filterA);
212 session.enableEvent(EVENT_NAME_C, null, false, filterB);
213
c1212ca0
AM
214 assertEquals(3, listener.getNbNotifications());
215 assertEquals(rules, listener.getCurrentRules());
f37120c3
AM
216 }
217
76afd4c7
AM
218 /**
219 * Test sending some notifications then detaching a listener. Subsequent
220 * notifications should not be sent.
221 */
222 @Test
223 public void testDetachingListener() {
224 Set<EventRule> rules = Stream.of(
225 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null),
226 new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null))
227 .collect(Collectors.toSet());
228
76afd4c7
AM
229 session.enableEvent(EVENT_NAME_A, null, false, null);
230 session.enableEvent(EVENT_NAME_B, null, false, null);
231 FilterNotificationManager.getInstance().unregisterListener(listener);
232 session.enableEvent(EVENT_NAME_C, null, false, null);
233
c1212ca0
AM
234 assertEquals(2, listener.getNbNotifications());
235 assertEquals(rules, listener.getCurrentRules());
76afd4c7
AM
236 }
237
238 /**
239 * Run a test with multiple listeners attached to the manager. All listeners
240 * should receive all the data.
241 */
242 @Test
243 public void testMultipleListeners() {
244 FilterNotificationManager fnm = FilterNotificationManager.getInstance();
245 TestFilterListener listener2 = new TestFilterListener();
246 TestFilterListener listener3 = new TestFilterListener();
247 fnm.registerListener(listener2);
248 fnm.registerListener(listener3);
249
250 Set<EventRule> rules = Stream.of(
251 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null),
252 new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null))
253 .collect(Collectors.toSet());
254
76afd4c7
AM
255 session.enableEvent(EVENT_NAME_A, null, false, null);
256 session.enableEvent(EVENT_NAME_B, null, false, null);
257 session.enableEvent(EVENT_NAME_C, null, false, null);
258 session.disableEvents(EVENT_NAME_C);
259
c1212ca0
AM
260 assertEquals(4, listener.getNbNotifications());
261 assertEquals(rules, listener.getCurrentRules());
262
263 assertEquals(4, listener2.getNbNotifications());
264 assertEquals(rules, listener2.getCurrentRules());
265
266 assertEquals(4, listener3.getNbNotifications());
267 assertEquals(rules, listener3.getCurrentRules());
76afd4c7
AM
268
269 fnm.unregisterListener(listener2);
270 fnm.unregisterListener(listener3);
271 }
272
273 /**
274 * Test with both attached and unattached listeners. The unattached ones
275 * should not receive anything, but should not interfere with the other
276 * ones.
277 */
278 @Test
279 public void testUnattachedListeners() {
280 FilterNotificationManager fnm = FilterNotificationManager.getInstance();
281 TestFilterListener listener2 = new TestFilterListener();
282 TestFilterListener listener3 = new TestFilterListener();
283 /* We attach then detach listener2. We never attach listener3 */
284 fnm.registerListener(listener2);
285 fnm.unregisterListener(listener2);
286
287 Set<EventRule> rules = Stream.of(
288 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null),
289 new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null))
290 .collect(Collectors.toSet());
291
c1212ca0
AM
292 session.enableEvent(EVENT_NAME_A, null, false, null);
293 session.enableEvent(EVENT_NAME_B, null, false, null);
294
295 assertEquals(2, listener.getNbNotifications());
296 assertEquals(rules, listener.getCurrentRules());
297
298 assertEquals(0, listener2.getNbNotifications());
299 assertEquals(Collections.EMPTY_SET, listener2.getCurrentRules());
300
301 assertEquals(0, listener3.getNbNotifications());
302 assertEquals(Collections.EMPTY_SET, listener3.getCurrentRules());
303 }
304
305 /**
306 * Test that a newly-registered listener correctly receives the "statedump",
307 * which means all the rules currently active, upon registration.
308 */
309 @Test
310 public void testStatedump() {
311 FilterNotificationManager fnm = FilterNotificationManager.getInstance();
312 TestFilterListener listener2 = new TestFilterListener();
313
314 Set<EventRule> rules1 = Stream.of(
315 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null),
316 new EventRule(EVENT_NAME_B, LOG_LEVEL_UNSPECIFIED, null))
317 .collect(Collectors.toSet());
318 Set<EventRule> rules2 = Stream.of(
319 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null),
320 new EventRule(EVENT_NAME_C, LOG_LEVEL_UNSPECIFIED, null))
321 .collect(Collectors.toSet());
76afd4c7
AM
322
323 session.enableEvent(EVENT_NAME_A, null, false, null);
324 session.enableEvent(EVENT_NAME_B, null, false, null);
c1212ca0 325 fnm.registerListener(listener2);
76afd4c7 326
c1212ca0
AM
327 /* We should have received the "statedump" when registering */
328 assertEquals(2, listener2.getNbNotifications());
329 assertEquals(rules1, listener2.getCurrentRules());
330
331 session.enableEvent(EVENT_NAME_C, null, false, null);
332 session.disableEvents(EVENT_NAME_B);
333
334 /* Subsequent changes should also be received */
335 assertEquals(4, listener2.getNbNotifications());
336 assertEquals(rules2, listener2.getCurrentRules());
337
338 fnm.unregisterListener(listener2);
76afd4c7
AM
339 }
340
f37120c3
AM
341 /**
342 * The filter listener used for tests.
343 *
344 * <p>
345 * Usage:
346 * <ul>
347 * <li>Specify the expected number of notifications and end rules with
348 * {@link #setParameters}.</li>
349 * <li>Send the commands to LTTng (using {@link ILttngSession} for example.
350 * </li>
351 * <li>Call {@link #waitForAllNotifications()}.</li>
352 * <li>Verify that {@link #checkRules()} returns true.</li>
353 * </ul>
354 * </p>
355 */
356 private static class TestFilterListener implements IFilterChangeListener {
357
358 private final Set<EventRule> currentRules = new HashSet<>();
c1212ca0 359 private volatile int currentNotifications = 0;
f37120c3
AM
360
361 public TestFilterListener() {}
362
363 @Override
364 public void eventRuleAdded(EventRule rule) {
365 currentRules.add(rule);
c1212ca0 366 currentNotifications++;
f37120c3
AM
367 }
368
369 @Override
370 public void eventRuleRemoved(EventRule rule) {
371 currentRules.remove(rule);
c1212ca0 372 currentNotifications++;
f37120c3
AM
373 }
374
c1212ca0
AM
375 public int getNbNotifications() {
376 return currentNotifications;
f37120c3
AM
377 }
378
c1212ca0
AM
379 public Set<EventRule> getCurrentRules() {
380 return currentRules;
f37120c3
AM
381 }
382 }
383
384}
This page took 0.039817 seconds and 4 git commands to generate.