All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.sphere.sdk.test.RetryIntegrationTest Maven / Gradle / Ivy

package io.sphere.sdk.test;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.slf4j.Logger;

public class RetryIntegrationTest implements TestRule {
    private final int maxAttempts;
    private final int millisBetweenAttempts;
    private final Logger logger;

    public RetryIntegrationTest(final int maxAttempts, final int millisBetweenAttempts, final Logger logger) {
        this.maxAttempts = maxAttempts;
        this.millisBetweenAttempts = millisBetweenAttempts;
        this.logger = logger;
    }

    @Override
    public Statement apply(final Statement test, final Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                Throwable lastThrowable = null;
                for (int attempt = 0; attempt < maxAttempts; attempt++) {
                    try {
                        Thread.sleep(millisBetweenAttempts);
                        test.evaluate();
                        return;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (Throwable t) {
                        logger.info(String.format("%s failed %d/%d", description.getMethodName(), attempt + 1, maxAttempts));
                        lastThrowable = t;
                    }
                }
                throw lastThrowable;
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy