Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.tradefed.invoker; |
| 17 | |
Jed Estep | 1db79d1 | 2015-12-14 18:18:51 -0800 | [diff] [blame] | 18 | import com.android.ddmlib.Log.LogLevel; |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 19 | import com.android.ddmlib.testrunner.TestResult.TestStatus; |
Jed Estep | 1db79d1 | 2015-12-14 18:18:51 -0800 | [diff] [blame] | 20 | import com.android.tradefed.log.LogUtil.CLog; |
Julien Desprez | 74089e1 | 2018-05-31 01:47:53 -0700 | [diff] [blame] | 21 | import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 22 | import com.android.tradefed.result.CollectingTestListener; |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 23 | import com.android.tradefed.result.ILogSaverListener; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 24 | import com.android.tradefed.result.ITestInvocationListener; |
Omari Stephens | 41978ee | 2011-02-23 19:03:05 -0800 | [diff] [blame] | 25 | import com.android.tradefed.result.InputStreamSource; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 26 | import com.android.tradefed.result.LogDataType; |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 27 | import com.android.tradefed.result.LogFile; |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 28 | import com.android.tradefed.result.TestDescription; |
jdesprez | 821a005 | 2018-01-16 03:37:16 -0800 | [diff] [blame] | 29 | import com.android.tradefed.result.TestResult; |
| 30 | import com.android.tradefed.result.TestRunResult; |
jdesprez | 0b8464a | 2017-06-26 17:33:58 -0700 | [diff] [blame] | 31 | import com.android.tradefed.util.TimeUtil; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 32 | |
jdesprez | 0b8464a | 2017-06-26 17:33:58 -0700 | [diff] [blame] | 33 | import java.util.Collection; |
Julien Desprez | 74089e1 | 2018-05-31 01:47:53 -0700 | [diff] [blame] | 34 | import java.util.HashMap; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 35 | import java.util.Map; |
Julien Desprez | e6b756b | 2018-04-16 10:56:38 -0700 | [diff] [blame] | 36 | import java.util.Map.Entry; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * A {@link ITestInvocationListener} that collects results from a invocation shard (aka an |
| 40 | * invocation split to run on multiple resources in parallel), and forwards them to another |
| 41 | * listener. |
| 42 | */ |
Julien Desprez | 453d889 | 2017-02-06 14:13:34 +0000 | [diff] [blame] | 43 | public class ShardListener extends CollectingTestListener { |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 44 | |
| 45 | private ITestInvocationListener mMasterListener; |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 46 | private IInvocationContext mModuleContext = null; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Create a {@link ShardListener}. |
| 50 | * |
Julien Desprez | 453d889 | 2017-02-06 14:13:34 +0000 | [diff] [blame] | 51 | * @param master the {@link ITestInvocationListener} the results should be forwarded. To prevent |
| 52 | * collisions with other {@link ShardListener}s, this object will synchronize on |
| 53 | * <var>master</var> when forwarding results. And results will only be sent once the |
| 54 | * invocation shard completes. |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 55 | */ |
Julien Desprez | 453d889 | 2017-02-06 14:13:34 +0000 | [diff] [blame] | 56 | public ShardListener(ITestInvocationListener master) { |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 57 | mMasterListener = master; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * {@inheritDoc} |
| 62 | */ |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 63 | @Override |
Julien Desprez | 53353e6 | 2016-08-12 15:24:33 +0100 | [diff] [blame] | 64 | public void invocationStarted(IInvocationContext context) { |
| 65 | super.invocationStarted(context); |
| 66 | synchronized (mMasterListener) { |
| 67 | mMasterListener.invocationStarted(context); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritDoc} |
| 73 | */ |
| 74 | @Override |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 75 | public void invocationFailed(Throwable cause) { |
| 76 | super.invocationFailed(cause); |
| 77 | synchronized (mMasterListener) { |
| 78 | mMasterListener.invocationFailed(cause); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * {@inheritDoc} |
| 84 | */ |
| 85 | @Override |
Omari Stephens | 41978ee | 2011-02-23 19:03:05 -0800 | [diff] [blame] | 86 | public void testLog(String dataName, LogDataType dataType, InputStreamSource dataStream) { |
Julien Desprez | e6b756b | 2018-04-16 10:56:38 -0700 | [diff] [blame] | 87 | // forward testLog results immediately, since result reporters might take action on it. |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 88 | synchronized (mMasterListener) { |
Julien Desprez | e6b756b | 2018-04-16 10:56:38 -0700 | [diff] [blame] | 89 | if (mMasterListener instanceof ShardMasterResultForwarder) { |
| 90 | // If the listener is a log saver, we should simply forward the testLog not save |
| 91 | // again. |
| 92 | ((ShardMasterResultForwarder) mMasterListener) |
| 93 | .testLogForward(dataName, dataType, dataStream); |
| 94 | } else { |
| 95 | mMasterListener.testLog(dataName, dataType, dataStream); |
| 96 | } |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
jdesprez | 2ffb1bd | 2018-03-19 10:37:23 -0700 | [diff] [blame] | 100 | /** {@inheritDoc} */ |
| 101 | @Override |
| 102 | public void testLogSaved( |
| 103 | String dataName, LogDataType dataType, InputStreamSource dataStream, LogFile logFile) { |
| 104 | super.testLogSaved(dataName, dataType, dataStream, logFile); |
| 105 | // Forward the testLogSaved callback. |
| 106 | synchronized (mMasterListener) { |
| 107 | if (mMasterListener instanceof ILogSaverListener) { |
| 108 | ((ILogSaverListener) mMasterListener) |
| 109 | .testLogSaved(dataName, dataType, dataStream, logFile); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** {@inheritDoc} */ |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 115 | @Override |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 116 | public void testModuleStarted(IInvocationContext moduleContext) { |
| 117 | super.testModuleStarted(moduleContext); |
| 118 | mModuleContext = moduleContext; |
Jed Estep | 1db79d1 | 2015-12-14 18:18:51 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * {@inheritDoc} |
| 123 | */ |
| 124 | @Override |
| 125 | public void testRunFailed(String failureMessage) { |
| 126 | super.testRunFailed(failureMessage); |
| 127 | CLog.logAndDisplay(LogLevel.ERROR, "FAILED: %s failed with message: %s", |
| 128 | getCurrentRunResults().getName(), failureMessage); |
| 129 | } |
| 130 | |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 131 | /** {@inheritDoc} */ |
Jed Estep | 1db79d1 | 2015-12-14 18:18:51 -0800 | [diff] [blame] | 132 | @Override |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 133 | public void testRunEnded(long elapsedTime, HashMap<String, Metric> runMetrics) { |
| 134 | super.testRunEnded(elapsedTime, runMetrics); |
| 135 | CLog.logAndDisplay( |
| 136 | LogLevel.INFO, "Sharded test completed: %s", getCurrentRunResults().getName()); |
| 137 | if (mModuleContext == null) { |
| 138 | // testRunEnded only forwards if it's not part of a module. If it's a module |
| 139 | // testModuleEnded is in charge of forwarding all run results. |
| 140 | synchronized (mMasterListener) { |
| 141 | forwardRunResults(getCurrentRunResults()); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** {@inheritDoc} */ |
| 147 | @Override |
| 148 | public void testModuleEnded() { |
| 149 | super.testModuleEnded(); |
| 150 | |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 151 | synchronized (mMasterListener) { |
jdesprez | 51beee8 | 2017-08-23 15:28:42 -0700 | [diff] [blame] | 152 | IInvocationContext moduleContext = null; |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 153 | // TODO: Support attempts and retries |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 154 | for (TestRunResult runResult : getMergedTestRunResults()) { |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 155 | // Only consider run results of the module in progress |
| 156 | if (getModuleContextForRunResult(runResult.getName()) != mModuleContext) { |
| 157 | continue; |
| 158 | } |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 159 | |
jdesprez | 51beee8 | 2017-08-23 15:28:42 -0700 | [diff] [blame] | 160 | // Stop or start the module |
| 161 | if (moduleContext != null |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 162 | && !getModuleContextForRunResult(runResult.getName()) |
| 163 | .equals(moduleContext)) { |
jdesprez | 51beee8 | 2017-08-23 15:28:42 -0700 | [diff] [blame] | 164 | mMasterListener.testModuleEnded(); |
| 165 | moduleContext = null; |
| 166 | } |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 167 | if (moduleContext == null |
| 168 | && getModuleContextForRunResult(runResult.getName()) != null) { |
| 169 | moduleContext = getModuleContextForRunResult(runResult.getName()); |
jdesprez | 51beee8 | 2017-08-23 15:28:42 -0700 | [diff] [blame] | 170 | mMasterListener.testModuleStarted(moduleContext); |
| 171 | } |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 172 | // Forward the run level results |
| 173 | forwardRunResults(runResult); |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 174 | } |
jdesprez | 51beee8 | 2017-08-23 15:28:42 -0700 | [diff] [blame] | 175 | // Close the last module |
| 176 | if (moduleContext != null) { |
| 177 | mMasterListener.testModuleEnded(); |
| 178 | moduleContext = null; |
| 179 | } |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 180 | } |
| 181 | mModuleContext = null; |
| 182 | } |
| 183 | |
| 184 | /** {@inheritDoc} */ |
| 185 | @Override |
| 186 | public void invocationEnded(long elapsedTime) { |
| 187 | super.invocationEnded(elapsedTime); |
| 188 | synchronized (mMasterListener) { |
| 189 | logShardContent(getMergedTestRunResults()); |
Julien Desprez | 69c2620 | 2019-06-04 09:31:24 -0700 | [diff] [blame^] | 190 | // Report all logs not associated with test runs |
| 191 | forwardLogAssociation(getNonAssociatedLogFiles(), mMasterListener); |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 192 | mMasterListener.invocationEnded(elapsedTime); |
| 193 | } |
| 194 | } |
| 195 | |
Julien Desprez | f97eb34 | 2019-05-28 11:49:54 -0700 | [diff] [blame] | 196 | private void forwardRunResults(TestRunResult runResult) { |
| 197 | // TODO: Support attempts and retries |
| 198 | mMasterListener.testRunStarted(runResult.getName(), runResult.getExpectedTestCount()); |
| 199 | forwardTestResults(runResult.getTestResults()); |
| 200 | if (runResult.isRunFailure()) { |
| 201 | mMasterListener.testRunFailed(runResult.getRunFailureMessage()); |
| 202 | } |
| 203 | |
| 204 | // Provide a strong association of the run to its logs. |
| 205 | forwardLogAssociation(runResult.getRunLoggedFiles(), mMasterListener); |
| 206 | |
| 207 | mMasterListener.testRunEnded(runResult.getElapsedTime(), runResult.getRunProtoMetrics()); |
| 208 | } |
| 209 | |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 210 | private void forwardTestResults(Map<TestDescription, TestResult> testResults) { |
| 211 | for (Map.Entry<TestDescription, TestResult> testEntry : testResults.entrySet()) { |
jdesprez | 4f7ade0 | 2017-04-03 11:46:11 -0700 | [diff] [blame] | 212 | mMasterListener.testStarted(testEntry.getKey(), testEntry.getValue().getStartTime()); |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 213 | switch (testEntry.getValue().getStatus()) { |
| 214 | case FAILURE: |
| 215 | mMasterListener.testFailed(testEntry.getKey(), |
| 216 | testEntry.getValue().getStackTrace()); |
| 217 | break; |
| 218 | case ASSUMPTION_FAILURE: |
| 219 | mMasterListener.testAssumptionFailure(testEntry.getKey(), |
| 220 | testEntry.getValue().getStackTrace()); |
| 221 | break; |
| 222 | case IGNORED: |
| 223 | mMasterListener.testIgnored(testEntry.getKey()); |
| 224 | break; |
Julien Desprez | 2763ca0 | 2016-12-06 14:39:33 +0000 | [diff] [blame] | 225 | default: |
| 226 | break; |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 227 | } |
Julien Desprez | e6b756b | 2018-04-16 10:56:38 -0700 | [diff] [blame] | 228 | // Provide a strong association of the test to its logs. |
| 229 | forwardLogAssociation(testEntry.getValue().getLoggedFiles(), mMasterListener); |
| 230 | |
Brett Chabot | e3b8b9e | 2011-10-21 14:17:06 -0700 | [diff] [blame] | 231 | if (!testEntry.getValue().getStatus().equals(TestStatus.INCOMPLETE)) { |
jdesprez | 4f7ade0 | 2017-04-03 11:46:11 -0700 | [diff] [blame] | 232 | mMasterListener.testEnded( |
| 233 | testEntry.getKey(), |
| 234 | testEntry.getValue().getEndTime(), |
Julien Desprez | 04db83b | 2018-04-25 06:06:36 -0700 | [diff] [blame] | 235 | testEntry.getValue().getProtoMetrics()); |
Brett Chabot | e3b8b9e | 2011-10-21 14:17:06 -0700 | [diff] [blame] | 236 | } |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
jdesprez | 0b8464a | 2017-06-26 17:33:58 -0700 | [diff] [blame] | 239 | |
Julien Desprez | e6b756b | 2018-04-16 10:56:38 -0700 | [diff] [blame] | 240 | /** Forward to the listener the logAssociated callback on the files. */ |
| 241 | private void forwardLogAssociation( |
| 242 | Map<String, LogFile> loggedFiles, ITestInvocationListener listener) { |
| 243 | for (Entry<String, LogFile> logFile : loggedFiles.entrySet()) { |
| 244 | if (listener instanceof ILogSaverListener) { |
| 245 | ((ILogSaverListener) listener).logAssociation(logFile.getKey(), logFile.getValue()); |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
jdesprez | 0b8464a | 2017-06-26 17:33:58 -0700 | [diff] [blame] | 250 | /** Log the content of the shard for easier debugging. */ |
| 251 | private void logShardContent(Collection<TestRunResult> listResults) { |
| 252 | CLog.d("================================================="); |
| 253 | CLog.d( |
| 254 | "========== Shard Primary Device %s ==========", |
| 255 | getInvocationContext().getDevices().get(0).getSerialNumber()); |
| 256 | for (TestRunResult runRes : listResults) { |
| 257 | CLog.d( |
| 258 | "\tRan '%s' in %s", |
| 259 | runRes.getName(), TimeUtil.formatElapsedTime(runRes.getElapsedTime())); |
| 260 | } |
| 261 | CLog.d("================================================="); |
| 262 | } |
Brett Chabot | b1052c2 | 2011-01-12 19:44:23 -0800 | [diff] [blame] | 263 | } |