Home | History | Annotate | Download | only in test
      1 /*
      2  *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 package org.appspot.apprtc.test;
     12 
     13 import java.util.concurrent.CountDownLatch;
     14 import java.util.concurrent.TimeUnit;
     15 
     16 import org.appspot.apprtc.util.LooperExecutor;
     17 
     18 import android.test.InstrumentationTestCase;
     19 import android.util.Log;
     20 
     21 public class LooperExecutorTest extends InstrumentationTestCase {
     22   private static final String TAG = "LooperTest";
     23   private static final int WAIT_TIMEOUT = 5000;
     24 
     25   public void testLooperExecutor() throws InterruptedException {
     26     Log.d(TAG, "testLooperExecutor");
     27     final int counter[] = new int[1];
     28     final int expectedCounter = 10;
     29     final CountDownLatch looperDone = new CountDownLatch(1);
     30 
     31     Runnable counterIncRunnable = new Runnable() {
     32       @Override
     33       public void run() {
     34         counter[0]++;
     35         Log.d(TAG, "Run " + counter[0]);
     36       }
     37     };
     38     LooperExecutor executor = new LooperExecutor();
     39 
     40     // Try to execute a counter increment task before starting an executor.
     41     executor.execute(counterIncRunnable);
     42 
     43     // Start the executor and run expected amount of counter increment task.
     44     executor.requestStart();
     45     for (int i = 0; i < expectedCounter; i++) {
     46       executor.execute(counterIncRunnable);
     47     }
     48     executor.execute(new Runnable() {
     49       @Override
     50       public void run() {
     51         looperDone.countDown();
     52       }
     53     });
     54     executor.requestStop();
     55 
     56     // Try to execute a task after stopping the executor.
     57     executor.execute(counterIncRunnable);
     58 
     59     // Wait for final looper task and make sure the counter increment task
     60     // is executed expected amount of times.
     61     looperDone.await(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
     62     assertTrue (looperDone.getCount() == 0);
     63     assertTrue (counter[0] == expectedCounter);
     64 
     65     Log.d(TAG, "testLooperExecutor done");
     66   }
     67 }
     68