2 * Copyright (C) 2015, EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
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.
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.
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.
19 package org
.lttng
.ust
.agent
.integration
.filter
;
21 import static org
.junit
.Assert
.assertTrue
;
23 import java
.io
.IOException
;
24 import java
.util
.Collections
;
25 import java
.util
.HashSet
;
27 import java
.util
.concurrent
.CountDownLatch
;
28 import java
.util
.concurrent
.TimeUnit
;
29 import java
.util
.stream
.Collectors
;
30 import java
.util
.stream
.Stream
;
32 import org
.junit
.After
;
33 import org
.junit
.Before
;
34 import org
.junit
.Ignore
;
35 import org
.junit
.Test
;
36 import org
.junit
.runner
.RunWith
;
37 import org
.lttng
.tools
.ILttngSession
;
38 import org
.lttng
.ust
.agent
.ILttngHandler
;
39 import org
.lttng
.ust
.agent
.filter
.FilterNotificationManager
;
40 import org
.lttng
.ust
.agent
.filter
.IFilterChangeListener
;
41 import org
.lttng
.ust
.agent
.session
.EventRule
;
42 import org
.lttng
.ust
.agent
.session
.LogLevelFilter
;
43 import org
.lttng
.ust
.agent
.session
.LogLevelFilter
.LogLevelType
;
44 import org
.lttng
.ust
.agent
.utils
.ILogLevelStrings
;
45 import org
.lttng
.ust
.agent
.utils
.TestPrintRunner
;
48 * Base test class for {@link IFilterChangeListener} tests.
50 * @author Alexandre Montplaisir
52 @RunWith(TestPrintRunner
.class)
53 public abstract class FilterListenerITBase
{
55 protected static final LogLevelFilter LOG_LEVEL_UNSPECIFIED
= new LogLevelFilter(Integer
.MIN_VALUE
, 0);
57 private static final String EVENT_NAME_A
= "eventA";
58 private static final String EVENT_NAME_B
= "eventB";
59 private static final String EVENT_NAME_C
= "eventC";
61 private ILttngSession session
;
62 private TestFilterListener listener
;
63 private ILttngHandler handler
;
65 protected abstract ILttngSession
.Domain
getSessionDomain();
66 protected abstract ILttngHandler
getLogHandler() throws SecurityException
, IOException
;
67 protected abstract ILogLevelStrings
getLogLevelStrings();
72 * @throws SecurityException
76 public void setup() throws SecurityException
, IOException
{
77 handler
= getLogHandler();
78 listener
= new TestFilterListener();
79 FilterNotificationManager
.getInstance().registerListener(listener
);
80 session
= ILttngSession
.createSession(null, getSessionDomain());
87 public void teardown() {
89 FilterNotificationManager
.getInstance().unregisterListener(listener
);
94 * Test not sending any commands.
97 public void testNoRules() {
98 Set
<EventRule
> rules
= Collections
.EMPTY_SET
;
99 listener
.setParameters(0, rules
);
100 /* Don't enable any events */
102 assertTrue(listener
.waitForAllNotifications());
103 assertTrue(listener
.checkRules());
107 * Test sending one event rule.
110 public void testOneRule() {
111 Set
<EventRule
> rules
= Collections
.singleton(
112 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, null));
114 listener
.setParameters(1, rules
);
116 session
.enableEvent(EVENT_NAME_A
, null, false, null);
118 assertTrue(listener
.waitForAllNotifications());
119 assertTrue(listener
.checkRules());
123 * Test sending many event rules.
126 public void testManyRules() {
127 Set
<EventRule
> rules
= Stream
128 .of(new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, null),
129 new EventRule(EVENT_NAME_B
, LOG_LEVEL_UNSPECIFIED
, null),
130 new EventRule(EVENT_NAME_C
, LOG_LEVEL_UNSPECIFIED
, null))
131 .collect(Collectors
.toSet());
133 listener
.setParameters(3, rules
);
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);
139 assertTrue(listener
.waitForAllNotifications());
140 assertTrue(listener
.checkRules());
144 * Test enabling then disabling some events.
147 public void testManyRulesDisableSome() {
148 Set
<EventRule
> rules
= Collections
.singleton(
149 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, null));
151 listener
.setParameters(4, rules
);
153 session
.enableEvent(EVENT_NAME_A
, null, false, null);
154 session
.enableEvent(EVENT_NAME_B
, null, false, null);
155 session
.enableEvent(EVENT_NAME_C
, null, false, null);
156 session
.disableEvents(EVENT_NAME_B
);
157 session
.disableEvents(EVENT_NAME_C
);
159 assertTrue(listener
.waitForAllNotifications());
160 assertTrue(listener
.checkRules());
164 * Test enabling some rules, then calling disable-event -a.
167 public void testManyRulesDisableAll() {
168 Set
<EventRule
> rules
= Collections
.EMPTY_SET
;
171 * We should receive 6 notifications, because a "disable-event -a" sends
172 * one for each event that was enabled.
174 listener
.setParameters(6, rules
);
176 session
.enableEvent(EVENT_NAME_A
, null, false, null);
177 session
.enableEvent(EVENT_NAME_B
, null, false, null);
178 session
.enableEvent(EVENT_NAME_C
, null, false, null);
179 session
.disableAllEvents();
181 assertTrue(listener
.waitForAllNotifications());
182 assertTrue(listener
.checkRules());
186 * Test enabling the same event name with various values of loglevels.
188 @Ignore("Does not work as expected atm, see http://bugs.lttng.org/issues/913")
190 public void testSameEventsDiffLogLevels() {
191 LogLevelFilter llf1
= new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_RANGE
);
192 LogLevelFilter llf2
= new LogLevelFilter(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_SINGLE
);
193 LogLevelFilter llf3
= new LogLevelFilter(getLogLevelStrings().infoInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_RANGE
);
195 Set
<EventRule
> rules
= Stream
.of(
196 new EventRule(EVENT_NAME_A
, llf1
, null),
197 new EventRule(EVENT_NAME_A
, llf2
, null),
198 new EventRule(EVENT_NAME_A
, llf3
, null))
199 .collect(Collectors
.toSet());
201 listener
.setParameters(3, rules
);
203 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), false, null);
204 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), true, null);
205 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().infoName(), false, null);
207 assertTrue(listener
.waitForAllNotifications());
208 assertTrue(listener
.checkRules());
212 * Test enabling the same event name with various filters.
214 @Ignore("Filters are not tracked yet")
216 public void testSameEventsDiffFilters() {
217 String filterA
= "filterA";
218 String filterB
= "filterB";
220 Set
<EventRule
> rules
= Stream
.of(
221 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, null),
222 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, filterA
),
223 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, filterB
))
224 .collect(Collectors
.toSet());
226 listener
.setParameters(3, rules
);
228 session
.enableEvent(EVENT_NAME_A
, null, false, null);
229 session
.enableEvent(EVENT_NAME_B
, null, false, filterA
);
230 session
.enableEvent(EVENT_NAME_C
, null, false, filterB
);
232 assertTrue(listener
.waitForAllNotifications());
233 assertTrue(listener
.checkRules());
237 * The filter listener used for tests.
242 * <li>Specify the expected number of notifications and end rules with
243 * {@link #setParameters}.</li>
244 * <li>Send the commands to LTTng (using {@link ILttngSession} for example.
246 * <li>Call {@link #waitForAllNotifications()}.</li>
247 * <li>Verify that {@link #checkRules()} returns true.</li>
251 private static class TestFilterListener
implements IFilterChangeListener
{
253 private final Set
<EventRule
> currentRules
= new HashSet
<>();
254 private CountDownLatch remainingExpectedNotifs
;
255 private Set
<EventRule
> expectedRules
;
257 public TestFilterListener() {}
260 public void eventRuleAdded(EventRule rule
) {
261 currentRules
.add(rule
);
262 remainingExpectedNotifs
.countDown();
266 public void eventRuleRemoved(EventRule rule
) {
267 currentRules
.remove(rule
);
268 remainingExpectedNotifs
.countDown();
271 public void setParameters(int expectedNotifications
, Set
<EventRule
> expectedRulesAtEnd
) {
272 this.remainingExpectedNotifs
= new CountDownLatch(expectedNotifications
);
273 this.expectedRules
= expectedRulesAtEnd
;
276 public boolean waitForAllNotifications() {
277 System
.out
.println("Waiting for all notifications to arrive...");
279 return remainingExpectedNotifs
.await(10, TimeUnit
.SECONDS
);
280 } catch (InterruptedException e
) {
285 public boolean checkRules() {
286 return ((remainingExpectedNotifs
.getCount() == 0) && currentRules
.equals(expectedRules
));