1 /* 2 * Copyright (C) 2017 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 android.host.test.longevity.listener; 17 18 import static java.util.concurrent.TimeUnit.MINUTES; 19 20 import java.util.Map; 21 22 import org.junit.runner.Description; 23 import org.junit.runner.notification.RunNotifier; 24 25 /** 26 * A {@link RunTerminator} for terminating early on test end due to long duration for host suites. 27 */ 28 public class TimeoutTerminator extends RunTerminator { 29 public static final String OPTION = "suite-timeout_msec"; 30 protected static final long DEFAULT = MINUTES.toMillis(30L); 31 protected static final long UNSET_TIMESTAMP = -1; 32 33 protected long mStartTimestamp = UNSET_TIMESTAMP; 34 protected long mSuiteTimeout; // ms 35 TimeoutTerminator(RunNotifier notifier, Map<String, String> args)36 public TimeoutTerminator(RunNotifier notifier, Map<String, String> args) { 37 super(notifier); 38 mSuiteTimeout = args.containsKey(OPTION) ? Long.parseLong(args.get(OPTION)) : DEFAULT; 39 } 40 41 /** 42 * {@inheritDoc} 43 * 44 * <p>Note: this initializes the countdown timer if unset. 45 */ 46 @Override testStarted(Description description)47 public void testStarted(Description description) { 48 if (mStartTimestamp == UNSET_TIMESTAMP) { 49 mStartTimestamp = getCurrentTimestamp(); 50 } 51 } 52 53 /** {@inheritDoc} */ 54 @Override testFinished(Description description)55 public void testFinished(Description description) { 56 if (mStartTimestamp != UNSET_TIMESTAMP 57 && (getCurrentTimestamp() - mStartTimestamp) > mSuiteTimeout) { 58 kill("the suite timed out"); 59 } 60 } 61 62 /** Returns the total timeout set for the suite. */ getTotalSuiteTimeoutMs()63 public long getTotalSuiteTimeoutMs() { 64 return mSuiteTimeout; 65 } 66 67 /** 68 * Returns the current timestamp in ms for calculating time differences. 69 * <p> 70 * Note: this is exposed for the platform-specific {@code TimeoutTerminator}. 71 */ getCurrentTimestamp()72 protected long getCurrentTimestamp() { 73 return System.currentTimeMillis(); 74 } 75 } 76