Jemmy入门之Waiter

Jemmy中Waiter主要用于等待某个动作做完或者等待某个对象出现。在规定时间没做完的话会抛出超时异常。

/**
 * 可以看到它实现了Waitable, Timeoutable, Outputable这三个接口
 * 不过不是很明白它为啥要实现Waitable
 */
public class Waiter implements Waitable, Timeoutable, Outputable {
	/**
	 * timeout 常量,其中WAIT_TIME一般后来会被覆盖
	 */
	private final static long TIME_DELTA = 10;
	private final static long WAIT_TIME = 60000;
	private final static long AFTER_WAIT_TIME = 0;
 
	private Waitable waitable;
	private long startTime = 0;
	private long endTime = -1;
	private Object result;
	private Timeouts timeouts;
	private String waitingTimeOrigin;
	private TestOut out;
 
	/**
	 * Constructor.
	 * 
	 * @param w
	 *            Waitable object defining waiting criteria.
	 */
	public Waiter(Waitable w) {
		super();
		setTimeouts(JemmyProperties.getProperties().getTimeouts());
		setOutput(JemmyProperties.getProperties().getOutput());
		waitable = w;
	}
       /**
	 * 初始化Waiter的timeout常量
	 */
	static {
		Timeouts.initDefault("Waiter.TimeDelta", TIME_DELTA);
		Timeouts.initDefault("Waiter.WaitingTime", WAIT_TIME);
		Timeouts.initDefault("Waiter.AfterWaitingTime", AFTER_WAIT_TIME);
	}
 
	/**
	 * 这个函数经常用于改写Waiter.WaitingTime,以实现自定义的等待时间
	 * @param timeouts
	 * 传入的timeouts,一般都包含了一些在其他类的静态初始化了的timeout,比如从
         * ComponentOperator传入的话一般会包含WAIT_COMPONENT_TIMEOUT这个timeout
	 * @param useAsWaitingTime
	 * 用来覆盖"Waiter.WaitingTime"的timeout,比如ComponentOperator.WaitComponentTimeout.
	 * @return the cloned timeouts.
	 */
	public Timeouts setTimeoutsToCloneOf(Timeouts timeouts,	String useAsWaitingTime) {
		Timeouts t = timeouts.cloneThis();
		t.setTimeout("Waiter.WaitingTime", t.getTimeout(useAsWaitingTime));
		setTimeouts(t);
		return t;
	}
 
	/**
	 * Waits for not null result of actionProduced method of Waitable
	 * implementation passed into constructor.
	 * 
	 * @param waitableObject
	 *            Object to be passed into actionProduced method.
	 * @return non null result of action.
	 * @throws TimeoutExpiredException
	 * @exception InterruptedException
	 */
	public Object waitAction(Object waitableObject) throws InterruptedException {
                startTime = System.currentTimeMillis();
		long timeDelta = timeouts.getTimeout("Waiter.TimeDelta");
		while ((result = checkActionProduced(waitableObject)) == null) {
                      /**
                        * 每隔多少毫秒检查一次动作是否完成
                        */
			Thread.currentThread().sleep(timeDelta);
                      /**
                        * 超时检查
                        */
			if (timeoutExpired()) {
				throw (new TimeoutExpiredException(getActualDescription()));
			}
		}
		endTime = System.currentTimeMillis();
		Thread.currentThread().sleep(timeouts.getTimeout("Waiter.AfterWaitingTime"));
		return (result);
	}
 
        /**
         * 检查动作是否完成,所以需要等待的动作必须实现Waitable的actionProduced方法。
         */
	private Object checkActionProduced(Object obj) {
		if (waitable != null) {
			return (waitable.actionProduced(obj));
		} else {
			return (actionProduced(obj));
		}
	}
       /**
         * 检查是否超时。
         */
	private boolean timeoutExpired() {
		return (timeFromStart() > timeouts.getTimeout("Waiter.WaitingTime"));
	}
        protected long timeFromStart() {
		return (System.currentTimeMillis() - startTime);
	}
}
1 day ago, this page was being read.

,

Subscribe to Comments