Migrate to Junit 5 Jupiter
[lttng-ust-java-tests.git] / lttng-ust-java-tests-common / src / main / java / org / lttng / ust / agent / integration / context / AppContextOrderingITBase.java
CommitLineData
73fb6785
AM
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
19package org.lttng.ust.agent.integration.context;
20
7a4f0255
MJ
21import static org.junit.jupiter.api.Assertions.assertFalse;
22import static org.junit.jupiter.api.Assertions.assertNotNull;
23import static org.junit.jupiter.api.Assertions.assertTrue;
24import static org.junit.jupiter.api.Assertions.fail;
73fb6785
AM
25
26import java.io.IOException;
27import java.util.List;
28
7a4f0255
MJ
29import org.junit.jupiter.api.AfterAll;
30import org.junit.jupiter.api.AfterEach;
31import org.junit.jupiter.api.BeforeEach;
32import org.junit.jupiter.api.Test;
33import org.junit.jupiter.api.extension.ExtendWith;
73fb6785
AM
34import org.lttng.tools.ILttngSession;
35import org.lttng.tools.LttngToolsHelper;
36import org.lttng.tools.ILttngSession.Domain;
37import org.lttng.ust.agent.ILttngHandler;
38import org.lttng.ust.agent.context.ContextInfoManager;
39import org.lttng.ust.agent.context.IContextInfoRetriever;
7a4f0255 40import org.lttng.ust.agent.utils.TestPrintExtension;
73fb6785
AM
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 */
7a4f0255 54@ExtendWith(TestPrintExtension.class)
73fb6785
AM
55public 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 */
7a4f0255 75 @BeforeEach
73fb6785
AM
76 public void testSetup() {
77 try {
78 cim = ContextInfoManager.getInstance();
79 } catch (SecurityException | IOException e) {
80 /* The native library is not available! */
1f3dd43c 81 fail(e.getMessage());
73fb6785
AM
82 }
83 session = ILttngSession.createSession(null, getDomain());
84 }
85
86 /**
87 * Base test cleanup
88 */
7a4f0255 89 @AfterEach
73fb6785
AM
90 public void testCleanup() {
91 session.close();
92 assertTrue(cim.unregisterContextInfoRetriever(RETRIEVER_NAME));
93 }
94
95 /**
96 * Base class cleanup
97 */
7a4f0255 98 @AfterAll
73fb6785
AM
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
5ab95f21
AM
147 String traceRetriverName = RETRIEVER_NAME.replace('.', '_');
148 String traceContextName = CONTEXT_NAME.replace('.', '_');
149
fd2b8059 150 String expectedString = "_app_" + traceRetriverName + "_" + traceContextName + " = { \"" + CONTEXT_VALUE + "\" } }";
73fb6785
AM
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.03139 seconds and 4 git commands to generate.