| Index: components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/SignalingThreadMock.java
|
| diff --git a/components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/SignalingThreadMock.java b/components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/SignalingThreadMock.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..518efa7d06ced0bb8bbefffdca0447061c377a61
|
| --- /dev/null
|
| +++ b/components/devtools_bridge/test/android/javatests/src/org/chromium/components/devtools_bridge/SignalingThreadMock.java
|
| @@ -0,0 +1,115 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.components.devtools_bridge;
|
| +
|
| +import java.util.concurrent.Callable;
|
| +import java.util.concurrent.ExecutionException;
|
| +import java.util.concurrent.ExecutorService;
|
| +import java.util.concurrent.Executors;
|
| +import java.util.concurrent.ScheduledExecutorService;
|
| +import java.util.concurrent.ScheduledFuture;
|
| +import java.util.concurrent.TimeUnit;
|
| +import java.util.concurrent.atomic.AtomicInteger;
|
| +
|
| +/**
|
| + * Convinience class for tests. Like WebRTC threads supports posts
|
| + * and synchromous invokes.
|
| + */
|
| +class SignalingThreadMock {
|
| + private static final int EXECUTION_TIME_LIMIT_MS = 5000;
|
| +
|
| + private final AtomicInteger mInvokationCounter = new AtomicInteger(0);
|
| + private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
|
| + private final ScheduledExecutorService mWatchDogExecutor =
|
| + Executors.newSingleThreadScheduledExecutor();
|
| + private ScheduledFuture<?> mWatchDogFuture;
|
| + private volatile Thread mThread;
|
| +
|
| + public void invoke(final Runnable runnable) {
|
| + try {
|
| + invoke(new RunnableAdaptor(runnable));
|
| + } catch (Exception e) {
|
| + throw new RuntimeException(e);
|
| + }
|
| + }
|
| +
|
| + public <T> T invoke(final Callable<T> callable) throws Exception {
|
| + if (isOnThread())
|
| + return callable.call();
|
| +
|
| + try {
|
| + return mExecutor.submit(new CallableWrapper<T>(callable)).get();
|
| + } catch (InterruptedException e) {
|
| + throw new RuntimeException(e);
|
| + } catch (ExecutionException e) {
|
| + throw (Exception) e.getCause();
|
| + }
|
| + }
|
| +
|
| + public void post(Runnable runnable) {
|
| + mExecutor.submit(new CallableWrapper<Object>(new RunnableAdaptor(runnable)));
|
| + }
|
| +
|
| + public void dispose() {
|
| + mExecutor.shutdown();
|
| + mWatchDogExecutor.shutdown();
|
| + }
|
| +
|
| + public boolean isOnThread() {
|
| + return Thread.currentThread() == mThread;
|
| + }
|
| +
|
| + private void onStartedExecution(final int index, final Exception timeoutException) {
|
| + mWatchDogFuture = mWatchDogExecutor.schedule(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + throw new RuntimeException(
|
| + "Time limit on " + Integer.toString(index) + " invocation",
|
| + timeoutException);
|
| + }
|
| + }, EXECUTION_TIME_LIMIT_MS, TimeUnit.MILLISECONDS);
|
| + mThread = Thread.currentThread();
|
| + }
|
| +
|
| + private void onFinishedExecution() {
|
| + mThread = null;
|
| + mWatchDogFuture.cancel(false);
|
| + }
|
| +
|
| + private class CallableWrapper<T> implements Callable<T> {
|
| + private final Callable<T> mWrapped;
|
| + private final int mIndex = mInvokationCounter.incrementAndGet();
|
| + private final Exception mTimeoutException;
|
| +
|
| + public CallableWrapper(Callable<T> wrapped) {
|
| + mWrapped = wrapped;
|
| + mTimeoutException = new Exception();
|
| + }
|
| +
|
| + @Override
|
| + public T call() throws Exception {
|
| + try {
|
| + onStartedExecution(mIndex, mTimeoutException);
|
| + return mWrapped.call();
|
| + } finally {
|
| + onFinishedExecution();
|
| + }
|
| + }
|
| + }
|
| +
|
| + private class RunnableAdaptor implements Callable<Object> {
|
| + private final Runnable mAdaptee;
|
| +
|
| + public RunnableAdaptor(Runnable adaptee) {
|
| + mAdaptee = adaptee;
|
| + }
|
| +
|
| + @Override
|
| + public Object call() {
|
| + mAdaptee.run();
|
| + return null;
|
| + }
|
| + }
|
| +}
|
|
|