package com.carrotsearch.randomizedtesting;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

import com.carrotsearch.randomizedtesting.annotations.Timeout;

/**
 * Test global timeout override (-Dtests.timeout=1000!).
 */
public class TestTimeoutOverride extends WithNestedTestClass {
  public static class Nested extends RandomizedTest {
    @Test
    @Timeout(millis = 5000)
    public void testMethod1() {
      assumeRunningNested();
      sleep(10000);
    }
  }

  public static class Nested2 extends RandomizedTest {
    @Test
    @Timeout(millis = 100)
    public void testMethod1() {
      assumeRunningNested();
      sleep(1000);
    }
  }

  @Test
  public void testTimeoutOverride() {
    System.setProperty(SysGlobals.SYSPROP_TIMEOUT(), "200!");
    long start = System.currentTimeMillis();
    Result result = JUnitCore.runClasses(Nested.class);
    long end = System.currentTimeMillis();
    Assert.assertEquals(1, result.getFailureCount());
    Assert.assertTrue(end - start < 3000);
  }
  
  @Test
  public void testDisableTimeout() {
    System.setProperty(SysGlobals.SYSPROP_TIMEOUT(), "0!");

    long start = System.currentTimeMillis();
    Result result = JUnitCore.runClasses(Nested2.class);
    long end = System.currentTimeMillis();
    Assert.assertEquals(0, result.getFailureCount());
    Assert.assertTrue(end - start > 900);
  }
}

@@ -66,24 +66,48 @@
 rk.Assert;%0A%0A
+import org.junit.After;%0A
 import org.j
@@ -1451,10 +1451,108 @@
 0);%0A  %7D%0A
+  %0A  @After%0A  public void cleanup() %7B%0A    System.clearProperty(SysGlobals.SYSPROP_TIMEOUT());%0A  %7D%0A
 %7D%0A

Clean up property after test.