parent
abe74ebf35
commit
a4f958a43d
@ -0,0 +1,7 @@ |
|||||||
|
package com.otaliastudios.cameraview.tools; |
||||||
|
|
||||||
|
import java.lang.annotation.Retention; |
||||||
|
import java.lang.annotation.RetentionPolicy; |
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME) |
||||||
|
public @interface Retry {} |
@ -0,0 +1,46 @@ |
|||||||
|
package com.otaliastudios.cameraview.tools; |
||||||
|
|
||||||
|
import com.otaliastudios.cameraview.CameraLogger; |
||||||
|
|
||||||
|
import org.junit.rules.TestRule; |
||||||
|
import org.junit.runner.Description; |
||||||
|
import org.junit.runners.model.Statement; |
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger; |
||||||
|
|
||||||
|
public class RetryRule implements TestRule { |
||||||
|
|
||||||
|
private final static String TAG = RetryRule.class.getSimpleName(); |
||||||
|
private final static CameraLogger LOG = CameraLogger.create(TAG); |
||||||
|
|
||||||
|
private AtomicInteger retries; |
||||||
|
|
||||||
|
public RetryRule(int retries) { |
||||||
|
this.retries = new AtomicInteger(retries); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Statement apply(final Statement base, final Description description) { |
||||||
|
return new Statement() { |
||||||
|
@Override |
||||||
|
public void evaluate() throws Throwable { |
||||||
|
if (description.getAnnotation(Retry.class) == null) { |
||||||
|
base.evaluate(); |
||||||
|
} else { |
||||||
|
Throwable caught = null; |
||||||
|
while (retries.getAndDecrement() > 0) { |
||||||
|
try { |
||||||
|
base.evaluate(); |
||||||
|
return; |
||||||
|
} catch (Throwable throwable) { |
||||||
|
caught = throwable; |
||||||
|
} |
||||||
|
} |
||||||
|
if (caught != null) { |
||||||
|
throw caught; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue