# CountingTaskExecutorRule

Artifact: [androidx.arch.core:core-testing](https://developer.android.com/jetpack/androidx/releases/arch-core) [View Source](https://cs.android.com/search?q=file:androidx/arch/core/executor/testing/CountingTaskExecutorRule.java+class:androidx.arch.core.executor.testing.CountingTaskExecutorRule) Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/arch-core#2.0.0)

*** ** * ** ***


```
public class CountingTaskExecutorRule extends TestWatcher
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [org.junit.rules.TestWatcher](https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html) ||
|   | ↳ | [androidx.arch.core.executor.testing.CountingTaskExecutorRule](https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule) |

*** ** * ** ***

A JUnit Test Rule that swaps the background executor used by the Architecture Components with a different one which counts the tasks as they are start and finish.

You can use this rule for your host side tests that use Architecture Components.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#CountingTaskExecutorRule()()` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#drainTasks(int,java.util.concurrent.TimeUnit)(int time, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/TimeUnit.html timeUnit)` Waits until all active tasks are finished. |
| `boolean` | `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#isIdle()()` Returns false if there are tasks waiting to be executed, true otherwise. |

| ### Protected methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#finished(org.junit.runner.Description)(https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` |
| `void` | `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#onIdle()()` Called when the number of awaiting tasks reaches to 0. |
| `void` | `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#starting(org.junit.runner.Description)(https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` |

| ### Inherited methods |
|---|
| From [org.junit.rules.TestWatcher](https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html) |---|---| | `https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html#apply-org.junit.runners.model.Statement-org.junit.runner.Description-(https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html base, https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` | | `void` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html#failed-java.lang.Throwable-org.junit.runner.Description-(https://developer.android.com/reference/java/lang/Throwable.html e, https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` | | `void` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html#skipped-org.junit.AssumptionViolatedException-org.junit.runner.Description-(https://junit.org/junit4/javadoc/latest/org/junit/AssumptionViolatedException.html e, https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` | | `void` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html#succeeded-org.junit.runner.Description-(https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` | |

## Public constructors

### CountingTaskExecutorRule

Added in 2.0.0

```
public CountingTaskExecutorRule()
```

## Public methods

### drainTasks

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/arch-core#2.0.0)

```
public void drainTasks(int time, @NonNull TimeUnit timeUnit)
```

Waits until all active tasks are finished.

| Parameters |
|---|---|
| `int time` | The duration to wait |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/TimeUnit.html timeUnit` | The time unit for the `time` parameter |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/InterruptedException.html` | If thread is interrupted while waiting |
| `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` | If tasks cannot be drained at the given time |

### isIdle

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/arch-core#2.0.0)

```
public boolean isIdle()
```

Returns false if there are tasks waiting to be executed, true otherwise.

| Returns |
|---|---|
| `boolean` | False if there are tasks waiting to be executed, true otherwise. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#onIdle()` |   |

## Protected methods

### finished

```
protected void finished(Description description)
```

### onIdle

Added in [2.0.0](https://developer.android.com/jetpack/androidx/releases/arch-core#2.0.0)

```
protected void onIdle()
```

Called when the number of awaiting tasks reaches to 0.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule#isIdle()` |   |

### starting

```
protected void starting(Description description)
```