Created
December 16, 2015 01:03
-
-
Save sks/28f1b8bc8db6f044dc93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.jodah.sarge.functional; | |
import static org.testng.Assert.assertEquals; | |
import static org.testng.Assert.assertTrue; | |
import static org.testng.Assert.fail; | |
import org.testng.annotations.Test; | |
import net.jodah.sarge.AbstractTest; | |
import net.jodah.sarge.Plans; | |
import net.jodah.sarge.Sarge; | |
import net.jodah.sarge.util.Duration; | |
/** | |
* @author Jonathan Halterman | |
*/ | |
@Test(groups = "slow") | |
public class BackoffTestTwice extends AbstractTest | |
{ | |
private static int counter; | |
static class Foo | |
{ | |
void doSomething() | |
{ | |
counter++; | |
throw new IllegalStateException(); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
Foo foo = new Sarge().supervised(Foo.class, Plans.retryOn(IllegalStateException.class, 5, Duration.inf(), | |
Duration.millis(100), Duration.millis(800))); | |
try | |
{ | |
foo.doSomething(); | |
} | |
catch (Exception e) | |
{ | |
System.err.println(e); | |
} | |
System.out.println(counter); | |
} | |
public void shouldBackoff() | |
{ | |
Foo foo = sarge.supervised(Foo.class, Plans.retryOn(IllegalStateException.class, 5, Duration.inf(), | |
Duration.millis(100), Duration.millis(800))); | |
long startTime = System.currentTimeMillis(); | |
try | |
{ | |
foo.doSomething(); | |
fail(); | |
} | |
catch (IllegalStateException expected) | |
{ | |
} | |
try | |
{ | |
foo.doSomething(); | |
fail(); | |
} | |
catch (IllegalStateException expected) | |
{ | |
} | |
assertEquals(counter, 12); | |
//Above line This fails as the counter value is only 7 , the second time foo.doSomething() was called, Sarge did no pick it up | |
assertTrue(System.currentTimeMillis() - startTime > 2300); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment