aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/invoker/ShardListener.java
blob: ea0f1fb31182a77c67b80654c38fd05457334f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tradefed.invoker;

import com.android.ddmlib.Log.LogLevel;
import com.android.ddmlib.testrunner.TestIdentifier;
import com.android.ddmlib.testrunner.TestResult;
import com.android.ddmlib.testrunner.TestResult.TestStatus;
import com.android.ddmlib.testrunner.TestRunResult;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.result.CollectingTestListener;
import com.android.tradefed.result.ITestInvocationListener;
import com.android.tradefed.result.InputStreamSource;
import com.android.tradefed.result.LogDataType;
import com.android.tradefed.util.TimeUtil;

import java.util.Collection;
import java.util.Map;

/**
 * A {@link ITestInvocationListener} that collects results from a invocation shard (aka an
 * invocation split to run on multiple resources in parallel), and forwards them to another
 * listener.
 */
public class ShardListener extends CollectingTestListener {

    private ITestInvocationListener mMasterListener;

    /**
     * Create a {@link ShardListener}.
     *
     * @param master the {@link ITestInvocationListener} the results should be forwarded. To prevent
     *     collisions with other {@link ShardListener}s, this object will synchronize on
     *     <var>master</var> when forwarding results. And results will only be sent once the
     *     invocation shard completes.
     */
    public ShardListener(ITestInvocationListener master) {
        mMasterListener = master;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void invocationStarted(IInvocationContext context) {
        super.invocationStarted(context);
        synchronized (mMasterListener) {
            mMasterListener.invocationStarted(context);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void invocationFailed(Throwable cause) {
        super.invocationFailed(cause);
        synchronized (mMasterListener) {
            mMasterListener.invocationFailed(cause);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void testLog(String dataName, LogDataType dataType, InputStreamSource dataStream) {
        // forward testLog results immediately, since they are not order dependent and there are
        // not stored by CollectingTestListener
        synchronized (mMasterListener) {
            mMasterListener.testLog(dataName, dataType, dataStream);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void testRunEnded(long elapsedTime, Map<String, String> runMetrics) {
        super.testRunEnded(elapsedTime, runMetrics);
        CLog.logAndDisplay(LogLevel.INFO, "Sharded test completed: %s",
                getCurrentRunResults().getName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void testRunFailed(String failureMessage) {
        super.testRunFailed(failureMessage);
        CLog.logAndDisplay(LogLevel.ERROR, "FAILED: %s failed with message: %s",
                getCurrentRunResults().getName(), failureMessage);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void invocationEnded(long elapsedTime) {
        super.invocationEnded(elapsedTime);
        synchronized (mMasterListener) {
            logShardContent(getRunResults());
            for (TestRunResult runResult : getRunResults()) {
                mMasterListener.testRunStarted(runResult.getName(), runResult.getNumTests());
                forwardTestResults(runResult.getTestResults());
                if (runResult.isRunFailure()) {
                    mMasterListener.testRunFailed(runResult.getRunFailureMessage());
                }
                mMasterListener.testRunEnded(runResult.getElapsedTime(), runResult.getRunMetrics());
            }
            mMasterListener.invocationEnded(elapsedTime);
        }
    }

    private void forwardTestResults(Map<TestIdentifier, TestResult> testResults) {
        for (Map.Entry<TestIdentifier, TestResult> testEntry : testResults.entrySet()) {
            mMasterListener.testStarted(testEntry.getKey(), testEntry.getValue().getStartTime());
            switch (testEntry.getValue().getStatus()) {
                case FAILURE:
                    mMasterListener.testFailed(testEntry.getKey(),
                            testEntry.getValue().getStackTrace());
                    break;
                case ASSUMPTION_FAILURE:
                    mMasterListener.testAssumptionFailure(testEntry.getKey(),
                            testEntry.getValue().getStackTrace());
                    break;
                case IGNORED:
                    mMasterListener.testIgnored(testEntry.getKey());
                    break;
                default:
                    break;
            }
            if (!testEntry.getValue().getStatus().equals(TestStatus.INCOMPLETE)) {
                mMasterListener.testEnded(
                        testEntry.getKey(),
                        testEntry.getValue().getEndTime(),
                        testEntry.getValue().getMetrics());
            }
        }
    }

    /** Log the content of the shard for easier debugging. */
    private void logShardContent(Collection<TestRunResult> listResults) {
        CLog.d("=================================================");
        CLog.d(
                "========== Shard Primary Device %s ==========",
                getInvocationContext().getDevices().get(0).getSerialNumber());
        for (TestRunResult runRes : listResults) {
            CLog.d(
                    "\tRan '%s' in %s",
                    runRes.getName(), TimeUtil.formatElapsedTime(runRes.getElapsedTime()));
        }
        CLog.d("=================================================");
    }
}