Home | History | Annotate | Download | only in executor
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.arch.core.executor;
     18 
     19 import androidx.annotation.RestrictTo;
     20 
     21 import org.junit.rules.TestRule;
     22 import org.junit.runner.Description;
     23 import org.junit.runners.model.MultipleFailureException;
     24 import org.junit.runners.model.Statement;
     25 import org.mockito.Mockito;
     26 
     27 import java.util.List;
     28 
     29 /**
     30  * A JUnit rule that swaps the task executor with a more controllable one.
     31  * Once we have the TaskExecutor API, we should consider making this public (via some test package).
     32  *
     33  * @hide
     34  */
     35 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
     36 public class JunitTaskExecutorRule implements TestRule {
     37     private final TaskExecutorWithFakeMainThread mTaskExecutor;
     38 
     39     public JunitTaskExecutorRule(int ioThreadCount, boolean spyOnExecutor) {
     40         if (spyOnExecutor) {
     41             mTaskExecutor = Mockito.spy(new TaskExecutorWithFakeMainThread(ioThreadCount));
     42         } else {
     43             mTaskExecutor = new TaskExecutorWithFakeMainThread(ioThreadCount);
     44         }
     45 
     46     }
     47 
     48     private void beforeStart() {
     49         ArchTaskExecutor.getInstance().setDelegate(mTaskExecutor);
     50     }
     51 
     52     private void afterFinished() {
     53         ArchTaskExecutor.getInstance().setDelegate(null);
     54     }
     55 
     56     public TaskExecutor getTaskExecutor() {
     57         return mTaskExecutor;
     58     }
     59 
     60     /**
     61      * Awaits while all currently posted tasks will be finished
     62      *
     63      * @param seconds timeout in seconds
     64      */
     65     public void drainTasks(int seconds) throws InterruptedException {
     66         mTaskExecutor.drainTasks(seconds);
     67     }
     68 
     69     @Override
     70     public Statement apply(final Statement base, Description description) {
     71         return new Statement() {
     72             @Override
     73             public void evaluate() throws Throwable {
     74                 beforeStart();
     75                 try {
     76                     base.evaluate();
     77                     finishExecutors();
     78                 } catch (Throwable t) {
     79                     throw new RuntimeException(t);
     80                 } finally {
     81                     afterFinished();
     82                 }
     83             }
     84         };
     85     }
     86 
     87     private void finishExecutors() throws InterruptedException, MultipleFailureException {
     88         mTaskExecutor.shutdown(10);
     89         final List<Throwable> errors = mTaskExecutor.getErrors();
     90         if (!errors.isEmpty()) {
     91             throw new MultipleFailureException(errors);
     92         }
     93     }
     94 }
     95