Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.result; |
| 17 | |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 18 | import com.android.ddmlib.testrunner.TestResult.TestStatus; |
Brett Chabot | beaebb0 | 2011-02-07 16:16:06 -0800 | [diff] [blame] | 19 | import com.android.tradefed.build.IBuildInfo; |
Brett Chabot | d110d21 | 2011-01-21 20:10:17 -0800 | [diff] [blame] | 20 | import com.android.tradefed.config.Option; |
Julien Desprez | 53353e6 | 2016-08-12 15:24:33 +0100 | [diff] [blame] | 21 | import com.android.tradefed.invoker.IInvocationContext; |
Julien Desprez | 93a041b | 2018-08-29 08:55:27 -0700 | [diff] [blame] | 22 | import com.android.tradefed.log.LogUtil.CLog; |
jdesprez | 9c6eb22 | 2018-04-02 10:47:08 -0700 | [diff] [blame] | 23 | import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; |
Julien Desprez | e5bdb4c | 2019-08-01 14:09:30 -0700 | [diff] [blame] | 24 | import com.android.tradefed.retry.MergeStrategy; |
Brett Chabot | 5ec5292 | 2010-09-07 18:08:30 -0700 | [diff] [blame] | 25 | |
Julien Desprez | 1047f36 | 2017-02-20 09:24:27 +0000 | [diff] [blame] | 26 | import com.google.common.annotations.VisibleForTesting; |
Julien Desprez | 93a041b | 2018-08-29 08:55:27 -0700 | [diff] [blame] | 27 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 28 | import java.util.ArrayList; |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 29 | import java.util.Collection; |
Julien Desprez | 0dd832d | 2018-12-03 10:35:53 -0800 | [diff] [blame] | 30 | import java.util.Collections; |
jdesprez | 9c6eb22 | 2018-04-02 10:47:08 -0700 | [diff] [blame] | 31 | import java.util.HashMap; |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 32 | import java.util.LinkedHashMap; |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 33 | import java.util.LinkedList; |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 34 | import java.util.List; |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 35 | import java.util.Map; |
Julien Desprez | 7ea0bd4 | 2018-12-05 09:59:28 -0800 | [diff] [blame] | 36 | import java.util.Map.Entry; |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 37 | import java.util.concurrent.atomic.AtomicBoolean; |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 40 | * A {@link ITestInvocationListener} that will collect all test results. |
jdesprez | 5390ce8 | 2018-01-25 16:54:14 -0800 | [diff] [blame] | 41 | * |
| 42 | * <p>Although the data structures used in this object are thread-safe, the {@link |
| 43 | * ITestInvocationListener} callbacks must be called in the correct order. |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 44 | */ |
jdesprez | 5390ce8 | 2018-01-25 16:54:14 -0800 | [diff] [blame] | 45 | public class CollectingTestListener implements ITestInvocationListener, ILogSaverListener { |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 46 | |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 47 | @Option( |
| 48 | name = "aggregate-metrics", |
| 49 | description = "attempt to add test metrics values for test runs with the same name.") |
Brett Chabot | d110d21 | 2011-01-21 20:10:17 -0800 | [diff] [blame] | 50 | private boolean mIsAggregateMetrics = false; |
| 51 | |
jdesprez | bc580f9 | 2017-06-02 11:41:40 -0700 | [diff] [blame] | 52 | /** Toggle the 'aggregate metrics' option */ |
| 53 | protected void setIsAggregrateMetrics(boolean aggregate) { |
Brett Chabot | d110d21 | 2011-01-21 20:10:17 -0800 | [diff] [blame] | 54 | mIsAggregateMetrics = aggregate; |
| 55 | } |
| 56 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 57 | private IInvocationContext mContext; |
| 58 | private IBuildInfo mBuildInfo; |
| 59 | private Map<String, IInvocationContext> mModuleContextMap = new HashMap<>(); |
| 60 | // Use LinkedHashMap to provide consistent iterations over the keys. |
Julien Desprez | 0dd832d | 2018-12-03 10:35:53 -0800 | [diff] [blame] | 61 | private Map<String, List<TestRunResult>> mTestRunResultMap = |
| 62 | Collections.synchronizedMap(new LinkedHashMap<>()); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 63 | |
| 64 | private IInvocationContext mCurrentModuleContext = null; |
| 65 | private TestRunResult mCurrentTestRunResult = new TestRunResult(); |
Julien Desprez | f94d308 | 2019-04-23 10:45:01 -0700 | [diff] [blame] | 66 | /** True if the default initialized mCurrentTestRunResult has its original value. */ |
| 67 | private boolean mDefaultRun = true; |
Julien Desprez | 69c2620 | 2019-06-04 09:31:24 -0700 | [diff] [blame] | 68 | /** Track whether or not a test run is currently in progress */ |
| 69 | private boolean mRunInProgress = false; |
| 70 | |
| 71 | private Map<String, LogFile> mNonAssociatedLogFiles = new LinkedHashMap<>(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 72 | |
| 73 | // Tracks if mStatusCounts are accurate, or if they need to be recalculated |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 74 | private AtomicBoolean mIsCountDirty = new AtomicBoolean(true); |
Julien Desprez | 0c490df | 2019-01-07 11:34:46 -0800 | [diff] [blame] | 75 | // Tracks if the expected count is accurate, or if it needs to be recalculated. |
| 76 | private AtomicBoolean mIsExpectedCountDirty = new AtomicBoolean(true); |
| 77 | private int mExpectedCount = 0; |
| 78 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 79 | // Represents the merged test results. This should not be accessed directly since it's only |
| 80 | // calculated when needed. |
Julien Desprez | bcd2ff3 | 2018-11-20 18:23:36 -0800 | [diff] [blame] | 81 | private final List<TestRunResult> mMergedTestRunResults = new ArrayList<>(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 82 | // Represents the number of tests in each TestStatus state of the merged test results. Indexed |
| 83 | // by TestStatus.ordinal() |
| 84 | private int[] mStatusCounts = new int[TestStatus.values().length]; |
| 85 | |
Julien Desprez | dc18665 | 2018-08-29 11:56:43 -0700 | [diff] [blame] | 86 | private MergeStrategy mStrategy = MergeStrategy.ONE_TESTCASE_PASS_IS_PASS; |
| 87 | |
| 88 | /** Sets the {@link MergeStrategy} to use when merging results. */ |
| 89 | public void setMergeStrategy(MergeStrategy strategy) { |
| 90 | mStrategy = strategy; |
| 91 | } |
| 92 | |
Brett Chabot | d110d21 | 2011-01-21 20:10:17 -0800 | [diff] [blame] | 93 | /** |
Julien Desprez | 9adeb51 | 2016-11-15 12:08:51 +0000 | [diff] [blame] | 94 | * Return the primary build info that was reported via {@link |
| 95 | * #invocationStarted(IInvocationContext)}. Primary build is the build returned by the first |
Julien Desprez | 1047f36 | 2017-02-20 09:24:27 +0000 | [diff] [blame] | 96 | * build provider of the running configuration. Returns null if there is no context (no build to |
| 97 | * test case). |
Brett Chabot | 0829eea | 2011-06-17 10:48:08 -0700 | [diff] [blame] | 98 | */ |
Julien Desprez | 9adeb51 | 2016-11-15 12:08:51 +0000 | [diff] [blame] | 99 | public IBuildInfo getPrimaryBuildInfo() { |
Julien Desprez | 1047f36 | 2017-02-20 09:24:27 +0000 | [diff] [blame] | 100 | if (mContext == null) { |
| 101 | return null; |
| 102 | } else { |
| 103 | return mContext.getBuildInfos().get(0); |
| 104 | } |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 108 | * Return the invocation context that was reported via {@link |
| 109 | * #invocationStarted(IInvocationContext)} |
Tsu Chiang Chuang | f39b58f | 2012-08-29 18:30:55 -0700 | [diff] [blame] | 110 | */ |
Julien Desprez | dc53cde | 2016-10-06 15:49:08 +0100 | [diff] [blame] | 111 | public IInvocationContext getInvocationContext() { |
| 112 | return mContext; |
| 113 | } |
| 114 | |
| 115 | /** |
Julien Desprez | 9adeb51 | 2016-11-15 12:08:51 +0000 | [diff] [blame] | 116 | * Returns the build info. |
| 117 | * |
| 118 | * @deprecated rely on the {@link IBuildInfo} from {@link #getInvocationContext()}. |
| 119 | */ |
| 120 | @Deprecated |
| 121 | public IBuildInfo getBuildInfo() { |
| 122 | return mBuildInfo; |
| 123 | } |
| 124 | |
| 125 | /** |
Julien Desprez | dc53cde | 2016-10-06 15:49:08 +0100 | [diff] [blame] | 126 | * Set the build info. Should only be used for testing. |
Julien Desprez | 9adeb51 | 2016-11-15 12:08:51 +0000 | [diff] [blame] | 127 | * |
| 128 | * @deprecated Not necessary for testing anymore. |
Julien Desprez | dc53cde | 2016-10-06 15:49:08 +0100 | [diff] [blame] | 129 | */ |
| 130 | @VisibleForTesting |
Julien Desprez | 9adeb51 | 2016-11-15 12:08:51 +0000 | [diff] [blame] | 131 | @Deprecated |
Tsu Chiang Chuang | f39b58f | 2012-08-29 18:30:55 -0700 | [diff] [blame] | 132 | public void setBuildInfo(IBuildInfo buildInfo) { |
| 133 | mBuildInfo = buildInfo; |
| 134 | } |
| 135 | |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 136 | /** {@inheritDoc} */ |
| 137 | @Override |
| 138 | public void invocationStarted(IInvocationContext context) { |
| 139 | mContext = context; |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 140 | mBuildInfo = getPrimaryBuildInfo(); |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** {@inheritDoc} */ |
| 144 | @Override |
| 145 | public void invocationEnded(long elapsedTime) { |
| 146 | // ignore |
| 147 | } |
| 148 | |
| 149 | /** {@inheritDoc} */ |
| 150 | @Override |
| 151 | public void invocationFailed(Throwable cause) { |
| 152 | // ignore |
| 153 | } |
| 154 | |
Julien Desprez | 7fc5425 | 2020-04-30 15:08:45 -0700 | [diff] [blame^] | 155 | /** {@inheritDoc} */ |
| 156 | @Override |
| 157 | public void invocationFailed(FailureDescription failure) { |
| 158 | // ignore |
| 159 | } |
| 160 | |
jdesprez | 51beee8 | 2017-08-23 15:28:42 -0700 | [diff] [blame] | 161 | @Override |
| 162 | public void testModuleStarted(IInvocationContext moduleContext) { |
| 163 | mCurrentModuleContext = moduleContext; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void testModuleEnded() { |
| 168 | mCurrentModuleContext = null; |
| 169 | } |
| 170 | |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 171 | /** {@inheritDoc} */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 172 | @Override |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 173 | public void testRunStarted(String name, int numTests) { |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 174 | testRunStarted(name, numTests, 0); |
| 175 | } |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 176 | |
Julien Desprez | f94d308 | 2019-04-23 10:45:01 -0700 | [diff] [blame] | 177 | private TestRunResult getNewRunResult() { |
| 178 | TestRunResult result = new TestRunResult(); |
| 179 | if (mDefaultRun) { |
| 180 | result = mCurrentTestRunResult; |
| 181 | mDefaultRun = false; |
| 182 | } |
| 183 | result.setAggregateMetrics(mIsAggregateMetrics); |
| 184 | return result; |
| 185 | } |
| 186 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 187 | /** {@inheritDoc} */ |
| 188 | @Override |
| 189 | public void testRunStarted(String name, int numTests, int attemptNumber) { |
Julien Desprez | 5cdec9f | 2019-06-13 13:32:43 -0700 | [diff] [blame] | 190 | testRunStarted(name, numTests, attemptNumber, System.currentTimeMillis()); |
| 191 | } |
| 192 | |
| 193 | /** {@inheritDoc} */ |
| 194 | @Override |
| 195 | public void testRunStarted(String name, int numTests, int attemptNumber, long startTime) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 196 | setCountDirty(); |
Julien Desprez | 0c490df | 2019-01-07 11:34:46 -0800 | [diff] [blame] | 197 | // Only testRunStarted can affect the expected count. |
| 198 | mIsExpectedCountDirty.set(true); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 199 | |
| 200 | // Associate the run name with the current module context |
| 201 | if (mCurrentModuleContext != null) { |
| 202 | mModuleContextMap.put(name, mCurrentModuleContext); |
| 203 | } |
| 204 | |
| 205 | // Add the list of maps if the run doesn't exist |
| 206 | if (!mTestRunResultMap.containsKey(name)) { |
| 207 | mTestRunResultMap.put(name, new LinkedList<>()); |
| 208 | } |
| 209 | List<TestRunResult> results = mTestRunResultMap.get(name); |
| 210 | |
| 211 | // Set the current test run result based on the attempt |
| 212 | if (attemptNumber < results.size()) { |
| 213 | if (results.get(attemptNumber) == null) { |
| 214 | throw new RuntimeException( |
| 215 | "Test run results should never be null in internal structure."); |
| 216 | } |
| 217 | } else if (attemptNumber == results.size()) { |
| 218 | // new run |
Julien Desprez | f94d308 | 2019-04-23 10:45:01 -0700 | [diff] [blame] | 219 | TestRunResult result = getNewRunResult(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 220 | results.add(result); |
| 221 | } else { |
Julien Desprez | 7ea0bd4 | 2018-12-05 09:59:28 -0800 | [diff] [blame] | 222 | int size = results.size(); |
| 223 | for (int i = size; i < attemptNumber; i++) { |
Julien Desprez | f94d308 | 2019-04-23 10:45:01 -0700 | [diff] [blame] | 224 | TestRunResult result = getNewRunResult(); |
Julien Desprez | 5cdec9f | 2019-06-13 13:32:43 -0700 | [diff] [blame] | 225 | result.testRunStarted(name, numTests, startTime); |
Julien Desprez | 7ea0bd4 | 2018-12-05 09:59:28 -0800 | [diff] [blame] | 226 | String errorMessage = |
| 227 | String.format( |
Julien Desprez | d997789 | 2020-04-14 09:01:15 -0700 | [diff] [blame] | 228 | "Run attempt %s of %s did not exists, but got attempt %s." |
| 229 | + " This is a placeholder for the missing attempt.", |
Julien Desprez | 7ea0bd4 | 2018-12-05 09:59:28 -0800 | [diff] [blame] | 230 | i, name, attemptNumber); |
| 231 | result.testRunFailed(errorMessage); |
| 232 | result.testRunEnded(0L, new HashMap<String, Metric>()); |
| 233 | results.add(result); |
| 234 | } |
| 235 | // New current run |
Julien Desprez | f94d308 | 2019-04-23 10:45:01 -0700 | [diff] [blame] | 236 | TestRunResult newResult = getNewRunResult(); |
Julien Desprez | 7ea0bd4 | 2018-12-05 09:59:28 -0800 | [diff] [blame] | 237 | results.add(newResult); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 238 | } |
| 239 | mCurrentTestRunResult = results.get(attemptNumber); |
| 240 | |
Julien Desprez | 5cdec9f | 2019-06-13 13:32:43 -0700 | [diff] [blame] | 241 | mCurrentTestRunResult.testRunStarted(name, numTests, startTime); |
Julien Desprez | 69c2620 | 2019-06-04 09:31:24 -0700 | [diff] [blame] | 242 | mRunInProgress = true; |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 243 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 244 | |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 245 | /** {@inheritDoc} */ |
Brett Chabot | 45bdfd7 | 2011-09-07 18:26:56 -0700 | [diff] [blame] | 246 | @Override |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 247 | public void testRunEnded(long elapsedTime, HashMap<String, Metric> runMetrics) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 248 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 249 | mCurrentTestRunResult.testRunEnded(elapsedTime, runMetrics); |
Julien Desprez | 69c2620 | 2019-06-04 09:31:24 -0700 | [diff] [blame] | 250 | mRunInProgress = false; |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** {@inheritDoc} */ |
| 254 | @Override |
| 255 | public void testRunFailed(String errorMessage) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 256 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 257 | mCurrentTestRunResult.testRunFailed(errorMessage); |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** {@inheritDoc} */ |
| 261 | @Override |
Julien Desprez | edb2a0c | 2020-03-18 11:10:34 -0700 | [diff] [blame] | 262 | public void testRunFailed(FailureDescription failure) { |
| 263 | setCountDirty(); |
| 264 | mCurrentTestRunResult.testRunFailed(failure); |
| 265 | } |
| 266 | |
| 267 | /** {@inheritDoc} */ |
| 268 | @Override |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 269 | public void testRunStopped(long elapsedTime) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 270 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 271 | mCurrentTestRunResult.testRunStopped(elapsedTime); |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** {@inheritDoc} */ |
| 275 | @Override |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 276 | public void testStarted(TestDescription test) { |
jdesprez | 4f7ade0 | 2017-04-03 11:46:11 -0700 | [diff] [blame] | 277 | testStarted(test, System.currentTimeMillis()); |
| 278 | } |
| 279 | |
| 280 | /** {@inheritDoc} */ |
| 281 | @Override |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 282 | public void testStarted(TestDescription test, long startTime) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 283 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 284 | mCurrentTestRunResult.testStarted(test, startTime); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 285 | } |
| 286 | |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 287 | /** {@inheritDoc} */ |
Brett Chabot | 45bdfd7 | 2011-09-07 18:26:56 -0700 | [diff] [blame] | 288 | @Override |
Julien Desprez | 6b9a947 | 2018-05-25 14:26:46 -0700 | [diff] [blame] | 289 | public void testEnded(TestDescription test, HashMap<String, Metric> testMetrics) { |
| 290 | testEnded(test, System.currentTimeMillis(), testMetrics); |
| 291 | } |
| 292 | |
| 293 | /** {@inheritDoc} */ |
| 294 | @Override |
Julien Desprez | 6b9a947 | 2018-05-25 14:26:46 -0700 | [diff] [blame] | 295 | public void testEnded(TestDescription test, long endTime, HashMap<String, Metric> testMetrics) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 296 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 297 | mCurrentTestRunResult.testEnded(test, endTime, testMetrics); |
jdesprez | 4f7ade0 | 2017-04-03 11:46:11 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** {@inheritDoc} */ |
Brett Chabot | 683ee43 | 2011-12-08 11:43:50 -0800 | [diff] [blame] | 301 | @Override |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 302 | public void testFailed(TestDescription test, String trace) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 303 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 304 | mCurrentTestRunResult.testFailed(test, trace); |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @Override |
Julien Desprez | e393c1c | 2020-04-07 10:34:42 -0700 | [diff] [blame] | 308 | public void testFailed(TestDescription test, FailureDescription failure) { |
| 309 | setCountDirty(); |
| 310 | mCurrentTestRunResult.testFailed(test, failure); |
| 311 | } |
| 312 | |
| 313 | @Override |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 314 | public void testAssumptionFailure(TestDescription test, String trace) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 315 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 316 | mCurrentTestRunResult.testAssumptionFailure(test, trace); |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | @Override |
jdesprez | 109b1de | 2018-02-07 16:33:40 -0800 | [diff] [blame] | 320 | public void testIgnored(TestDescription test) { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 321 | setCountDirty(); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 322 | mCurrentTestRunResult.testIgnored(test); |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 323 | } |
| 324 | |
jdesprez | 9c6eb22 | 2018-04-02 10:47:08 -0700 | [diff] [blame] | 325 | /** {@inheritDoc} */ |
| 326 | @Override |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 327 | public void logAssociation(String dataName, LogFile logFile) { |
Julien Desprez | 69c2620 | 2019-06-04 09:31:24 -0700 | [diff] [blame] | 328 | if (mRunInProgress) { |
| 329 | mCurrentTestRunResult.testLogSaved(dataName, logFile); |
| 330 | } else { |
| 331 | mNonAssociatedLogFiles.put(dataName, logFile); |
| 332 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /** |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 336 | * Gets the results for the current test run. |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 337 | * |
| 338 | * <p>Note the results may not be complete. It is recommended to test the value of {@link |
Brett Chabot | 5371491 | 2011-01-09 16:38:50 -0800 | [diff] [blame] | 339 | * TestRunResult#isRunComplete()} and/or (@link TestRunResult#isRunFailure()} as appropriate |
| 340 | * before processing the results. |
Brett Chabot | 4d081a9 | 2010-09-29 16:44:19 -0700 | [diff] [blame] | 341 | * |
| 342 | * @return the {@link TestRunResult} representing data collected during last test run |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 343 | */ |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 344 | public TestRunResult getCurrentRunResults() { |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 345 | return mCurrentTestRunResult; |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 348 | /** Returns the total number of complete tests for all runs. */ |
Brett Chabot | 873741a | 2010-09-15 12:24:08 -0700 | [diff] [blame] | 349 | public int getNumTotalTests() { |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 350 | computeMergedResults(); |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 351 | int total = 0; |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 352 | for (TestStatus s : TestStatus.values()) { |
| 353 | total += mStatusCounts[s.ordinal()]; |
Brett Chabot | d5769e3 | 2011-09-26 19:54:40 -0700 | [diff] [blame] | 354 | } |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 355 | return total; |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Julien Desprez | 96a6a99 | 2018-08-31 10:42:29 -0700 | [diff] [blame] | 358 | /** |
| 359 | * Returns the number of expected tests count. Could differ from {@link #getNumTotalTests()} if |
| 360 | * some tests did not run. |
| 361 | */ |
Julien Desprez | 0c490df | 2019-01-07 11:34:46 -0800 | [diff] [blame] | 362 | public synchronized int getExpectedTests() { |
| 363 | // If expected count is not dirty, no need to do anything |
| 364 | if (!mIsExpectedCountDirty.compareAndSet(true, false)) { |
| 365 | return mExpectedCount; |
Julien Desprez | 96a6a99 | 2018-08-31 10:42:29 -0700 | [diff] [blame] | 366 | } |
Julien Desprez | 0c490df | 2019-01-07 11:34:46 -0800 | [diff] [blame] | 367 | |
| 368 | computeMergedResults(); |
| 369 | mExpectedCount = 0; |
| 370 | for (TestRunResult result : getMergedTestRunResults()) { |
| 371 | mExpectedCount += result.getExpectedTestCount(); |
| 372 | } |
| 373 | return mExpectedCount; |
Julien Desprez | 96a6a99 | 2018-08-31 10:42:29 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 376 | /** Returns the number of tests in given state for this run. */ |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 377 | public int getNumTestsInState(TestStatus status) { |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 378 | computeMergedResults(); |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 379 | return mStatusCounts[status.ordinal()]; |
Brett Chabot | fccfc61 | 2010-03-29 16:08:49 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 382 | /** Returns if the invocation had any failed or assumption failed tests. */ |
Brett Chabot | 057e16e | 2010-06-27 21:53:48 -0700 | [diff] [blame] | 383 | public boolean hasFailedTests() { |
Brett Chabot | 2ea6ede | 2014-08-04 11:15:19 -0700 | [diff] [blame] | 384 | return getNumAllFailedTests() > 0; |
Brett Chabot | 057e16e | 2010-06-27 21:53:48 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 387 | /** Returns the total number of test runs in a failure state */ |
Guang Zhu | a438332 | 2017-11-08 13:55:00 -0800 | [diff] [blame] | 388 | public int getNumAllFailedTestRuns() { |
| 389 | int count = 0; |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 390 | for (TestRunResult result : getMergedTestRunResults()) { |
| 391 | if (result.isRunFailure()) { |
Guang Zhu | a438332 | 2017-11-08 13:55:00 -0800 | [diff] [blame] | 392 | count++; |
| 393 | } |
| 394 | } |
| 395 | return count; |
| 396 | } |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 397 | |
| 398 | /** |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 399 | * Returns the total number of tests in a failure state (only failed, assumption failures do not |
| 400 | * count toward it). |
| 401 | */ |
| 402 | public int getNumAllFailedTests() { |
| 403 | return getNumTestsInState(TestStatus.FAILURE); |
| 404 | } |
| 405 | |
| 406 | /** |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 407 | * Return the merged collection of results for all runs across different attempts. |
| 408 | * |
| 409 | * <p>If there are multiple results, each test run is merged, with the latest test result |
| 410 | * overwriting test results of previous runs. Test runs are ordered by attempt number. |
| 411 | * |
| 412 | * <p>Metrics for the same attempt will be merged based on the preference set by {@code |
| 413 | * aggregate-metrics}. The final metrics will be the metrics of the last attempt. |
| 414 | */ |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 415 | public List<TestRunResult> getMergedTestRunResults() { |
| 416 | computeMergedResults(); |
Julien Desprez | bcd2ff3 | 2018-11-20 18:23:36 -0800 | [diff] [blame] | 417 | return new ArrayList<>(mMergedTestRunResults); |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Returns the results for all test runs. |
| 422 | * |
| 423 | * @deprecated Use {@link #getMergedTestRunResults()} |
| 424 | */ |
| 425 | @Deprecated |
| 426 | public Collection<TestRunResult> getRunResults() { |
| 427 | return getMergedTestRunResults(); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Computes and stores the merged results and the total status counts since both operations are |
| 432 | * expensive. |
| 433 | */ |
Julien Desprez | bcd2ff3 | 2018-11-20 18:23:36 -0800 | [diff] [blame] | 434 | private synchronized void computeMergedResults() { |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 435 | // If not dirty, nothing to be done |
| 436 | if (!mIsCountDirty.compareAndSet(true, false)) { |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 437 | return; |
| 438 | } |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 439 | |
Julien Desprez | bcd2ff3 | 2018-11-20 18:23:36 -0800 | [diff] [blame] | 440 | mMergedTestRunResults.clear(); |
Julien Desprez | 93a041b | 2018-08-29 08:55:27 -0700 | [diff] [blame] | 441 | // Merge results |
| 442 | if (mTestRunResultMap.isEmpty() && mCurrentTestRunResult.isRunFailure()) { |
Julien Desprez | bcd2ff3 | 2018-11-20 18:23:36 -0800 | [diff] [blame] | 443 | // In case of early failure that is a bit untracked, still add it to the list to |
| 444 | // not loose it. |
Julien Desprez | d997789 | 2020-04-14 09:01:15 -0700 | [diff] [blame] | 445 | CLog.e( |
| 446 | "Early failure resulting in no testRunStart. Results might be inconsistent:" |
| 447 | + "\n%s", |
| 448 | mCurrentTestRunResult.getRunFailureMessage()); |
Julien Desprez | 93a041b | 2018-08-29 08:55:27 -0700 | [diff] [blame] | 449 | mMergedTestRunResults.add(mCurrentTestRunResult); |
| 450 | } else { |
Julien Desprez | 7ea0bd4 | 2018-12-05 09:59:28 -0800 | [diff] [blame] | 451 | for (Entry<String, List<TestRunResult>> results : mTestRunResultMap.entrySet()) { |
| 452 | TestRunResult res = TestRunResult.merge(results.getValue(), mStrategy); |
| 453 | if (res == null) { |
| 454 | // Merge can return null in case of results being empty. |
| 455 | CLog.w("No results for %s", results.getKey()); |
| 456 | } else { |
| 457 | mMergedTestRunResults.add(res); |
| 458 | } |
Julien Desprez | 93a041b | 2018-08-29 08:55:27 -0700 | [diff] [blame] | 459 | } |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 460 | } |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 461 | // Reset counts |
| 462 | for (TestStatus s : TestStatus.values()) { |
| 463 | mStatusCounts[s.ordinal()] = 0; |
| 464 | } |
| 465 | |
| 466 | // Calculate results |
| 467 | for (TestRunResult result : mMergedTestRunResults) { |
| 468 | for (TestStatus s : TestStatus.values()) { |
| 469 | mStatusCounts[s.ordinal()] += result.getNumTestsInState(s); |
| 470 | } |
| 471 | } |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 472 | } |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 473 | |
Julien Desprez | 42aa6dd | 2018-12-05 15:56:52 -0800 | [diff] [blame] | 474 | /** |
| 475 | * Keep dirty count as AtomicBoolean to ensure when accessed from another thread the state is |
| 476 | * consistent. |
| 477 | */ |
| 478 | private void setCountDirty() { |
| 479 | mIsCountDirty.set(true); |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Return all the names for all the test runs. |
| 484 | * |
| 485 | * <p>These test runs may have run multiple times with different attempts. |
| 486 | */ |
| 487 | public Collection<String> getTestRunNames() { |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 488 | return new ArrayList<String>(mTestRunResultMap.keySet()); |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /** |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 492 | * Gets all the attempts for a {@link TestRunResult} of a given test run. |
| 493 | * |
| 494 | * @param testRunName The name given by {{@link #testRunStarted(String, int)}. |
| 495 | * @return All {@link TestRunResult} for a given test run, ordered by attempts. |
| 496 | */ |
| 497 | public List<TestRunResult> getTestRunAttempts(String testRunName) { |
| 498 | return mTestRunResultMap.get(testRunName); |
| 499 | } |
| 500 | |
| 501 | /** |
Julien Desprez | 2e19eaa | 2019-07-01 15:29:17 -0700 | [diff] [blame] | 502 | * Gets all the results for a given attempt. |
| 503 | * |
| 504 | * @param attempt The attempt we want results for. |
| 505 | * @return All {@link TestRunResult} for a given attempt. |
| 506 | */ |
| 507 | public List<TestRunResult> getTestRunForAttempts(int attempt) { |
| 508 | List<TestRunResult> allResultForAttempts = new ArrayList<>(); |
| 509 | for (Entry<String, List<TestRunResult>> runInfo : mTestRunResultMap.entrySet()) { |
| 510 | if (attempt < runInfo.getValue().size()) { |
| 511 | TestRunResult attemptRes = runInfo.getValue().get(attempt); |
| 512 | allResultForAttempts.add(attemptRes); |
| 513 | } |
| 514 | } |
| 515 | return allResultForAttempts; |
| 516 | } |
| 517 | |
| 518 | /** |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 519 | * Returns whether a given test run name has any results. |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 520 | * |
| 521 | * @param testRunName The name given by {{@link #testRunStarted(String, int)}. |
| 522 | */ |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 523 | public boolean hasTestRunResultsForName(String testRunName) { |
| 524 | return mTestRunResultMap.containsKey(testRunName); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Returns the number of attempts for a given test run name. |
| 529 | * |
| 530 | * @param testRunName The name given by {{@link #testRunStarted(String, int)}. |
| 531 | */ |
| 532 | public int getTestRunAttemptCount(String testRunName) { |
| 533 | List<TestRunResult> results = mTestRunResultMap.get(testRunName); |
| 534 | if (results == null) { |
| 535 | return 0; |
| 536 | } |
| 537 | return results.size(); |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Return the {@link TestRunResult} for a single attempt. |
| 542 | * |
| 543 | * @param testRunName The name given by {{@link #testRunStarted(String, int)}. |
| 544 | * @param attempt The attempt id. |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 545 | * @return The {@link TestRunResult} for the given name and attempt id or {@code null} if it |
| 546 | * does not exist. |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 547 | */ |
| 548 | public TestRunResult getTestRunAtAttempt(String testRunName, int attempt) { |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 549 | List<TestRunResult> results = mTestRunResultMap.get(testRunName); |
| 550 | if (results == null || attempt < 0 || attempt >= results.size()) { |
| 551 | return null; |
| 552 | } |
| 553 | |
| 554 | return results.get(attempt); |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | /** |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 558 | * Returns the {@link IInvocationContext} of the module associated with the results. |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 559 | * |
| 560 | * @param testRunName The name given by {{@link #testRunStarted(String, int)}. |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 561 | * @return The {@link IInvocationContext} of the module for a given test run name {@code null} |
| 562 | * if there are no results for that name. |
Eric Rowe | 6a19da7 | 2018-07-24 19:13:55 -0700 | [diff] [blame] | 563 | */ |
Eric Rowe | a6d559e | 2018-07-26 12:01:02 -0700 | [diff] [blame] | 564 | public IInvocationContext getModuleContextForRunResult(String testRunName) { |
| 565 | return mModuleContextMap.get(testRunName); |
Eric Rowe | 7d8e0c6 | 2018-07-25 14:27:41 -0700 | [diff] [blame] | 566 | } |
Julien Desprez | 69c2620 | 2019-06-04 09:31:24 -0700 | [diff] [blame] | 567 | |
| 568 | /** Returns a copy of the map containing all the logged file not associated with a test run. */ |
| 569 | public Map<String, LogFile> getNonAssociatedLogFiles() { |
| 570 | return new LinkedHashMap<>(mNonAssociatedLogFiles); |
| 571 | } |
Julien Desprez | e4abca3 | 2019-07-08 14:24:02 -0700 | [diff] [blame] | 572 | |
| 573 | /** |
| 574 | * Allows to clear the results for a given run name. Should only be used in some cases like the |
| 575 | * aggregator of results. |
| 576 | */ |
| 577 | protected final synchronized void clearResultsForName(String testRunName) { |
| 578 | setCountDirty(); |
| 579 | mTestRunResultMap.remove(testRunName); |
| 580 | } |
Brett Chabot | 51a4e3d | 2010-03-09 11:08:44 -0800 | [diff] [blame] | 581 | } |