001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.context.junit4.statements;
018
019import java.lang.reflect.Method;
020import java.util.concurrent.TimeoutException;
021
022import org.junit.runners.model.Statement;
023
024import org.springframework.test.annotation.TestAnnotationUtils;
025import org.springframework.util.Assert;
026
027/**
028 * {@code SpringFailOnTimeout} is a custom JUnit {@link Statement} which adds
029 * support for Spring's {@link org.springframework.test.annotation.Timed @Timed}
030 * annotation by throwing an exception if the next statement in the execution
031 * chain takes more than the specified number of milliseconds.
032 *
033 * <p>In contrast to JUnit's
034 * {@link org.junit.internal.runners.statements.FailOnTimeout FailOnTimeout},
035 * the next {@code statement} will be executed in the same thread as the
036 * caller and will therefore not be aborted preemptively.
037 *
038 * @author Sam Brannen
039 * @since 3.0
040 * @see #evaluate()
041 */
042public class SpringFailOnTimeout extends Statement {
043
044        private final Statement next;
045
046        private final long timeout;
047
048
049        /**
050         * Construct a new {@code SpringFailOnTimeout} statement for the supplied
051         * {@code testMethod}, retrieving the configured timeout from the
052         * {@code @Timed} annotation on the supplied method.
053         * @param next the next {@code Statement} in the execution chain
054         * @param testMethod the current test method
055         * @see TestAnnotationUtils#getTimeout(Method)
056         */
057        public SpringFailOnTimeout(Statement next, Method testMethod) {
058                this(next, TestAnnotationUtils.getTimeout(testMethod));
059        }
060
061        /**
062         * Construct a new {@code SpringFailOnTimeout} statement for the supplied
063         * {@code timeout}.
064         * <p>If the supplied {@code timeout} is {@code 0}, the execution of the
065         * {@code next} statement will not be timed.
066         * @param next the next {@code Statement} in the execution chain; never {@code null}
067         * @param timeout the configured {@code timeout} for the current test, in milliseconds;
068         * never negative
069         */
070        public SpringFailOnTimeout(Statement next, long timeout) {
071                Assert.notNull(next, "next statement must not be null");
072                Assert.isTrue(timeout >= 0, "timeout must be non-negative");
073                this.next = next;
074                this.timeout = timeout;
075        }
076
077
078        /**
079         * Evaluate the next {@link Statement statement} in the execution chain
080         * (typically an instance of {@link SpringRepeat}) and throw a
081         * {@link TimeoutException} if the next {@code statement} executes longer
082         * than the specified {@code timeout}.
083         */
084        @Override
085        public void evaluate() throws Throwable {
086                if (this.timeout == 0) {
087                        this.next.evaluate();
088                }
089                else {
090                        long startTime = System.currentTimeMillis();
091                        this.next.evaluate();
092                        long elapsed = System.currentTimeMillis() - startTime;
093                        if (elapsed > this.timeout) {
094                                throw new TimeoutException(
095                                                String.format("Test took %s ms; limit was %s ms.", elapsed, this.timeout));
096                        }
097                }
098        }
099
100}