2 * Copyright (C) 2016, 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
.context
;
21 import static org
.junit
.Assert
.assertFalse
;
22 import static org
.junit
.Assert
.assertNotNull
;
23 import static org
.junit
.Assert
.assertTrue
;
24 import static org
.junit
.Assert
.fail
;
26 import java
.io
.IOException
;
27 import java
.util
.List
;
29 import org
.junit
.After
;
30 import org
.junit
.AfterClass
;
31 import org
.junit
.Before
;
32 import org
.junit
.Test
;
33 import org
.junit
.runner
.RunWith
;
34 import org
.lttng
.tools
.ILttngSession
;
35 import org
.lttng
.tools
.LttngToolsHelper
;
36 import org
.lttng
.tools
.ILttngSession
.Domain
;
37 import org
.lttng
.ust
.agent
.ILttngHandler
;
38 import org
.lttng
.ust
.agent
.context
.ContextInfoManager
;
39 import org
.lttng
.ust
.agent
.context
.IContextInfoRetriever
;
40 import org
.lttng
.ust
.agent
.utils
.TestPrintRunner
;
43 * To obtain application contexts in a trace, three steps are required:
46 * <li>Having the Java agent register to the sessiond (Agent)</li>
47 * <li>Registering the application-provided context info retriever (Retriever)</li>
48 * <li>Enabling the contexts in the tracing session (Session)</li>
51 * These three steps however can occur in any order ; this means there are 6
52 * possible cases. The goal of this class is to test all these cases.
54 @RunWith(TestPrintRunner
.class)
55 public abstract class AppContextOrderingITBase
{
57 protected static final String EVENT_NAME
= "EventName";
59 private static final IContextInfoRetriever RETRIEVER
= ContextInfoRetrieverStubs
.STRING_RETRIEVER
;
60 private static final String RETRIEVER_NAME
= "MyRetriever";
61 private static final String CONTEXT_NAME
= ContextInfoRetrieverStubs
.CONTEXT_NAME
;
62 private static final String CONTEXT_VALUE
= ContextInfoRetrieverStubs
.STRING_VALUE
;
64 protected ILttngHandler logHandler
;
66 private ContextInfoManager cim
;
67 private ILttngSession session
;
69 protected abstract Domain
getDomain();
70 protected abstract void sendEventsToLoggers();
76 public void testSetup() {
78 cim
= ContextInfoManager
.getInstance();
79 } catch (SecurityException
| IOException e
) {
80 /* The native library is not available! */
83 session
= ILttngSession
.createSession(null, getDomain());
90 public void testCleanup() {
92 assertTrue(cim
.unregisterContextInfoRetriever(RETRIEVER_NAME
));
99 public static void julClassCleanup() {
100 LttngToolsHelper
.deleteAllTraces();
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
108 * Instantiate the log handler for the corresponding logging API. This will
109 * also spawn the agent and have it register to the sessiond, so it
110 * corresponds to the "Agent" step.
112 * This method should set the 'logHandler' field accordingly.
114 protected abstract void registerAgent();
117 * Register the context info retriever to UST. This corresponds to the
120 private void registerRetriever() {
121 assertTrue(cim
.registerContextInfoRetriever(RETRIEVER_NAME
, RETRIEVER
));
125 * Enable the contexts in the tracing session. This corresponds to the "Session" step.
127 private void enableContextInSessions() {
128 assertTrue(session
.enableAllEvents());
129 assertTrue(session
.enableAppContext(RETRIEVER_NAME
, CONTEXT_NAME
));
133 * Start tracing, send events from the application, and verify that the
134 * output contains the expected context information.
136 * This should be called only after all 3 steps above are done.
138 private void traceSendEventsAndVerify() {
139 assertTrue(session
.start());
140 sendEventsToLoggers();
141 assertTrue(session
.stop());
143 List
<String
> output
= session
.view();
144 assertNotNull(output
);
145 assertFalse(output
.isEmpty());
147 String expectedString
= "_app_" + RETRIEVER_NAME
+ "_" + CONTEXT_NAME
+ " = { string = \"" + CONTEXT_VALUE
+ "\" } }";
148 output
.forEach(line
-> assertTrue(line
.contains(expectedString
)));
151 // ------------------------------------------------------------------------
153 // ------------------------------------------------------------------------
156 * Test the sequence Agent -> Retriever -> Session
159 public void testAgentRetrieverSession() {
162 enableContextInSessions();
164 traceSendEventsAndVerify();
168 * Test the sequence Agent -> Session -> Retriever
171 public void testAgentSessionRetriever() {
173 enableContextInSessions();
176 traceSendEventsAndVerify();
180 * Test the sequence Retriever -> Agent -> Session
183 public void testRetrieverAgentSession() {
186 enableContextInSessions();
188 traceSendEventsAndVerify();
192 * Test the sequence Retriever -> Session -> Agent
195 public void testRetrieverSessionAgent() {
198 enableContextInSessions();
200 traceSendEventsAndVerify();
204 * Test the sequence Session -> Agent -> Retriever
207 public void testSessionAgentRetriever() {
210 enableContextInSessions();
212 traceSendEventsAndVerify();
216 * Test the sequence Session -> Retriever -> Agent
219 public void testSessionRetrieverAgent() {
222 enableContextInSessions();
224 traceSendEventsAndVerify();