Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / android / javatests / src / org / chromium / mojo / bindings / ExecutorFactoryTest.java
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.mojo.bindings;
6
7 import android.test.suitebuilder.annotation.SmallTest;
8
9 import org.chromium.mojo.MojoTestCase;
10 import org.chromium.mojo.system.impl.CoreImpl;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.BrokenBarrierException;
15 import java.util.concurrent.CyclicBarrier;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19
20 /**
21  * Testing the executor factory.
22  */
23 public class ExecutorFactoryTest extends MojoTestCase {
24
25     private static final long RUN_LOOP_TIMEOUT_MS = 50;
26     private static final int CONCURRENCY_LEVEL = 5;
27     private static final ExecutorService WORKERS = Executors.newFixedThreadPool(CONCURRENCY_LEVEL);
28
29     private Executor mExecutor;
30     private List<Thread> mThreadContainer;
31
32     /**
33      * @see MojoTestCase#setUp()
34      */
35     @Override
36     protected void setUp() throws Exception {
37         super.setUp();
38         mExecutor = ExecutorFactory.getExecutorForCurrentThread(CoreImpl.getInstance());
39         mThreadContainer = new ArrayList<Thread>();
40     }
41
42     /**
43      * Testing the {@link Executor} when called from the executor thread.
44      */
45     @SmallTest
46     public void testExecutorOnCurrentThread() {
47         Runnable action = new Runnable() {
48             @Override
49             public void run() {
50                 mThreadContainer.add(Thread.currentThread());
51             }
52         };
53         mExecutor.execute(action);
54         mExecutor.execute(action);
55         assertEquals(0, mThreadContainer.size());
56         nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
57         assertEquals(2, mThreadContainer.size());
58         for (Thread thread : mThreadContainer) {
59             assertEquals(Thread.currentThread(), thread);
60         }
61     }
62
63     /**
64      * Testing the {@link Executor} when called from another thread.
65      */
66     @SmallTest
67     public void testExecutorOnOtherThread() {
68         final CyclicBarrier barrier = new CyclicBarrier(CONCURRENCY_LEVEL + 1);
69         for (int i = 0; i < CONCURRENCY_LEVEL; ++i) {
70             WORKERS.execute(new Runnable() {
71                 @Override
72                 public void run() {
73                     mExecutor.execute(new Runnable() {
74
75                         @Override
76                         public void run() {
77                             mThreadContainer.add(Thread.currentThread());
78                         }
79                     });
80                     try {
81                         barrier.await();
82                     } catch (InterruptedException e) {
83                         fail("Unexpected exception: " + e.getMessage());
84                     } catch (BrokenBarrierException e) {
85                         fail("Unexpected exception: " + e.getMessage());
86                     }
87                 }
88             });
89         }
90         try {
91             barrier.await();
92         } catch (InterruptedException e) {
93             fail("Unexpected exception: " + e.getMessage());
94         } catch (BrokenBarrierException e) {
95             fail("Unexpected exception: " + e.getMessage());
96         }
97         assertEquals(0, mThreadContainer.size());
98         nativeRunLoop(RUN_LOOP_TIMEOUT_MS);
99         assertEquals(CONCURRENCY_LEVEL, mThreadContainer.size());
100         for (Thread thread : mThreadContainer) {
101             assertEquals(Thread.currentThread(), thread);
102         }
103     }
104 }