package eu.dnetlib.contract.utils;


/**
 * Blocks exiting feed test.
 * @author mhorst
 *
 */
public class GenericTestSleeper {
	
	public static final long DEFAULT_MAX_WAIT_TIME_MS = 5000;
	
	public static final String FEED_TYPE = "FEED";
	
	public static final String DELETE_TYPE = "DELETE";
	
	protected String sleeperType;
	
	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(sleeperType)) {
				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;
	}

	public void setSleeperType(String sleeperType) {
		this.sleeperType = sleeperType;
	}
	
}
