Add copyright header to all source files
[lttng-ust-java-tests.git] / src / test / java / org / lttng / ust / agent / integration / EnabledEventsTestBase.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;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.List;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.lttng.ust.agent.ILttngHandler;
31 import org.lttng.ust.agent.utils.LttngSession;
32 import org.lttng.ust.agent.utils.LttngSession.Domain;
33
34 public abstract class EnabledEventsTestBase {
35
36 protected static final String EVENT_NAME_A = "EventA";
37 protected static final String EVENT_NAME_B = "EventAB";
38 protected static final String EVENT_NAME_C = "EventABC";
39 protected static final String EVENT_NAME_D = "EventABCD";
40
41 private LttngSession session;
42
43 /* Fields defined by the sub-class */
44 protected ILttngHandler handlerA;
45 protected ILttngHandler handlerB;
46 protected ILttngHandler handlerC;
47
48 protected abstract Domain getDomain();
49
50 protected abstract void sendEventsToLoggers();
51
52 @Before
53 public void testSetup() {
54 session = new LttngSession(null, getDomain());
55 }
56
57 @After
58 public void testTeardown() {
59 session.close();
60
61 handlerA.close();
62 handlerB.close();
63 handlerC.close();
64
65 handlerA = null;
66 handlerB = null;
67 handlerC = null;
68 }
69
70 /**
71 * Test sending events on the Java side, but no events enabled in the
72 * tracing session. There should be nothing in the resulting trace, and
73 * handlers should not have logged anything.
74 */
75 @Test
76 public void testNoEvents() {
77 assertTrue(session.start());
78
79 sendEventsToLoggers();
80
81 assertTrue(session.stop());
82
83 List<String> output = session.view();
84 assertNotNull(output);
85 assertTrue(output.isEmpty());
86
87 assertEquals(0, handlerA.getEventCount());
88 assertEquals(0, handlerB.getEventCount());
89 assertEquals(0, handlerC.getEventCount());
90 }
91
92 /**
93 * Test sending events on the Java side, and all events enabled in the
94 * tracing session. All handlers should have sent their events.
95 */
96 @Test
97 public void testAllEvents() {
98 assertTrue(session.enableAllEvents());
99 assertTrue(session.start());
100
101 sendEventsToLoggers();
102
103 assertTrue(session.stop());
104
105 List<String> output = session.view();
106 assertNotNull(output);
107 assertEquals(30, output.size()); // loggerD has no handler attached
108
109 assertEquals(10, handlerA.getEventCount());
110 assertEquals(10, handlerB.getEventCount());
111 assertEquals(10, handlerC.getEventCount());
112 }
113
114 /**
115 * Test sending events on the Java side, with only some of them enabled in
116 * the tracing session. Only the subset that is enabled should be received.
117 */
118 @Test
119 public void testSomeEvents() {
120 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D));
121 assertTrue(session.start());
122
123 sendEventsToLoggers();
124
125 assertTrue(session.stop());
126
127 List<String> output = session.view();
128 assertNotNull(output);
129 assertEquals(20, output.size());
130
131 assertEquals(10, handlerA.getEventCount());
132 assertEquals(0, handlerB.getEventCount());
133 assertEquals(10, handlerC.getEventCount());
134 }
135
136 /**
137 * Test with all events enabled (-a), plus some other events added manually.
138 * Events should still be retained, but there should be no duplicates.
139 */
140 @Test
141 public void testAllEventsAndSome() {
142 assertTrue(session.enableAllEvents());
143 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_B));
144 assertTrue(session.start());
145
146 sendEventsToLoggers();
147
148 assertTrue(session.stop());
149
150 List<String> output = session.view();
151 assertNotNull(output);
152 assertEquals(30, output.size());
153
154 assertEquals(10, handlerA.getEventCount());
155 assertEquals(10, handlerB.getEventCount());
156 assertEquals(10, handlerC.getEventCount());
157 }
158
159 /**
160 * Same as {@link #testSomeEvents()}, but some events were enabled first,
161 * then disabled. Makes sure the enabled-event refcounting works properly.
162 */
163 @Test
164 public void testSomeEventsAfterDisabling() {
165 assertTrue(session.enableEvents(EVENT_NAME_A, EVENT_NAME_C, EVENT_NAME_D));
166 assertTrue(session.disableEvents(EVENT_NAME_C));
167 assertTrue(session.start());
168
169 sendEventsToLoggers();
170
171 assertTrue(session.stop());
172
173 List<String> output = session.view();
174 assertNotNull(output);
175 assertEquals(10, output.size());
176
177 assertEquals(10, handlerA.getEventCount());
178 assertEquals(0, handlerB.getEventCount());
179 assertEquals(0, handlerC.getEventCount());
180 }
181
182 /**
183 * Test enabling an event prefix, which means an event name ending with a *,
184 * to match all events starting with this name.
185 */
186 @Test
187 public void testEventPrefix() {
188 // should match event/loggers B and C, but not A.
189 assertTrue(session.enableEvents("EventAB*"));
190 assertTrue(session.start());
191
192 sendEventsToLoggers();
193
194 assertTrue(session.stop());
195
196 List<String> output = session.view();
197 assertNotNull(output);
198 assertEquals(20, output.size());
199
200 assertEquals(0, handlerA.getEventCount());
201 assertEquals(10, handlerB.getEventCount());
202 assertEquals(10, handlerC.getEventCount());
203 }
204
205 /**
206 * Same as {@link #testEventPrefix()}, but with multiple prefixes that
207 * overlap. There should not be any duplicate events in the trace or in the
208 * handlers.
209 */
210 @Test
211 public void testEventPrefixOverlapping() {
212 // should still match B and C
213 assertTrue(session.enableEvents("EventAB*", "EventABC*"));
214 assertTrue(session.start());
215
216 sendEventsToLoggers();
217
218 assertTrue(session.stop());
219
220 List<String> output = session.view();
221 assertNotNull(output);
222 assertEquals(20, output.size());
223
224 assertEquals(0, handlerA.getEventCount());
225 assertEquals(10, handlerB.getEventCount());
226 assertEquals(10, handlerC.getEventCount());
227 }
228
229 /**
230 * Test with all events enabled (-a), plus an event prefix. Once again,
231 * there should be no duplicates.
232 */
233 @Test
234 public void testAllEventsAndPrefix() {
235 assertTrue(session.enableAllEvents());
236 assertTrue(session.enableEvents("EventAB*"));
237 assertTrue(session.start());
238
239 sendEventsToLoggers();
240
241 assertTrue(session.stop());
242
243 List<String> output = session.view();
244 assertNotNull(output);
245 assertEquals(30, output.size());
246
247 assertEquals(10, handlerA.getEventCount());
248 assertEquals(10, handlerB.getEventCount());
249 assertEquals(10, handlerC.getEventCount());
250 }
251 }
This page took 0.040582 seconds and 4 git commands to generate.