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.events;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
25 import java.util.Arrays;
26 import java.util.List;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.lttng.tools.ILttngSession;
33 import org.lttng.tools.ILttngSession.Domain;
34 import org.lttng.ust.agent.ILttngHandler;
35 import org.lttng.ust.agent.utils.TestPrintRunner;
38 * Base abstract class to implement all sorts of integration tests verifying the
39 * presence of enabled events in resulting traces.
41 @RunWith(TestPrintRunner.class)
42 public abstract class EnabledEventsITBase {
44 protected static final String EVENT_NAME_A = "EventA";
45 protected static final String EVENT_NAME_B = "EventAB";
46 protected static final String EVENT_NAME_C = "EventABC";
47 protected static final String EVENT_NAME_D = "EventABCD";
49 private ILttngSession session;
51 /* Fields defined by the sub-class */
52 protected ILttngHandler handlerA;
53 protected ILttngHandler handlerB;
54 protected ILttngHandler handlerC;
56 protected abstract Domain getDomain();
58 protected abstract void sendEventsToLoggers();
61 * Send one event using a localized API to logger/handler A.
63 protected abstract void sendLocalizedEvent(String rawString, Object[] params);
69 public void testSetup() {
70 session = ILttngSession.createSession(null, getDomain());
77 public void testTeardown() {
90 * Test sending events on the Java side, but no events enabled in the
91 * tracing session. There should be nothing in the resulting trace, and
92 * handlers should not have logged anything.
95 public void testNoEvents() {
96 assertTrue(session.start());
98 sendEventsToLoggers();
100 assertTrue(session.stop());
102 List<String> output = session.view();
103 assertNotNull(output);
104 assertTrue(output.isEmpty());
106 assertEquals(0, handlerA.getEventCount());
107 assertEquals(0, handlerB.getEventCount());
108 assertEquals(0, handlerC.getEventCount());
112 * Test sending events on the Java side, and all events enabled in the
113 * tracing session. All handlers should have sent their events.
116 public void testAllEvents() {
117 assertTrue(session.enableAllEvents());
118 assertTrue(session.start());
120 sendEventsToLoggers();
122 assertTrue(session.stop());
124 List<String> output = session.view();
125 assertNotNull(output);
126 assertEquals(30, output.size()); // loggerD has no handler attached
128 assertEquals(10, handlerA.getEventCount());
129 assertEquals(10, handlerB.getEventCount());
130 assertEquals(10, handlerC.getEventCount());
134 * Test sending events on the Java side, with only some of them enabled in
135 * the tracing session. Only the subset that is enabled should be received.
138 public void testSomeEvents() {
139 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D));
140 assertTrue(session.start());
142 sendEventsToLoggers();
144 assertTrue(session.stop());
146 List<String> output = session.view();
147 assertNotNull(output);
148 assertEquals(20, output.size());
150 assertEquals(10, handlerA.getEventCount());
151 assertEquals(0, handlerB.getEventCount());
152 assertEquals(10, handlerC.getEventCount());
156 * Test with all events enabled (-a), plus some other events added manually.
157 * Events should still be retained, but there should be no duplicates.
160 public void testAllEventsAndSome() {
161 assertTrue(session.enableAllEvents());
162 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B));
163 assertTrue(session.start());
165 sendEventsToLoggers();
167 assertTrue(session.stop());
169 List<String> output = session.view();
170 assertNotNull(output);
171 assertEquals(30, output.size());
173 assertEquals(10, handlerA.getEventCount());
174 assertEquals(10, handlerB.getEventCount());
175 assertEquals(10, handlerC.getEventCount());
179 * Same as {@link #testSomeEvents()}, but some events were enabled first,
180 * then disabled. Makes sure the enabled-event refcounting works properly.
183 public void testSomeEventsAfterDisabling() {
184 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D));
185 assertTrue(session.disableEvents(EVENT_NAME_C));
186 assertTrue(session.start());
188 sendEventsToLoggers();
190 assertTrue(session.stop());
192 List<String> output = session.view();
193 assertNotNull(output);
194 assertEquals(10, output.size());
196 assertEquals(10, handlerA.getEventCount());
197 assertEquals(0, handlerB.getEventCount());
198 assertEquals(0, handlerC.getEventCount());
202 * Test enabling an event prefix, which means an event name ending with a *,
203 * to match all events starting with this name.
206 public void testEventPrefix() {
207 // should match event/loggers B and C, but not A.
208 assertTrue(session.enableEvents("EventAB*"));
209 assertTrue(session.start());
211 sendEventsToLoggers();
213 assertTrue(session.stop());
215 List<String> output = session.view();
216 assertNotNull(output);
217 assertEquals(20, output.size());
219 assertEquals(0, handlerA.getEventCount());
220 assertEquals(10, handlerB.getEventCount());
221 assertEquals(10, handlerC.getEventCount());
225 * Same as {@link #testEventPrefix()}, but with multiple prefixes that
226 * overlap. There should not be any duplicate events in the trace or in the
230 public void testEventPrefixOverlapping() {
231 // should still match B and C
232 assertTrue(session.enableEvents("EventAB*", "EventABC*"));
233 assertTrue(session.start());
235 sendEventsToLoggers();
237 assertTrue(session.stop());
239 List<String> output = session.view();
240 assertNotNull(output);
241 assertEquals(20, output.size());
243 assertEquals(0, handlerA.getEventCount());
244 assertEquals(10, handlerB.getEventCount());
245 assertEquals(10, handlerC.getEventCount());
249 * Test with all events enabled (-a), plus an event prefix. Once again,
250 * there should be no duplicates.
253 public void testAllEventsAndPrefix() {
254 assertTrue(session.enableAllEvents());
255 assertTrue(session.enableEvents("EventAB*"));
256 assertTrue(session.start());
258 sendEventsToLoggers();
260 assertTrue(session.stop());
262 List<String> output = session.view();
263 assertNotNull(output);
264 assertEquals(30, output.size());
266 assertEquals(10, handlerA.getEventCount());
267 assertEquals(10, handlerB.getEventCount());
268 assertEquals(10, handlerC.getEventCount());
272 * Test sending a localized message.
275 public void testLocalizedMessage() {
276 Integer value1 = Integer.valueOf(10);
277 List<Integer> value2 = Arrays.asList(Integer.valueOf(1000), Integer.valueOf(1001), Integer.valueOf(1002));
279 assertTrue(session.enableAllEvents());
280 assertTrue(session.start());
282 sendLocalizedEvent("Message with a localized value: {0} and some others: {1}",
283 new Object[] { value1, value2 });
285 assertTrue(session.stop());
286 assertEquals(1, handlerA.getEventCount());
288 List<String> output = session.view();
290 assertNotNull(output);
291 assertEquals(1, output.size());
292 assertTrue(output.get(0).contains("msg = \"Message with a localized value: 10 and some others: [1000, 1001, 1002]\""));