| 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.filter; |
| 20 | |
| 21 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | |
| 23 | import java.util.Collections; |
| 24 | import java.util.Set; |
| 25 | import java.util.stream.Collectors; |
| 26 | import java.util.stream.Stream; |
| 27 | |
| 28 | import org.junit.jupiter.api.Test; |
| 29 | import org.junit.jupiter.api.extension.ExtendWith; |
| 30 | import org.junit.jupiter.api.AfterEach; |
| 31 | import org.lttng.tools.ILttngSession; |
| 32 | import org.lttng.ust.agent.filter.FilterChangeNotifier; |
| 33 | import org.lttng.ust.agent.integration.filter.FilterListenerITBase.TestFilterListener; |
| 34 | import org.lttng.ust.agent.session.EventRule; |
| 35 | import org.lttng.ust.agent.utils.EventRuleFactory; |
| 36 | import org.lttng.ust.agent.utils.TestPrintExtension; |
| 37 | |
| 38 | /** |
| 39 | * For the filter change notifications to work, several setup steps are |
| 40 | * required: |
| 41 | * |
| 42 | * <ul> |
| 43 | * <li>Initialize the Java agent register it to the sessiond [Agent]</li> |
| 44 | * <li>Instantiate a filer change listener, and register it to the notifier |
| 45 | * [Listener]</li> |
| 46 | * <li>Apply some event rule changes in the tracing session (lttng enable-event, |
| 47 | * etc.) [Session]</li> |
| 48 | * </ul> |
| 49 | * |
| 50 | * <p> |
| 51 | * Then on teardown, the following steps are expected: |
| 52 | * </p> |
| 53 | * |
| 54 | * <ul> |
| 55 | * <li>Dispose the Java agent, closing the connection to the sessiond [Agent] |
| 56 | * </li> |
| 57 | * <li>Destroy the tracing session, removing tracked events [Session]</li> |
| 58 | * </ul> |
| 59 | * |
| 60 | * (and then the filter change listener should be de-registered from the |
| 61 | * notifier. If it is deregistered earlier, then obviously no notifications |
| 62 | * would be received thereafter). |
| 63 | * |
| 64 | * <p> |
| 65 | * Within these two sets, each step can happen in any order. This results in 6 x |
| 66 | * 2 = 12 possibilities. The goal of this test class it to test these 12 |
| 67 | * possibilities. |
| 68 | * </p> |
| 69 | */ |
| 70 | @ExtendWith(TestPrintExtension.class) |
| 71 | @SuppressWarnings("javadoc") |
| 72 | public abstract class FilterListenerOrderingITBase { |
| 73 | |
| 74 | protected static final String EVENT_NAME_A = "EventA"; |
| 75 | private static final String EVENT_NAME_B = "EventB"; |
| 76 | |
| 77 | protected EventRuleFactory eventRuleFactory; |
| 78 | |
| 79 | private ILttngSession session; |
| 80 | private TestFilterListener listener; |
| 81 | |
| 82 | /** |
| 83 | * Base class cleanup |
| 84 | */ |
| 85 | @AfterEach |
| 86 | public void baseTeardown() { |
| 87 | /* |
| 88 | * Deregister the listener (should always be done after all the other |
| 89 | * steps). |
| 90 | */ |
| 91 | FilterChangeNotifier.getInstance().unregisterListener(listener); |
| 92 | listener = null; |
| 93 | } |
| 94 | |
| 95 | // ------------------------------------------------------------------------ |
| 96 | // Utility methods |
| 97 | // ------------------------------------------------------------------------ |
| 98 | |
| 99 | protected abstract ILttngSession.Domain getDomain(); |
| 100 | |
| 101 | protected abstract void registerAgent(); |
| 102 | |
| 103 | private void registerListener() { |
| 104 | listener = new TestFilterListener(); |
| 105 | FilterChangeNotifier.getInstance().registerListener(listener); |
| 106 | } |
| 107 | |
| 108 | private void enableRulesInSession() { |
| 109 | session = ILttngSession.createCommandLineSession(null, getDomain()); |
| 110 | session.enableEvent(EVENT_NAME_A, null, false, null); |
| 111 | session.enableEvent(EVENT_NAME_B, null, false, null); |
| 112 | } |
| 113 | |
| 114 | protected abstract void deregisterAgent(); |
| 115 | |
| 116 | private void destroySession() { |
| 117 | session.close(); |
| 118 | session = null; |
| 119 | } |
| 120 | |
| 121 | protected EventRuleFactory getEventRuleFactory() { |
| 122 | if (eventRuleFactory == null) { |
| 123 | eventRuleFactory = new EventRuleFactory(getDomain()); |
| 124 | } |
| 125 | return eventRuleFactory; |
| 126 | } |
| 127 | |
| 128 | // ------------------------------------------------------------------------ |
| 129 | // Test methods |
| 130 | // ------------------------------------------------------------------------ |
| 131 | |
| 132 | /** |
| 133 | * Check that the expected event rules are present after setup but before |
| 134 | * teardown. |
| 135 | */ |
| 136 | private void checkOngoingConditions() { |
| 137 | Set<EventRule> exptectedRules = Stream.of( |
| 138 | getEventRuleFactory().createRule(EVENT_NAME_A), |
| 139 | getEventRuleFactory().createRule(EVENT_NAME_B)) |
| 140 | .collect(Collectors.toSet()); |
| 141 | |
| 142 | assertEquals(2, listener.getNbNotifications()); |
| 143 | assertEquals(exptectedRules, listener.getCurrentRules()); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Check that the expected event rules are present after/during teardown. |
| 148 | */ |
| 149 | private void checkFinalConditions() { |
| 150 | Set<EventRule> expectedRules = Collections.EMPTY_SET; |
| 151 | |
| 152 | assertEquals(4, listener.getNbNotifications()); |
| 153 | assertEquals(expectedRules, listener.getCurrentRules()); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testAgentListenerSession_AgentSession() { |
| 158 | registerAgent(); |
| 159 | registerListener(); |
| 160 | enableRulesInSession(); |
| 161 | |
| 162 | checkOngoingConditions(); |
| 163 | |
| 164 | deregisterAgent(); |
| 165 | destroySession(); |
| 166 | |
| 167 | checkFinalConditions(); |
| 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testAgentSessionListener_AgentSession() { |
| 172 | registerAgent(); |
| 173 | enableRulesInSession(); |
| 174 | registerListener(); |
| 175 | |
| 176 | checkOngoingConditions(); |
| 177 | |
| 178 | deregisterAgent(); |
| 179 | destroySession(); |
| 180 | |
| 181 | checkFinalConditions(); |
| 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testListenerAgentSession_AgentSession() { |
| 186 | registerListener(); |
| 187 | registerAgent(); |
| 188 | enableRulesInSession(); |
| 189 | |
| 190 | checkOngoingConditions(); |
| 191 | |
| 192 | deregisterAgent(); |
| 193 | destroySession(); |
| 194 | |
| 195 | checkFinalConditions(); |
| 196 | } |
| 197 | |
| 198 | @Test |
| 199 | public void testListenerSessionAgent_AgentSession() { |
| 200 | registerListener(); |
| 201 | enableRulesInSession(); |
| 202 | registerAgent(); |
| 203 | |
| 204 | checkOngoingConditions(); |
| 205 | |
| 206 | deregisterAgent(); |
| 207 | destroySession(); |
| 208 | |
| 209 | checkFinalConditions(); |
| 210 | } |
| 211 | |
| 212 | @Test |
| 213 | public void testSessionAgentListener_AgentSession() { |
| 214 | enableRulesInSession(); |
| 215 | registerAgent(); |
| 216 | registerListener(); |
| 217 | |
| 218 | checkOngoingConditions(); |
| 219 | |
| 220 | deregisterAgent(); |
| 221 | destroySession(); |
| 222 | |
| 223 | checkFinalConditions(); |
| 224 | } |
| 225 | |
| 226 | @Test |
| 227 | public void testSessionListenerAgent_AgentSession() { |
| 228 | enableRulesInSession(); |
| 229 | registerListener(); |
| 230 | registerAgent(); |
| 231 | |
| 232 | checkOngoingConditions(); |
| 233 | |
| 234 | deregisterAgent(); |
| 235 | destroySession(); |
| 236 | |
| 237 | checkFinalConditions(); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | @Test |
| 243 | public void testAgentListenerSession_SessionAgent() { |
| 244 | registerAgent(); |
| 245 | registerListener(); |
| 246 | enableRulesInSession(); |
| 247 | |
| 248 | checkOngoingConditions(); |
| 249 | |
| 250 | destroySession(); |
| 251 | checkFinalConditions(); |
| 252 | deregisterAgent(); |
| 253 | checkFinalConditions(); |
| 254 | } |
| 255 | |
| 256 | @Test |
| 257 | public void testAgentSessionListener_SessionAgent() { |
| 258 | registerAgent(); |
| 259 | enableRulesInSession(); |
| 260 | registerListener(); |
| 261 | |
| 262 | checkOngoingConditions(); |
| 263 | |
| 264 | destroySession(); |
| 265 | checkFinalConditions(); |
| 266 | deregisterAgent(); |
| 267 | checkFinalConditions(); |
| 268 | } |
| 269 | |
| 270 | @Test |
| 271 | public void testListenerAgentSession_SessionAgent() { |
| 272 | registerListener(); |
| 273 | registerAgent(); |
| 274 | enableRulesInSession(); |
| 275 | |
| 276 | checkOngoingConditions(); |
| 277 | |
| 278 | destroySession(); |
| 279 | checkFinalConditions(); |
| 280 | deregisterAgent(); |
| 281 | checkFinalConditions(); |
| 282 | } |
| 283 | |
| 284 | @Test |
| 285 | public void testListenerSessionAgent_SessionAgent() { |
| 286 | registerListener(); |
| 287 | enableRulesInSession(); |
| 288 | registerAgent(); |
| 289 | |
| 290 | checkOngoingConditions(); |
| 291 | |
| 292 | destroySession(); |
| 293 | checkFinalConditions(); |
| 294 | deregisterAgent(); |
| 295 | checkFinalConditions(); |
| 296 | } |
| 297 | |
| 298 | @Test |
| 299 | public void testSessionAgentListener_SessionAgent() { |
| 300 | enableRulesInSession(); |
| 301 | registerAgent(); |
| 302 | registerListener(); |
| 303 | |
| 304 | checkOngoingConditions(); |
| 305 | |
| 306 | destroySession(); |
| 307 | checkFinalConditions(); |
| 308 | deregisterAgent(); |
| 309 | checkFinalConditions(); |
| 310 | } |
| 311 | |
| 312 | @Test |
| 313 | public void testSessionListenerAgent_SessionAgent() { |
| 314 | enableRulesInSession(); |
| 315 | registerListener(); |
| 316 | registerAgent(); |
| 317 | |
| 318 | checkOngoingConditions(); |
| 319 | |
| 320 | destroySession(); |
| 321 | checkFinalConditions(); |
| 322 | deregisterAgent(); |
| 323 | checkFinalConditions(); |
| 324 | } |
| 325 | |
| 326 | } |