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
.client
;
21 import static org
.junit
.Assert
.assertEquals
;
22 import static org
.junit
.Assert
.assertTrue
;
23 import static org
.junit
.Assume
.assumeTrue
;
25 import java
.util
.ArrayList
;
26 import java
.util
.Arrays
;
27 import java
.util
.Collections
;
28 import java
.util
.List
;
30 import org
.junit
.After
;
31 import org
.junit
.AfterClass
;
32 import org
.junit
.Before
;
33 import org
.junit
.BeforeClass
;
34 import org
.junit
.Ignore
;
35 import org
.junit
.Test
;
36 import org
.lttng
.tools
.ILttngSession
;
37 import org
.lttng
.tools
.LttngToolsHelper
;
38 import org
.lttng
.ust
.agent
.ILttngAgent
;
39 import org
.lttng
.ust
.agent
.client
.LttngTcpSessiondClient
;
40 import org
.lttng
.ust
.agent
.session
.EventRule
;
41 import org
.lttng
.ust
.agent
.session
.LogLevelSelector
;
42 import org
.lttng
.ust
.agent
.session
.LogLevelSelector
.LogLevelType
;
43 import org
.lttng
.ust
.agent
.utils
.ILogLevelStrings
;
46 * Tests for the TCP client only, without using an agent.
48 * This test suite requires that a *root* session daemon is running on the
49 * system. Since we have to explicitly tell the TCP client which sessiond to
50 * connect to, we have to hard-code it in here.
52 * @author Alexandre Montplaisir
54 public class TcpClientIT
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 private static final LogLevelSelector LOG_LEVEL_UNSPECIFIED
= new LogLevelSelector(Integer
.MIN_VALUE
, 0);
62 private static final String EVENT_NAME_A
= "eventA";
63 private static final String EVENT_NAME_B
= "eventB";
64 private static final String EVENT_NAME_C
= "eventC";
65 private static final String EVENT_NAME_ALL
= "*";
67 /* Test configuration */
68 private static final int DOMAIN_VALUE
= ILttngAgent
.Domain
.JUL
.value();
69 private static final ILttngSession
.Domain SESSION_DOMAIN
= ILttngSession
.Domain
.JUL
;
70 private static final boolean ROOT_SESSIOND
= true;
72 private static TcpClientDebugListener clientListener
;
73 private static LttngTcpSessiondClient client
;
74 private static Thread clientThread
;
76 private ILttngSession session
;
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
86 public static void setupClass() {
87 LttngToolsHelper
.destroyAllSessions();
89 clientListener
= new TcpClientDebugListener();
90 client
= new LttngTcpSessiondClient(clientListener
, DOMAIN_VALUE
, ROOT_SESSIOND
);
92 clientThread
= new Thread(client
);
95 assumeTrue("Timed out waiting for root sessiond", client
.waitForConnection(5));
102 public static void teardownClass() {
103 if (client
!= null) {
106 if (clientThread
!= null) {
109 } catch (InterruptedException e
) {
118 public void setup() {
119 session
= ILttngSession
.createSession(null, SESSION_DOMAIN
);
120 clientListener
.clearAllCommands();
127 public void teardown() {
132 private static ILogLevelStrings
getLogLevelStrings() {
133 return ILogLevelStrings
.JUL_LOGLEVEL_STRINGS
;
137 * Check that two lists contain the exact same element (including
138 * duplicates), but their order does not matter.
140 private static <T
extends Comparable
<T
>> boolean containSameElements(List
<T
> list1
, List
<T
> list2
) {
141 List
<T
> newlist1
= new ArrayList
<>(list1
);
142 List
<T
> newlist2
= new ArrayList
<>(list2
);
143 Collections
.sort(newlist1
);
144 Collections
.sort(newlist2
);
145 return (newlist1
.equals(newlist2
));
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
154 * Test enabling one event.
157 public void testEnableEvent() {
158 session
.enableEvent(EVENT_NAME_A
, null, false, null);
160 List
<EventRule
> expectedCommands
= Collections
.singletonList(
161 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, null));
163 List
<EventRule
> actualCommands
= clientListener
.getEnabledEventCommands();
164 assertEquals(expectedCommands
, actualCommands
);
168 * Test an "enable-event -a" command.
171 public void testEnableAllEvents() {
172 session
.enableAllEvents();
174 List
<EventRule
> expectedCommands
= Collections
.singletonList(
175 new EventRule(EVENT_NAME_ALL
, LOG_LEVEL_UNSPECIFIED
, null));
176 List
<EventRule
> actualCommands
= clientListener
.getEnabledEventCommands();
178 assertEquals(expectedCommands
, actualCommands
);
182 * Test enabling then disabling one event.
185 public void testEnableThenDisableOneEvent() {
186 session
.enableEvent(EVENT_NAME_A
, null, false, null);
187 session
.disableEvents(EVENT_NAME_A
);
189 List
<EventRule
> expectedEnableCommands
= Collections
.singletonList(
190 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, null));
191 List
<String
> expectedDisableCommands
= Collections
.singletonList(EVENT_NAME_A
);
193 assertEquals(expectedEnableCommands
, clientListener
.getEnabledEventCommands());
194 assertTrue(containSameElements(expectedDisableCommands
, clientListener
.getDisabledEventCommands()));
198 * Test enabling some events manually, then disabling all events (-a).
201 public void testEnableSomeThenDisableAll() {
202 session
.enableEvent(EVENT_NAME_A
, null, false, null);
203 session
.enableEvent(EVENT_NAME_B
, null, false, null);
204 session
.enableEvent(EVENT_NAME_C
, null, false, null);
205 session
.disableAllEvents();
207 List
<EventRule
> expectedEnableCommands
= Arrays
.asList(
208 new EventRule(EVENT_NAME_A
, LOG_LEVEL_UNSPECIFIED
, null),
209 new EventRule(EVENT_NAME_B
, LOG_LEVEL_UNSPECIFIED
, null),
210 new EventRule(EVENT_NAME_C
, LOG_LEVEL_UNSPECIFIED
, null));
212 * A "disable-event -a" will send one command for each enabled event.
213 * The order may be different though.
215 List
<String
> expectedDisableCommands
= Arrays
.asList(
216 EVENT_NAME_A
, EVENT_NAME_B
, EVENT_NAME_C
);
218 assertEquals(expectedEnableCommands
, clientListener
.getEnabledEventCommands());
219 assertTrue(containSameElements(expectedDisableCommands
, clientListener
.getDisabledEventCommands()));
223 * Test enabling then (enable-event -a) then disabling all (disable-event -a) events.
226 public void testEnableAllThenDisableAll() {
227 session
.enableAllEvents();
228 session
.disableAllEvents();
230 List
<EventRule
> expectedEnableCommands
= Arrays
.asList(
231 new EventRule(EVENT_NAME_ALL
, LOG_LEVEL_UNSPECIFIED
, null));
232 List
<String
> expectedDisableCommands
= Arrays
.asList(
235 assertEquals(expectedEnableCommands
, clientListener
.getEnabledEventCommands());
236 assertTrue(containSameElements(expectedDisableCommands
, clientListener
.getDisabledEventCommands()));
240 * Test specifying an event with a --loglevel option.
243 public void testEnableEventLogLevelRange() {
244 LogLevelSelector lls
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_RANGE
);
246 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), false, null);
248 List
<EventRule
> expectedCommands
= Collections
.singletonList(
249 new EventRule(EVENT_NAME_A
, lls
, null));
250 List
<EventRule
> actualCommands
= clientListener
.getEnabledEventCommands();
252 assertEquals(expectedCommands
, actualCommands
);
256 * Test enabling an event with a --loglevel-only option.
259 public void testEnableEventLogLevelSingle() {
260 LogLevelSelector lls
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_SINGLE
);
262 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), true, null);
264 List
<EventRule
> expectedCommands
= Collections
.singletonList(
265 new EventRule(EVENT_NAME_A
, lls
, null));
266 List
<EventRule
> actualCommands
= clientListener
.getEnabledEventCommands();
268 assertEquals(expectedCommands
, actualCommands
);
272 * Test enabling an event twice, for the same loglevel, with --loglevel followed by --loglevel-only.
274 @Ignore("See http://bugs.lttng.org/issues/913")
276 public void testEnableEventsLogLevelRangeAndSingle() {
277 LogLevelSelector lls1
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_RANGE
);
278 LogLevelSelector lls2
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_SINGLE
);
280 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), false, null);
281 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), true, null);
283 List
<EventRule
> expectedCommands
= Arrays
.asList(
284 new EventRule(EVENT_NAME_A
, lls1
, null),
285 new EventRule(EVENT_NAME_A
, lls2
, null)
287 List
<EventRule
> actualCommands
= clientListener
.getEnabledEventCommands();
289 assertEquals(expectedCommands
, actualCommands
);
293 * Test enabling an event twice, for the same loglevel, with --loglevel--only followed by --loglevel.
295 @Ignore("See http://bugs.lttng.org/issues/913")
297 public void testEnableEventsLogLevelSingleAndRange() {
298 LogLevelSelector lls1
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_SINGLE
);
299 LogLevelSelector lls2
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_RANGE
);
301 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), true, null);
302 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), false, null);
304 List
<EventRule
> expectedCommands
= Arrays
.asList(
305 new EventRule(EVENT_NAME_A
, lls1
, null),
306 new EventRule(EVENT_NAME_A
, lls2
, null)
308 List
<EventRule
> actualCommands
= clientListener
.getEnabledEventCommands();
310 assertEquals(expectedCommands
, actualCommands
);
314 * Test enabling the same event, same loglevel, but different loglevel types
315 * (--loglevel vs --loglevel-only) in two separate sessions.
318 public void testEnableEventsLogLevelRangeAndSingleDiffSessions() {
319 try (ILttngSession session2
= ILttngSession
.createSession(null, SESSION_DOMAIN
);) {
321 LogLevelSelector lls1
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_RANGE
);
322 LogLevelSelector lls2
= new LogLevelSelector(getLogLevelStrings().warningInt(), LogLevelType
.LTTNG_EVENT_LOGLEVEL_SINGLE
);
324 session
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), false, null);
325 session2
.enableEvent(EVENT_NAME_A
, getLogLevelStrings().warningName(), true, null);
327 List
<EventRule
> expectedCommands
= Arrays
.asList(new EventRule(EVENT_NAME_A
, lls1
, null),
328 new EventRule(EVENT_NAME_A
, lls2
, null));
329 List
<EventRule
> actualCommands
= clientListener
.getEnabledEventCommands();
331 assertEquals(expectedCommands
, actualCommands
);