mobile/android/tests/background/junit3/src/testhelpers/WaitHelper.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 package org.mozilla.gecko.background.testhelpers;
michael@0 5
michael@0 6 import java.util.concurrent.ArrayBlockingQueue;
michael@0 7 import java.util.concurrent.BlockingQueue;
michael@0 8 import java.util.concurrent.TimeUnit;
michael@0 9
michael@0 10 import org.mozilla.gecko.background.common.log.Logger;
michael@0 11
michael@0 12 /**
michael@0 13 * Implements waiting for asynchronous test events.
michael@0 14 *
michael@0 15 * Call WaitHelper.getTestWaiter() to get the unique instance.
michael@0 16 *
michael@0 17 * Call performWait(runnable) to execute runnable synchronously.
michael@0 18 * runnable *must* call performNotify() on all exit paths to signal to
michael@0 19 * the TestWaiter that the runnable has completed.
michael@0 20 *
michael@0 21 * @author rnewman
michael@0 22 * @author nalexander
michael@0 23 */
michael@0 24 public class WaitHelper {
michael@0 25
michael@0 26 public static final String LOG_TAG = "WaitHelper";
michael@0 27
michael@0 28 public static class Result {
michael@0 29 public Throwable error;
michael@0 30 public Result() {
michael@0 31 error = null;
michael@0 32 }
michael@0 33
michael@0 34 public Result(Throwable error) {
michael@0 35 this.error = error;
michael@0 36 }
michael@0 37 }
michael@0 38
michael@0 39 public static abstract class WaitHelperError extends Error {
michael@0 40 private static final long serialVersionUID = 7074690961681883619L;
michael@0 41 }
michael@0 42
michael@0 43 /**
michael@0 44 * Immutable.
michael@0 45 *
michael@0 46 * @author rnewman
michael@0 47 */
michael@0 48 public static class TimeoutError extends WaitHelperError {
michael@0 49 private static final long serialVersionUID = 8591672555848651736L;
michael@0 50 public final int waitTimeInMillis;
michael@0 51
michael@0 52 public TimeoutError(int waitTimeInMillis) {
michael@0 53 this.waitTimeInMillis = waitTimeInMillis;
michael@0 54 }
michael@0 55 }
michael@0 56
michael@0 57 public static class MultipleNotificationsError extends WaitHelperError {
michael@0 58 private static final long serialVersionUID = -9072736521571635495L;
michael@0 59 }
michael@0 60
michael@0 61 public static class InterruptedError extends WaitHelperError {
michael@0 62 private static final long serialVersionUID = 8383948170038639308L;
michael@0 63 }
michael@0 64
michael@0 65 public static class InnerError extends WaitHelperError {
michael@0 66 private static final long serialVersionUID = 3008502618576773778L;
michael@0 67 public Throwable innerError;
michael@0 68
michael@0 69 public InnerError(Throwable e) {
michael@0 70 innerError = e;
michael@0 71 if (e != null) {
michael@0 72 // Eclipse prints the stack trace of the cause.
michael@0 73 this.initCause(e);
michael@0 74 }
michael@0 75 }
michael@0 76 }
michael@0 77
michael@0 78 public BlockingQueue<Result> queue = new ArrayBlockingQueue<Result>(1);
michael@0 79
michael@0 80 /**
michael@0 81 * How long performWait should wait for, in milliseconds, with the
michael@0 82 * convention that a negative value means "wait forever".
michael@0 83 */
michael@0 84 public static int defaultWaitTimeoutInMillis = -1;
michael@0 85
michael@0 86 public void performWait(Runnable action) throws WaitHelperError {
michael@0 87 this.performWait(defaultWaitTimeoutInMillis, action);
michael@0 88 }
michael@0 89
michael@0 90 public void performWait(int waitTimeoutInMillis, Runnable action) throws WaitHelperError {
michael@0 91 Logger.debug(LOG_TAG, "performWait called.");
michael@0 92
michael@0 93 Result result = null;
michael@0 94
michael@0 95 try {
michael@0 96 if (action != null) {
michael@0 97 try {
michael@0 98 action.run();
michael@0 99 Logger.debug(LOG_TAG, "Action done.");
michael@0 100 } catch (Exception ex) {
michael@0 101 Logger.debug(LOG_TAG, "Performing action threw: " + ex.getMessage());
michael@0 102 throw new InnerError(ex);
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 if (waitTimeoutInMillis < 0) {
michael@0 107 result = queue.take();
michael@0 108 } else {
michael@0 109 result = queue.poll(waitTimeoutInMillis, TimeUnit.MILLISECONDS);
michael@0 110 }
michael@0 111 Logger.debug(LOG_TAG, "Got result from queue: " + result);
michael@0 112 } catch (InterruptedException e) {
michael@0 113 // We were interrupted.
michael@0 114 Logger.debug(LOG_TAG, "performNotify interrupted with InterruptedException " + e);
michael@0 115 final InterruptedError interruptedError = new InterruptedError();
michael@0 116 interruptedError.initCause(e);
michael@0 117 throw interruptedError;
michael@0 118 }
michael@0 119
michael@0 120 if (result == null) {
michael@0 121 // We timed out.
michael@0 122 throw new TimeoutError(waitTimeoutInMillis);
michael@0 123 } else if (result.error != null) {
michael@0 124 Logger.debug(LOG_TAG, "Notified with error: " + result.error.getMessage());
michael@0 125
michael@0 126 // Rethrow any assertion with which we were notified.
michael@0 127 InnerError innerError = new InnerError(result.error);
michael@0 128 throw innerError;
michael@0 129 }
michael@0 130 // Success!
michael@0 131 }
michael@0 132
michael@0 133 public void performNotify(final Throwable e) {
michael@0 134 if (e != null) {
michael@0 135 Logger.debug(LOG_TAG, "performNotify called with Throwable: " + e.getMessage());
michael@0 136 } else {
michael@0 137 Logger.debug(LOG_TAG, "performNotify called.");
michael@0 138 }
michael@0 139
michael@0 140 if (!queue.offer(new Result(e))) {
michael@0 141 // This could happen if performNotify is called multiple times (which is an error).
michael@0 142 throw new MultipleNotificationsError();
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 public void performNotify() {
michael@0 147 this.performNotify(null);
michael@0 148 }
michael@0 149
michael@0 150 public static Runnable onThreadRunnable(final Runnable r) {
michael@0 151 return new Runnable() {
michael@0 152 @Override
michael@0 153 public void run() {
michael@0 154 new Thread(r).start();
michael@0 155 }
michael@0 156 };
michael@0 157 }
michael@0 158
michael@0 159 private static WaitHelper singleWaiter = new WaitHelper();
michael@0 160 public static WaitHelper getTestWaiter() {
michael@0 161 return singleWaiter;
michael@0 162 }
michael@0 163
michael@0 164 public static void resetTestWaiter() {
michael@0 165 singleWaiter = new WaitHelper();
michael@0 166 }
michael@0 167
michael@0 168 public boolean isIdle() {
michael@0 169 return queue.isEmpty();
michael@0 170 }
michael@0 171 }

mercurial