Ticket #13157: patch-test-fix-dateutils-timeout.patch

File patch-test-fix-dateutils-timeout.patch, 6.1 KB (added by michael2402, 8 years ago)
  • test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    diff --git a/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java b/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java
    index 9fba4cd..f0f5ea2 100644
    a b package org.openstreetmap.josm.testutils;  
    33
    44import java.io.File;
    55import java.io.IOException;
     6import java.text.MessageFormat;
    67import java.util.TimeZone;
    78
    8 import org.junit.rules.DisableOnDebug;
    99import org.junit.rules.TemporaryFolder;
    1010import org.junit.rules.TestRule;
    11 import org.junit.rules.Timeout;
    1211import org.junit.runner.Description;
    1312import org.junit.runners.model.InitializationError;
    1413import org.junit.runners.model.Statement;
    import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;  
    3231 * @author Michael Zangl
    3332 */
    3433public class JOSMTestRules implements TestRule {
    35     private Timeout timeout = Timeout.seconds(10);
     34    private int timeout = 10 * 1000;
    3635    private TemporaryFolder josmHome;
    3736    private boolean usePreferences = false;
    3837    private APIType useAPI = APIType.NONE;
    public class JOSMTestRules implements TestRule {  
    4544     * @return this instance, for easy chaining
    4645     */
    4746    public JOSMTestRules noTimeout() {
    48         timeout = null;
     47        timeout = -1;
    4948        return this;
    5049    }
    5150
    public class JOSMTestRules implements TestRule {  
    5554     * @return this instance, for easy chaining
    5655     */
    5756    public JOSMTestRules timeout(int millis) {
    58         timeout = Timeout.millis(millis);
     57        timeout = millis;
    5958        return this;
    6059    }
    6160
    public class JOSMTestRules implements TestRule {  
    134133    }
    135134
    136135    @Override
    137     public Statement apply(final Statement base, Description description) {
    138         Statement statement = new Statement() {
    139             @Override
    140             public void evaluate() throws Throwable {
    141                 before();
    142                 try {
    143                     base.evaluate();
    144                 } finally {
    145                     after();
    146                 }
    147             }
    148         };
    149         if (timeout != null) {
    150             statement = new DisableOnDebug(timeout).apply(statement, description);
     136    public Statement apply(Statement base, Description description) {
     137        Statement statement = base;
     138        if (timeout > 0) {
     139            // TODO: new DisableOnDebug(timeout)
     140            statement = new FailOnTimeoutStatement(statement, timeout);
    151141        }
     142        statement = new CreateJosmEnvironment(statement);
    152143        if (josmHome != null) {
    153144            statement = josmHome.apply(statement, description);
    154145        }
    public class JOSMTestRules implements TestRule {  
    252243        System.gc();
    253244    }
    254245
     246    private final class CreateJosmEnvironment extends Statement {
     247        private final Statement base;
     248
     249        private CreateJosmEnvironment(Statement base) {
     250            this.base = base;
     251        }
     252
     253        @Override
     254        public void evaluate() throws Throwable {
     255            before();
     256            try {
     257                base.evaluate();
     258            } finally {
     259                after();
     260            }
     261        }
     262    }
     263
    255264    enum APIType {
    256265        NONE, FAKE, DEV
    257266    }
     267
     268    /**
     269     * The junit timeout statement has problems when switchting timezones. This one does not.
     270     * @author Michael Zangl
     271     * @since xxx
     272     */
     273    private static class FailOnTimeoutStatement extends Statement {
     274
     275        private int timeout;
     276        private Statement original;
     277
     278        FailOnTimeoutStatement(Statement original, int timeout) {
     279            this.original = original;
     280            this.timeout = timeout;
     281        }
     282
     283        @Override
     284        public void evaluate() throws Throwable {
     285            TimeoutThread thread = new TimeoutThread(original);
     286            thread.setDaemon(true);
     287            thread.start();
     288            thread.join(timeout);
     289            thread.interrupt();
     290            if (!thread.isDone) {
     291                Throwable exception = thread.getExecutionException();
     292                if (exception != null) {
     293                    throw exception;
     294                } else {
     295                    throw new Exception(MessageFormat.format("Test timed out after {0}ms", timeout));
     296                }
     297            }
     298        }
     299
     300    }
     301
     302    private static class TimeoutThread extends Thread {
     303        public boolean isDone;
     304        private Statement original;
     305        private Throwable exceptionCaught;
     306
     307        private TimeoutThread(Statement original) {
     308            super("Timeout runner");
     309            this.original = original;
     310        }
     311
     312        public Throwable getExecutionException() {
     313            return exceptionCaught;
     314        }
     315
     316        @Override
     317        public void run() {
     318            try {
     319                original.evaluate();
     320                isDone = true;
     321            } catch (Throwable e) {
     322                exceptionCaught = e;
     323            }
     324        }
     325    }
    258326}
  • test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java b/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java
    index 658ba4f..0fcf036 100644
    a b public class ExifReaderTest {  
    3434     */
    3535    @Rule
    3636    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    37     public JOSMTestRules test = new JOSMTestRules().timeout(60000);
     37    public JOSMTestRules test = new JOSMTestRules();
    3838
    3939    private File orientationSampleFile, directionSampleFile;
    4040
  • test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    diff --git a/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java b/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java
    index 06803a7..23373d2 100644
    a b public class DateUtilsTest {  
    2323
    2424    /**
    2525     * Set the timezone and timeout.
     26     * <p>
     27     * Timeouts need to be disabled because we change the time zone.
    2628     */
    2729    @Rule
    2830    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")