Commit | Line | Data |
---|---|---|
2b408e85 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 | ||
0e77c728 | 19 | package org.lttng.ust.agent.integration.events; |
c5524c71 AM |
20 | |
21 | import static org.junit.Assert.assertEquals; | |
22 | import static org.junit.Assert.assertNotNull; | |
23 | import static org.junit.Assert.assertTrue; | |
24 | ||
3fa81377 | 25 | import java.util.Arrays; |
c5524c71 AM |
26 | import java.util.List; |
27 | ||
28 | import org.junit.After; | |
29 | import org.junit.Before; | |
30 | import org.junit.Test; | |
64c5b50c | 31 | import org.junit.runner.RunWith; |
45d1768c AM |
32 | import org.lttng.tools.ILttngSession; |
33 | import org.lttng.tools.ILttngSession.Domain; | |
c5524c71 | 34 | import org.lttng.ust.agent.ILttngHandler; |
64c5b50c | 35 | import org.lttng.ust.agent.utils.TestPrintRunner; |
c5524c71 | 36 | |
8a0613fa AM |
37 | /** |
38 | * Base abstract class to implement all sorts of integration tests verifying the | |
39 | * presence of enabled events in resulting traces. | |
40 | */ | |
64c5b50c | 41 | @RunWith(TestPrintRunner.class) |
7b82be36 | 42 | public abstract class EnabledEventsITBase { |
c5524c71 AM |
43 | |
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"; | |
6c2b24d5 | 47 | protected static final String EVENT_NAME_D = "EventABCDÉ"; |
c5524c71 | 48 | |
45d1768c | 49 | private ILttngSession session; |
8576633f | 50 | |
c5524c71 AM |
51 | /* Fields defined by the sub-class */ |
52 | protected ILttngHandler handlerA; | |
53 | protected ILttngHandler handlerB; | |
54 | protected ILttngHandler handlerC; | |
55 | ||
56 | protected abstract Domain getDomain(); | |
57 | ||
58 | protected abstract void sendEventsToLoggers(); | |
59 | ||
3fa81377 AM |
60 | /** |
61 | * Send one event using a localized API to logger/handler A. | |
62 | */ | |
63 | protected abstract void sendLocalizedEvent(String rawString, Object[] params); | |
64 | ||
8a0613fa AM |
65 | /** |
66 | * Base test setup | |
67 | */ | |
c5524c71 AM |
68 | @Before |
69 | public void testSetup() { | |
ff620bef | 70 | session = ILttngSession.createSession(null, getDomain()); |
c5524c71 AM |
71 | } |
72 | ||
8a0613fa AM |
73 | /** |
74 | * Base test teardown | |
75 | */ | |
c5524c71 AM |
76 | @After |
77 | public void testTeardown() { | |
8576633f | 78 | session.close(); |
c5524c71 AM |
79 | |
80 | handlerA.close(); | |
81 | handlerB.close(); | |
82 | handlerC.close(); | |
83 | ||
84 | handlerA = null; | |
85 | handlerB = null; | |
86 | handlerC = null; | |
87 | } | |
88 | ||
89 | /** | |
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. | |
93 | */ | |
94 | @Test | |
95 | public void testNoEvents() { | |
8576633f | 96 | assertTrue(session.start()); |
c5524c71 AM |
97 | |
98 | sendEventsToLoggers(); | |
99 | ||
8576633f | 100 | assertTrue(session.stop()); |
c5524c71 | 101 | |
8576633f | 102 | List<String> output = session.view(); |
c5524c71 AM |
103 | assertNotNull(output); |
104 | assertTrue(output.isEmpty()); | |
105 | ||
c5524c71 AM |
106 | assertEquals(0, handlerA.getEventCount()); |
107 | assertEquals(0, handlerB.getEventCount()); | |
108 | assertEquals(0, handlerC.getEventCount()); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Test sending events on the Java side, and all events enabled in the | |
113 | * tracing session. All handlers should have sent their events. | |
114 | */ | |
115 | @Test | |
116 | public void testAllEvents() { | |
8576633f AM |
117 | assertTrue(session.enableAllEvents()); |
118 | assertTrue(session.start()); | |
c5524c71 AM |
119 | |
120 | sendEventsToLoggers(); | |
121 | ||
8576633f | 122 | assertTrue(session.stop()); |
c5524c71 | 123 | |
8576633f | 124 | List<String> output = session.view(); |
c5524c71 AM |
125 | assertNotNull(output); |
126 | assertEquals(30, output.size()); // loggerD has no handler attached | |
127 | ||
c5524c71 AM |
128 | assertEquals(10, handlerA.getEventCount()); |
129 | assertEquals(10, handlerB.getEventCount()); | |
130 | assertEquals(10, handlerC.getEventCount()); | |
131 | } | |
132 | ||
133 | /** | |
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. | |
136 | */ | |
137 | @Test | |
138 | public void testSomeEvents() { | |
8576633f AM |
139 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
140 | assertTrue(session.start()); | |
c5524c71 AM |
141 | |
142 | sendEventsToLoggers(); | |
143 | ||
8576633f | 144 | assertTrue(session.stop()); |
c5524c71 | 145 | |
8576633f | 146 | List<String> output = session.view(); |
c5524c71 AM |
147 | assertNotNull(output); |
148 | assertEquals(20, output.size()); | |
149 | ||
c5524c71 AM |
150 | assertEquals(10, handlerA.getEventCount()); |
151 | assertEquals(0, handlerB.getEventCount()); | |
152 | assertEquals(10, handlerC.getEventCount()); | |
153 | } | |
154 | ||
155 | /** | |
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. | |
158 | */ | |
159 | @Test | |
160 | public void testAllEventsAndSome() { | |
8576633f AM |
161 | assertTrue(session.enableAllEvents()); |
162 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B)); | |
163 | assertTrue(session.start()); | |
c5524c71 AM |
164 | |
165 | sendEventsToLoggers(); | |
166 | ||
8576633f | 167 | assertTrue(session.stop()); |
c5524c71 | 168 | |
8576633f | 169 | List<String> output = session.view(); |
c5524c71 AM |
170 | assertNotNull(output); |
171 | assertEquals(30, output.size()); | |
172 | ||
c5524c71 AM |
173 | assertEquals(10, handlerA.getEventCount()); |
174 | assertEquals(10, handlerB.getEventCount()); | |
175 | assertEquals(10, handlerC.getEventCount()); | |
176 | } | |
177 | ||
178 | /** | |
179 | * Same as {@link #testSomeEvents()}, but some events were enabled first, | |
180 | * then disabled. Makes sure the enabled-event refcounting works properly. | |
181 | */ | |
182 | @Test | |
183 | public void testSomeEventsAfterDisabling() { | |
8576633f AM |
184 | assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D)); |
185 | assertTrue(session.disableEvents(EVENT_NAME_C)); | |
186 | assertTrue(session.start()); | |
c5524c71 AM |
187 | |
188 | sendEventsToLoggers(); | |
189 | ||
8576633f | 190 | assertTrue(session.stop()); |
c5524c71 | 191 | |
8576633f | 192 | List<String> output = session.view(); |
c5524c71 AM |
193 | assertNotNull(output); |
194 | assertEquals(10, output.size()); | |
195 | ||
c5524c71 AM |
196 | assertEquals(10, handlerA.getEventCount()); |
197 | assertEquals(0, handlerB.getEventCount()); | |
198 | assertEquals(0, handlerC.getEventCount()); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Test enabling an event prefix, which means an event name ending with a *, | |
203 | * to match all events starting with this name. | |
204 | */ | |
205 | @Test | |
206 | public void testEventPrefix() { | |
8576633f AM |
207 | // should match event/loggers B and C, but not A. |
208 | assertTrue(session.enableEvents("EventAB*")); | |
209 | assertTrue(session.start()); | |
c5524c71 AM |
210 | |
211 | sendEventsToLoggers(); | |
212 | ||
8576633f | 213 | assertTrue(session.stop()); |
c5524c71 | 214 | |
8576633f | 215 | List<String> output = session.view(); |
c5524c71 AM |
216 | assertNotNull(output); |
217 | assertEquals(20, output.size()); | |
218 | ||
c5524c71 AM |
219 | assertEquals(0, handlerA.getEventCount()); |
220 | assertEquals(10, handlerB.getEventCount()); | |
221 | assertEquals(10, handlerC.getEventCount()); | |
222 | } | |
223 | ||
224 | /** | |
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 | |
227 | * handlers. | |
228 | */ | |
229 | @Test | |
230 | public void testEventPrefixOverlapping() { | |
8576633f AM |
231 | // should still match B and C |
232 | assertTrue(session.enableEvents("EventAB*", "EventABC*")); | |
233 | assertTrue(session.start()); | |
c5524c71 AM |
234 | |
235 | sendEventsToLoggers(); | |
236 | ||
8576633f | 237 | assertTrue(session.stop()); |
c5524c71 | 238 | |
8576633f | 239 | List<String> output = session.view(); |
c5524c71 AM |
240 | assertNotNull(output); |
241 | assertEquals(20, output.size()); | |
242 | ||
c5524c71 AM |
243 | assertEquals(0, handlerA.getEventCount()); |
244 | assertEquals(10, handlerB.getEventCount()); | |
245 | assertEquals(10, handlerC.getEventCount()); | |
246 | } | |
247 | ||
248 | /** | |
249 | * Test with all events enabled (-a), plus an event prefix. Once again, | |
250 | * there should be no duplicates. | |
251 | */ | |
252 | @Test | |
253 | public void testAllEventsAndPrefix() { | |
8576633f AM |
254 | assertTrue(session.enableAllEvents()); |
255 | assertTrue(session.enableEvents("EventAB*")); | |
256 | assertTrue(session.start()); | |
c5524c71 AM |
257 | |
258 | sendEventsToLoggers(); | |
259 | ||
8576633f | 260 | assertTrue(session.stop()); |
c5524c71 | 261 | |
8576633f | 262 | List<String> output = session.view(); |
c5524c71 AM |
263 | assertNotNull(output); |
264 | assertEquals(30, output.size()); | |
265 | ||
c5524c71 AM |
266 | assertEquals(10, handlerA.getEventCount()); |
267 | assertEquals(10, handlerB.getEventCount()); | |
268 | assertEquals(10, handlerC.getEventCount()); | |
269 | } | |
3fa81377 AM |
270 | |
271 | /** | |
272 | * Test sending a localized message. | |
273 | */ | |
274 | @Test | |
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)); | |
278 | ||
279 | assertTrue(session.enableAllEvents()); | |
280 | assertTrue(session.start()); | |
281 | ||
282 | sendLocalizedEvent("Message with a localized value: {0} and some others: {1}", | |
283 | new Object[] { value1, value2 }); | |
284 | ||
285 | assertTrue(session.stop()); | |
286 | assertEquals(1, handlerA.getEventCount()); | |
287 | ||
288 | List<String> output = session.view(); | |
289 | ||
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]\"")); | |
293 | } | |
c5524c71 | 294 | } |