a2a038c7206d433445ae7fe3c80753d69bd4f1a7
[lttng-ust-java-tests.git] /
1 /*
2 * Copyright (C) 2016, 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.context;
20
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;
25
26 import java.io.IOException;
27 import java.util.List;
28
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;
41
42 /**
43 * To obtain application contexts in a trace, three steps are required:
44 *
45 * <ul>
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>
49 * </ul>
50 *
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.
53 */
54 @RunWith(TestPrintRunner.class)
55 public abstract class AppContextOrderingITBase {
56
57 protected static final String EVENT_NAME = "EventName";
58
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;
63
64 protected ILttngHandler logHandler;
65
66 private ContextInfoManager cim;
67 private ILttngSession session;
68
69 protected abstract Domain getDomain();
70 protected abstract void sendEventsToLoggers();
71
72 /**
73 * Base test setup
74 */
75 @Before
76 public void testSetup() {
77 try {
78 cim = ContextInfoManager.getInstance();
79 } catch (SecurityException | IOException e) {
80 /* The native library is not available! */
81 fail(e.getMessage());
82 }
83 session = ILttngSession.createSession(null, getDomain());
84 }
85
86 /**
87 * Base test cleanup
88 */
89 @After
90 public void testCleanup() {
91 session.close();
92 assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME));
93 }
94
95 /**
96 * Base class cleanup
97 */
98 @AfterClass
99 public static void julClassCleanup() {
100 LttngToolsHelper.deleteAllTraces();
101 }
102
103 // ------------------------------------------------------------------------
104 // Utility methods
105 // ------------------------------------------------------------------------
106
107 /**
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.
111 *
112 * This method should set the 'logHandler' field accordingly.
113 */
114 protected abstract void registerAgent();
115
116 /**
117 * Register the context info retriever to UST. This corresponds to the
118 * "Retriever" step.
119 */
120 private void registerRetriever() {
121 assertTrue(cim.registerContextInfoRetriever(RETRIEVER_NAME, RETRIEVER));
122 }
123
124 /**
125 * Enable the contexts in the tracing session. This corresponds to the "Session" step.
126 */
127 private void enableContextInSessions() {
128 assertTrue(session.enableAllEvents());
129 assertTrue(session.enableAppContext(RETRIEVER_NAME, CONTEXT_NAME));
130 }
131
132 /**
133 * Start tracing, send events from the application, and verify that the
134 * output contains the expected context information.
135 *
136 * This should be called only after all 3 steps above are done.
137 */
138 private void traceSendEventsAndVerify() {
139 assertTrue(session.start());
140 sendEventsToLoggers();
141 assertTrue(session.stop());
142
143 List<String> output = session.view();
144 assertNotNull(output);
145 assertFalse(output.isEmpty());
146
147 String traceRetriverName = RETRIEVER_NAME.replace('.', '_');
148 String traceContextName = CONTEXT_NAME.replace('.', '_');
149
150 String expectedString = "_app_" + traceRetriverName + "_" + traceContextName + " = { string = \"" + CONTEXT_VALUE + "\" } }";
151 output.forEach(line -> assertTrue(line.contains(expectedString)));
152 }
153
154 // ------------------------------------------------------------------------
155 // Test methods
156 // ------------------------------------------------------------------------
157
158 /**
159 * Test the sequence Agent -> Retriever -> Session
160 */
161 @Test
162 public void testAgentRetrieverSession() {
163 registerAgent();
164 registerRetriever();
165 enableContextInSessions();
166
167 traceSendEventsAndVerify();
168 }
169
170 /**
171 * Test the sequence Agent -> Session -> Retriever
172 */
173 @Test
174 public void testAgentSessionRetriever() {
175 registerAgent();
176 enableContextInSessions();
177 registerRetriever();
178
179 traceSendEventsAndVerify();
180 }
181
182 /**
183 * Test the sequence Retriever -> Agent -> Session
184 */
185 @Test
186 public void testRetrieverAgentSession() {
187 registerRetriever();
188 registerAgent();
189 enableContextInSessions();
190
191 traceSendEventsAndVerify();
192 }
193
194 /**
195 * Test the sequence Retriever -> Session -> Agent
196 */
197 @Test
198 public void testRetrieverSessionAgent() {
199 registerAgent();
200 registerRetriever();
201 enableContextInSessions();
202
203 traceSendEventsAndVerify();
204 }
205
206 /**
207 * Test the sequence Session -> Agent -> Retriever
208 */
209 @Test
210 public void testSessionAgentRetriever() {
211 registerAgent();
212 registerRetriever();
213 enableContextInSessions();
214
215 traceSendEventsAndVerify();
216 }
217
218 /**
219 * Test the sequence Session -> Retriever -> Agent
220 */
221 @Test
222 public void testSessionRetrieverAgent() {
223 registerAgent();
224 registerRetriever();
225 enableContextInSessions();
226
227 traceSendEventsAndVerify();
228 }
229 }
This page took 0.033252 seconds and 3 git commands to generate.