Changeset 10569 in josm for trunk


Ignore:
Timestamp:
2016-07-20T00:25:18+02:00 (8 years ago)
Author:
Don-vip
Message:

fix #13157 - Make DateUtilsTest not time out on some time zones (patch by michael2402) - gsoc-core

Location:
trunk/test/unit/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/testutils/JOSMTestRules.java

    r10553 r10569  
    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;
     
    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;
     
    4645     */
    4746    public JOSMTestRules noTimeout() {
    48         timeout = null;
     47        timeout = -1;
    4948        return this;
    5049    }
     
    5655     */
    5756    public JOSMTestRules timeout(int millis) {
    58         timeout = Timeout.millis(millis);
     57        timeout = millis;
    5958        return this;
    6059    }
     
    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);
    151         }
     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);
     141        }
     142        statement = new CreateJosmEnvironment(statement);
    152143        if (josmHome != null) {
    153144            statement = josmHome.apply(statement, description);
     
    254245    }
    255246
     247    private final class CreateJosmEnvironment extends Statement {
     248        private final Statement base;
     249
     250        private CreateJosmEnvironment(Statement base) {
     251            this.base = base;
     252        }
     253
     254        @Override
     255        public void evaluate() throws Throwable {
     256            before();
     257            try {
     258                base.evaluate();
     259            } finally {
     260                after();
     261            }
     262        }
     263    }
     264
    256265    enum APIType {
    257266        NONE, FAKE, DEV
    258267    }
     268
     269    /**
     270     * The junit timeout statement has problems when switchting timezones. This one does not.
     271     * @author Michael Zangl
     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    private static final class TimeoutThread extends Thread {
     302        public boolean isDone;
     303        private Statement original;
     304        private Throwable exceptionCaught;
     305
     306        private TimeoutThread(Statement original) {
     307            super("Timeout runner");
     308            this.original = original;
     309        }
     310
     311        public Throwable getExecutionException() {
     312            return exceptionCaught;
     313        }
     314
     315        @Override
     316        public void run() {
     317            try {
     318                original.evaluate();
     319                isDone = true;
     320            } catch (Throwable e) {
     321                exceptionCaught = e;
     322            }
     323        }
     324    }
    259325}
  • trunk/test/unit/org/openstreetmap/josm/tools/ExifReaderTest.java

    r10513 r10569  
    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;
  • trunk/test/unit/org/openstreetmap/josm/tools/date/DateUtilsTest.java

    r10563 r10569  
    2323
    2424    /**
    25      * Set the timezone and no timeout (junit timeout task seems to have problems with it when switching time zones).
     25     * 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")
    29     public JOSMTestRules test = new JOSMTestRules().i18n().preferences().noTimeout();
     31    public JOSMTestRules test = new JOSMTestRules().i18n().preferences();
    3032
    3133    /**
Note: See TracChangeset for help on using the changeset viewer.