diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-08 20:59:02 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-08 20:59:02 +0000 |
commit | b856433e86b51eeb230c727095af5108dad61e86 (patch) | |
tree | 1943561f806d9a257b1fa99a86548073c83d3ed0 /mojo/android/javatests | |
parent | d027020080801b97d742aab193f87f8352216251 (diff) | |
download | chromium_src-b856433e86b51eeb230c727095af5108dad61e86.zip chromium_src-b856433e86b51eeb230c727095af5108dad61e86.tar.gz chromium_src-b856433e86b51eeb230c727095af5108dad61e86.tar.bz2 |
Adding an ExecutorFactory for the bindings.
This will allow to call back the thread owning a connector from the
finalization thread.
This API is private to the bindings code.
R=rmcilroy@chromium.org
Review URL: https://codereview.chromium.org/368923004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281821 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/android/javatests')
-rw-r--r-- | mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java new file mode 100644 index 0000000..40fa43b --- /dev/null +++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/ExecutorFactoryTest.java @@ -0,0 +1,104 @@ +// 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.mojo.bindings; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.chromium.mojo.MojoTestCase; +import org.chromium.mojo.system.impl.CoreImpl; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * Testing the executor factory. + */ +public class ExecutorFactoryTest extends MojoTestCase { + + private static final long RUN_LOOP_TIMEOUT_MS = 50; + private static final int CONCURRENCY_LEVEL = 5; + private static final ExecutorService WORKERS = Executors.newFixedThreadPool(CONCURRENCY_LEVEL); + + private Executor mExecutor; + private List<Thread> mThreadContainer; + + /** + * @see MojoTestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + mExecutor = ExecutorFactory.getExecutorForCurrentThread(CoreImpl.getInstance()); + mThreadContainer = new ArrayList<Thread>(); + } + + /** + * Testing the {@link Executor} when called from the executor thread. + */ + @SmallTest + public void testExecutorOnCurrentThread() { + Runnable action = new Runnable() { + @Override + public void run() { + mThreadContainer.add(Thread.currentThread()); + } + }; + mExecutor.execute(action); + mExecutor.execute(action); + assertEquals(0, mThreadContainer.size()); + nativeRunLoop(RUN_LOOP_TIMEOUT_MS); + assertEquals(2, mThreadContainer.size()); + for (Thread thread : mThreadContainer) { + assertEquals(Thread.currentThread(), thread); + } + } + + /** + * Testing the {@link Executor} when called from another thread. + */ + @SmallTest + public void testExecutorOnOtherThread() { + final CyclicBarrier barrier = new CyclicBarrier(CONCURRENCY_LEVEL + 1); + for (int i = 0; i < CONCURRENCY_LEVEL; ++i) { + WORKERS.execute(new Runnable() { + @Override + public void run() { + mExecutor.execute(new Runnable() { + + @Override + public void run() { + mThreadContainer.add(Thread.currentThread()); + } + }); + try { + barrier.await(); + } catch (InterruptedException e) { + fail("Unexpected exception: " + e.getMessage()); + } catch (BrokenBarrierException e) { + fail("Unexpected exception: " + e.getMessage()); + } + } + }); + } + try { + barrier.await(); + } catch (InterruptedException e) { + fail("Unexpected exception: " + e.getMessage()); + } catch (BrokenBarrierException e) { + fail("Unexpected exception: " + e.getMessage()); + } + assertEquals(0, mThreadContainer.size()); + nativeRunLoop(RUN_LOOP_TIMEOUT_MS); + assertEquals(CONCURRENCY_LEVEL, mThreadContainer.size()); + for (Thread thread : mThreadContainer) { + assertEquals(Thread.currentThread(), thread); + } + } +} |