001
014
015 package com.liferay.portal.test.rule.callback;
016
017 import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
018 import com.liferay.portal.kernel.test.util.TestPropsValues;
019 import com.liferay.portal.kernel.util.GetterUtil;
020
021 import org.junit.Assert;
022 import org.junit.runner.Description;
023 import org.junit.runner.RunWith;
024 import org.junit.runner.Runner;
025
026
029 public class CITimeoutTestCallback extends BaseTestCallback<Long, Object> {
030
031 public static final CITimeoutTestCallback INSTANCE =
032 new CITimeoutTestCallback();
033
034 @Override
035 public void doAfterClass(Description description, Long startTime) {
036 long testTime = System.currentTimeMillis() - startTime;
037
038 if (testTime <= TestPropsValues.CI_TEST_TIMEOUT_TIME) {
039 return;
040 }
041
042 String message =
043 description.getClassName() + " spent " + testTime +
044 "ms and surpassed the timeout threshold " +
045 TestPropsValues.CI_TEST_TIMEOUT_TIME + "ms.";
046
047 System.setProperty(_CI_TIMEOUT_TEST_CLASS_MESSAGE, message);
048
049 Assert.fail(
050 message + " Marked it as failed and aborting subsequent tests.");
051 }
052
053 @Override
054 public Long doBeforeClass(Description description) {
055 String message = System.getProperty(_CI_TIMEOUT_TEST_CLASS_MESSAGE);
056
057 if (message != null) {
058 Assert.fail(
059 "Abort running " + description.getClassName() + " due to : " +
060 message);
061 }
062
063 if (!_isArquillianTest(description)) {
064 return System.currentTimeMillis();
065 }
066
067 String startTimeKey =
068 _CI_TIMEOUT_ARQUILLIAN_TEST_START_TIME + description.getClassName();
069
070 String startTimeValue = System.getProperty(startTimeKey);
071
072 if (startTimeValue == null) {
073 long startTime = System.currentTimeMillis();
074
075 System.setProperty(startTimeKey, String.valueOf(startTime));
076
077 return startTime;
078 }
079
080 return GetterUtil.getLongStrict(startTimeValue);
081 }
082
083 private CITimeoutTestCallback() {
084 }
085
086 private boolean _isArquillianTest(Description description) {
087 RunWith runWith = description.getAnnotation(RunWith.class);
088
089 if (runWith == null) {
090 return false;
091 }
092
093 Class<? extends Runner> runnerClass = runWith.value();
094
095 String runnerClassName = runnerClass.getName();
096
097 if (runnerClassName.equals(
098 "com.liferay.arquillian.extension.junit.bridge.junit." +
099 "Arquillian")) {
100
101 return true;
102 }
103
104 return false;
105 }
106
107 private static final String _CI_TIMEOUT_ARQUILLIAN_TEST_START_TIME =
108 "CI_TIMEOUT_ARQUILLIAN_TEST_START_TIME_";
109
110 private static final String _CI_TIMEOUT_TEST_CLASS_MESSAGE =
111 "CI_TIMEOUT_TEST_CLASS_MESSAGE";
112
113 }