| OLD | NEW | 
 | (Empty) | 
|   1 /* |  | 
|   2  * libjingle |  | 
|   3  * Copyright 2015 Google Inc. |  | 
|   4  * |  | 
|   5  * Redistribution and use in source and binary forms, with or without |  | 
|   6  * modification, are permitted provided that the following conditions are met: |  | 
|   7  * |  | 
|   8  *  1. Redistributions of source code must retain the above copyright notice, |  | 
|   9  *     this list of conditions and the following disclaimer. |  | 
|  10  *  2. Redistributions in binary form must reproduce the above copyright notice, |  | 
|  11  *     this list of conditions and the following disclaimer in the documentation |  | 
|  12  *     and/or other materials provided with the distribution. |  | 
|  13  *  3. The name of the author may not be used to endorse or promote products |  | 
|  14  *     derived from this software without specific prior written permission. |  | 
|  15  * |  | 
|  16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |  | 
|  17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |  | 
|  18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |  | 
|  19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |  | 
|  20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |  | 
|  21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |  | 
|  22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |  | 
|  23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |  | 
|  24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |  | 
|  25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |  | 
|  26  */ |  | 
|  27  |  | 
|  28 package org.appspot.apprtc.test; |  | 
|  29  |  | 
|  30 import java.util.concurrent.CountDownLatch; |  | 
|  31 import java.util.concurrent.TimeUnit; |  | 
|  32  |  | 
|  33 import org.appspot.apprtc.util.LooperExecutor; |  | 
|  34  |  | 
|  35 import android.test.InstrumentationTestCase; |  | 
|  36 import android.util.Log; |  | 
|  37  |  | 
|  38 public class LooperExecutorTest extends InstrumentationTestCase { |  | 
|  39   private static final String TAG = "LooperTest"; |  | 
|  40   private static final int WAIT_TIMEOUT = 5000; |  | 
|  41  |  | 
|  42   public void testLooperExecutor() throws InterruptedException { |  | 
|  43     Log.d(TAG, "testLooperExecutor"); |  | 
|  44     final int counter[] = new int[1]; |  | 
|  45     final int expectedCounter = 10; |  | 
|  46     final CountDownLatch looperDone = new CountDownLatch(1); |  | 
|  47  |  | 
|  48     Runnable counterIncRunnable = new Runnable() { |  | 
|  49       @Override |  | 
|  50       public void run() { |  | 
|  51         counter[0]++; |  | 
|  52         Log.d(TAG, "Run " + counter[0]); |  | 
|  53       } |  | 
|  54     }; |  | 
|  55     LooperExecutor executor = new LooperExecutor(); |  | 
|  56  |  | 
|  57     // Try to execute a counter increment task before starting an executor. |  | 
|  58     executor.execute(counterIncRunnable); |  | 
|  59  |  | 
|  60     // Start the executor and run expected amount of counter increment task. |  | 
|  61     executor.requestStart(); |  | 
|  62     for (int i = 0; i < expectedCounter; i++) { |  | 
|  63       executor.execute(counterIncRunnable); |  | 
|  64     } |  | 
|  65     executor.execute(new Runnable() { |  | 
|  66       @Override |  | 
|  67       public void run() { |  | 
|  68         looperDone.countDown(); |  | 
|  69       } |  | 
|  70     }); |  | 
|  71     executor.requestStop(); |  | 
|  72  |  | 
|  73     // Try to execute a task after stopping the executor. |  | 
|  74     executor.execute(counterIncRunnable); |  | 
|  75  |  | 
|  76     // Wait for final looper task and make sure the counter increment task |  | 
|  77     // is executed expected amount of times. |  | 
|  78     looperDone.await(WAIT_TIMEOUT, TimeUnit.MILLISECONDS); |  | 
|  79     assertTrue (looperDone.getCount() == 0); |  | 
|  80     assertTrue (counter[0] == expectedCounter); |  | 
|  81  |  | 
|  82     Log.d(TAG, "testLooperExecutor done"); |  | 
|  83   } |  | 
|  84 } |  | 
| OLD | NEW |