package eu.dnetlib.contract.utils;


/**
 * Blocks exiting feed test.
 * @author mhorst
 *
 */
public class FeedTestSleeper {
	
	public static final long DEFAULT_MAX_WAIT_TIME_MS = 5000;
	
	private long maxWaitTime = DEFAULT_MAX_WAIT_TIME_MS;
	
	private ITestSleeperClient client;
	
	@SuppressWarnings("static-access")
	public void waitUntilFinished() throws InterruptedException {
		long startTime = System.currentTimeMillis();
		boolean doFinish = false;
		while (!doFinish) {
			if (client.isProcessingFinished()) {
				doFinish = true;
			} else {
				Thread.currentThread().sleep(1000);
			}
			if (startTime+maxWaitTime<System.currentTimeMillis()) {
				throw new RuntimeException("wait time exceeded maxium of " 
						+ maxWaitTime + " ms.");
			}
		}
	}

	public void setMaxWaitTime(long maxWaitTime) {
		this.maxWaitTime = maxWaitTime;
	}

	public void setClient(ITestSleeperClient client) {
		this.client = client;
	}
	
}
