/**
 *
 */
package test.utils;

import javastrava.api.v3.service.Strava;
import javastrava.api.v3.service.exception.StravaAPIRateLimitException;
import javastrava.api.v3.service.exception.StravaServiceUnavailableException;

/**
 * @author danshannon
 *
 */
public class RateLimitedTestRunner {
	private static Strava strava = null;

	static {
		try {
			strava = TestUtils.strava();
		} catch (final StravaServiceUnavailableException e) {
			waitForServiceRestoration();
		}
	}

	public static void run(final TestCallback t) throws Exception {
		boolean loop = true;
		while (loop) {
			try {
				t.test();
				loop = false;
			} catch (final StravaAPIRateLimitException e) {
				waitForRateLimit();
			} catch (final StravaServiceUnavailableException e) {
				waitForServiceRestoration();
			}
		}
	}

	/**
	 *
	 */
	private static void waitForServiceRestoration() {
		boolean loop = true;
		while (loop) {
			try {
				System.out.println("WARN - Strava temporarily unavailable (503 error) - pausing execution for 60 seconds");
				Thread.sleep(60000l);
			} catch (final InterruptedException e) {
				// ignore
			}
			try {
				strava = TestUtils.strava();
				strava.getAuthenticatedAthlete();
				// If the call works, then we didn't get a service unavailable exception so we're good to go
				loop = false;
			} catch (final StravaServiceUnavailableException e) {
				loop = true;
			}
		}

	}

	private static void waitForRateLimit() {
		boolean loop = true;
		while (loop) {
			try {
				System.out.println("WARN - Rate limit exceeded - pausing test execution for 15 seconds");
				Thread.sleep(15000l);
			} catch (final InterruptedException e) {
				// ignore
			}
			try {
				strava.getAuthenticatedAthlete();
				// If the call to Strava works then we didn't get a rate limit exception so we're good to go
				loop = false;
			} catch (final StravaAPIRateLimitException e) {
				loop = true;
			}
		}
	}
}

@@ -29,49 +29,8 @@
 s;%0A%0A
-import javastrava.api.v3.service.Strava;%0A
 impo
@@ -97,16 +97,16 @@
 eption;%0A
+
 import j
@@ -250,193 +250,8 @@
 r %7B%0A
-%09private static Strava strava = null;%0A%0A%09static %7B%0A%09%09try %7B%0A%09%09%09strava = TestUtils.strava();%0A%09%09%7D catch (final StravaServiceUnavailableException e) %7B%0A%09%09%09waitForServiceRestoration();%0A%09%09%7D%0A%09%7D%0A%0A
 %09pub
@@ -902,17 +902,8 @@
 %09%09%09%09
-strava = 
 Test
@@ -920,20 +920,8 @@
 va()
-;%0A%09%09%09%09strava
 .get
@@ -1434,22 +1434,34 @@
 y %7B%0A%09%09%09%09
+TestUtils.
 strava
+()
 .getAuth

Fix issue javastrava-api #61 See https://github.com/danshannon/javastravav3api/issues/61