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 traceRetriverName = RETRIEVER_NAME.replace('.', '_');
148 String traceContextName = CONTEXT_NAME.replace('.', '_');
150 String expectedString = "_app_" + traceRetriverName + "_" + traceContextName + " = { string = \"" + CONTEXT_VALUE + "\" } }";
151 output.forEach(line -> assertTrue(line.contains(expectedString)));
154 // ------------------------------------------------------------------------
156 // ------------------------------------------------------------------------
159 * Test the sequence Agent -> Retriever -> Session
162 public void testAgentRetrieverSession() {
165 enableContextInSessions();
167 traceSendEventsAndVerify();
171 * Test the sequence Agent -> Session -> Retriever
174 public void testAgentSessionRetriever() {
176 enableContextInSessions();
179 traceSendEventsAndVerify();
183 * Test the sequence Retriever -> Agent -> Session
186 public void testRetrieverAgentSession() {
189 enableContextInSessions();
191 traceSendEventsAndVerify();
195 * Test the sequence Retriever -> Session -> Agent
198 public void testRetrieverSessionAgent() {
201 enableContextInSessions();
203 traceSendEventsAndVerify();
207 * Test the sequence Session -> Agent -> Retriever
210 public void testSessionAgentRetriever() {
213 enableContextInSessions();
215 traceSendEventsAndVerify();
219 * Test the sequence Session -> Retriever -> Agent
222 public void testSessionRetrieverAgent() {
225 enableContextInSessions();
227 traceSendEventsAndVerify();