Merge "Improve resiliency of message parser in the case that events are missing" into marshmallow-cts-dev
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/DeqpTestRunner.java b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/DeqpTestRunner.java
index 885f267..7d2c340 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/DeqpTestRunner.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/DeqpTestRunner.java
@@ -421,6 +421,12 @@
         private boolean handleBeginTestCase(Map<String, String> values) {
             String casePath = values.get("dEQP-BeginTestCase-TestCasePath");
 
+            if (mCurrentTestId != null) {
+                    CLog.w("Got unexpected start of %s, so aborting", mCurrentTestId);
+                    abortTest(mCurrentTestId, INCOMPLETE_LOG_MESSAGE);
+                    mCurrentTestId = null;
+            }
+
             mCurrentTestLog = "";
             mGotTestResult = false;
 
@@ -432,10 +438,7 @@
 
             mCurrentTestId = pathToIdentifier(casePath);
 
-            // mark instance as started
-            if (mPendingResults.get(mCurrentTestId) != null) {
-                mPendingResults.get(mCurrentTestId).remainingConfigs.remove(mRunConfig);
-            } else {
+            if (mPendingResults.get(mCurrentTestId) == null) {
                 CLog.w("Got unexpected start of %s", mCurrentTestId);
             }
             return true;
@@ -453,6 +456,7 @@
                     result.errorMessages.put(mRunConfig, INCOMPLETE_LOG_MESSAGE);
                     CLog.i("Test %s failed as it ended before receiving result.", mCurrentTestId);
                 }
+                result.remainingConfigs.remove(mRunConfig);
 
                 if (mLogData && mCurrentTestLog != null && mCurrentTestLog.length() > 0) {
                     result.testLogs.put(mRunConfig, mCurrentTestLog);
@@ -523,6 +527,7 @@
                 mPendingResults.get(mCurrentTestId).allInstancesPassed = false;
                 mPendingResults.get(mCurrentTestId)
                         .errorMessages.put(mRunConfig, "Terminated: " + reason);
+                result.remainingConfigs.remove(mRunConfig);
 
                 // Pending result finished, report result
                 if (result.remainingConfigs.isEmpty()) {