e7c537ce5cd8bfb0b5f98836c6f33a166c631817
[lttng-ust-java-tests.git] / lttng-ust-java-tests / src / test / 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.Assert.assertTrue;
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.concurrent.CountDownLatch;
28 import java.util.concurrent.TimeUnit;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31
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;
46
47 /**
48 * Base test class for {@link IFilterChangeListener} tests.
49 *
50 * @author Alexandre Montplaisir
51 */
52 @RunWith(TestPrintRunner.class)
53 public abstract class FilterListenerITBase {
54
55 protected static final LogLevelFilter LOG_LEVEL_UNSPECIFIED = new LogLevelFilter(Integer.MIN_VALUE, 0);
56
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";
60
61 private ILttngSession session;
62 private TestFilterListener listener;
63 private ILttngHandler handler;
64
65 protected abstract ILttngSession.Domain getSessionDomain();
66 protected abstract ILttngHandler getLogHandler() throws SecurityException, IOException;
67 protected abstract ILogLevelStrings getLogLevelStrings();
68
69 /**
70 * Test setup
71 *
72 * @throws SecurityException
73 * @throws IOException
74 */
75 @Before
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());
81 }
82
83 /**
84 * Test teardown
85 */
86 @After
87 public void teardown() {
88 session.close();
89 FilterNotificationManager.getInstance().unregisterListener(listener);
90 handler.close();
91 }
92
93 /**
94 * Test not sending any commands.
95 */
96 @Test
97 public void testNoRules() {
98 Set<EventRule> rules = Collections.EMPTY_SET;
99 listener.setParameters(0, rules);
100 /* Don't enable any events */
101
102 assertTrue(listener.waitForAllNotifications());
103 assertTrue(listener.checkRules());
104 }
105
106 /**
107 * Test sending one event rule.
108 */
109 @Test
110 public void testOneRule() {
111 Set<EventRule> rules = Collections.singleton(
112 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null));
113
114 listener.setParameters(1, rules);
115
116 session.enableEvent(EVENT_NAME_A, null, false, null);
117
118 assertTrue(listener.waitForAllNotifications());
119 assertTrue(listener.checkRules());
120 }
121
122 /**
123 * Test sending many event rules.
124 */
125 @Test
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());
132
133 listener.setParameters(3, rules);
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 assertTrue(listener.waitForAllNotifications());
140 assertTrue(listener.checkRules());
141 }
142
143 /**
144 * Test enabling then disabling some events.
145 */
146 @Test
147 public void testManyRulesDisableSome() {
148 Set<EventRule> rules = Collections.singleton(
149 new EventRule(EVENT_NAME_A, LOG_LEVEL_UNSPECIFIED, null));
150
151 listener.setParameters(4, rules);
152
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);
158
159 assertTrue(listener.waitForAllNotifications());
160 assertTrue(listener.checkRules());
161 }
162
163 /**
164 * Test enabling some rules, then calling disable-event -a.
165 */
166 @Test
167 public void testManyRulesDisableAll() {
168 Set<EventRule> rules = Collections.EMPTY_SET;
169
170 /*
171 * We should receive 6 notifications, because a "disable-event -a" sends
172 * one for each event that was enabled.
173 */
174 listener.setParameters(6, rules);
175
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();
180
181 assertTrue(listener.waitForAllNotifications());
182 assertTrue(listener.checkRules());
183 }
184
185 /**
186 * Test enabling the same event name with various values of loglevels.
187 */
188 @Ignore("Does not work as expected atm, see http://bugs.lttng.org/issues/913")
189 @Test
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);
194
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());
200
201 listener.setParameters(3, rules);
202
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);
206
207 assertTrue(listener.waitForAllNotifications());
208 assertTrue(listener.checkRules());
209 }
210
211 /**
212 * Test enabling the same event name with various filters.
213 */
214 @Ignore("Filters are not tracked yet")
215 @Test
216 public void testSameEventsDiffFilters() {
217 String filterA = "filterA";
218 String filterB = "filterB";
219
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());
225
226 listener.setParameters(3, rules);
227
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);
231
232 assertTrue(listener.waitForAllNotifications());
233 assertTrue(listener.checkRules());
234 }
235
236 /**
237 * The filter listener used for tests.
238 *
239 * <p>
240 * Usage:
241 * <ul>
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.
245 * </li>
246 * <li>Call {@link #waitForAllNotifications()}.</li>
247 * <li>Verify that {@link #checkRules()} returns true.</li>
248 * </ul>
249 * </p>
250 */
251 private static class TestFilterListener implements IFilterChangeListener {
252
253 private final Set<EventRule> currentRules = new HashSet<>();
254 private CountDownLatch remainingExpectedNotifs;
255 private Set<EventRule> expectedRules;
256
257 public TestFilterListener() {}
258
259 @Override
260 public void eventRuleAdded(EventRule rule) {
261 currentRules.add(rule);
262 remainingExpectedNotifs.countDown();
263 }
264
265 @Override
266 public void eventRuleRemoved(EventRule rule) {
267 currentRules.remove(rule);
268 remainingExpectedNotifs.countDown();
269 }
270
271 public void setParameters(int expectedNotifications, Set<EventRule> expectedRulesAtEnd) {
272 this.remainingExpectedNotifs = new CountDownLatch(expectedNotifications);
273 this.expectedRules = expectedRulesAtEnd;
274 }
275
276 public boolean waitForAllNotifications() {
277 System.out.println("Waiting for all notifications to arrive...");
278 try {
279 return remainingExpectedNotifs.await(10, TimeUnit.SECONDS);
280 } catch (InterruptedException e) {
281 return false;
282 }
283 }
284
285 public boolean checkRules() {
286 return ((remainingExpectedNotifs.getCount() == 0) && currentRules.equals(expectedRules));
287 }
288 }
289
290 }
This page took 0.037746 seconds and 3 git commands to generate.