# 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)

*** ** * ** ***


```
class CountingTaskExecutorRule : TestWatcher
```

<br />

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.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/kotlin/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/kotlin/androidx/arch/core/executor/testing/CountingTaskExecutorRule#CountingTaskExecutorRule()()` |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/arch/core/executor/testing/CountingTaskExecutorRule#drainTasks(int,java.util.concurrent.TimeUnit)(time: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, timeUnit: https://developer.android.com/reference/java/util/concurrent/TimeUnit.html)` Waits until all active tasks are finished. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/arch/core/executor/testing/CountingTaskExecutorRule#isIdle()()` Returns false if there are tasks waiting to be executed, true otherwise. |

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

| ### Inherited functions |
|---|
| 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-(base: https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html!, description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html#failed-java.lang.Throwable-org.junit.runner.Description-(e: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-throwable/index.html!, description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html#skipped-org.junit.AssumptionViolatedException-org.junit.runner.Description-(e: https://junit.org/junit4/javadoc/latest/org/junit/AssumptionViolatedException.html!, description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` | | `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://junit.org/junit4/javadoc/latest/org/junit/rules/TestWatcher.html#succeeded-org.junit.runner.Description-(description: https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html!)` | |

## Public constructors

### CountingTaskExecutorRule

Added in 2.0.0

```
CountingTaskExecutorRule()
```

## Public functions

### drainTasks

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

```
fun drainTasks(time: Int, timeUnit: TimeUnit): Unit
```

Waits until all active tasks are finished.

| Parameters |
|---|---|
| `time: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The duration to wait |
| `timeUnit: https://developer.android.com/reference/java/util/concurrent/TimeUnit.html` | 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)

```
fun isIdle(): Boolean
```

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | False if there are tasks waiting to be executed, true otherwise. |

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

## Protected functions

### finished

```
protected fun finished(description: Description!): Unit
```

### onIdle

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

```
protected fun onIdle(): Unit
```

Called when the number of awaiting tasks reaches to 0.

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

### starting

```
protected fun starting(description: Description!): Unit
```