# CountingTaskExecutorRule

added in[version 1.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact android.arch.core:core-testing:1.1.1  
Summary:[Ctors](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule#pubctors)\|[Methods](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule#inhmethods)  

# CountingTaskExecutorRule

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.arch.core.executor.testing.CountingTaskExecutorRule](https://developer.android.com/reference/androidx/arch/core/executor/testing/CountingTaskExecutorRule)instead.

`
public


class
CountingTaskExecutorRule
`  
`


``

extends TestWatcher


``


`

|---|---|-------------------------------------------------------------|
| java.lang.Object                                                  |||
| ↳ | org.junit.rules.TestWatcher                                    ||
|   | ↳ | android.arch.core.executor.testing.CountingTaskExecutorRule |

<br />

*** ** * ** ***

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                                                                        ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[CountingTaskExecutorRule](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule#CountingTaskExecutorRule())`() ` |

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

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

| ### Inherited methods |
|-----------------------|---|
| From class` org.junit.rules.TestWatcher ` |--------------|-----------------------------------------------------------------| | ` Statement` | ` apply(Statement arg0, Description arg1) `                     | | ` void`      | ` failed(Throwable arg0, Description arg1) `                    | | ` void`      | ` finished(Description arg0) `                                  | | ` void`      | ` skipped(AssumptionViolatedException arg0, Description arg1) ` | | ` void`      | ` skipped(AssumptionViolatedException arg0, Description arg1) ` | | ` void`      | ` starting(Description arg0) `                                  | | ` void`      | ` succeeded(Description arg0) `                                 | ||
| From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||
| From interface` org.junit.rules.TestRule ` |-----------------------|---------------------------------------------| | ` abstract Statement` | ` apply(Statement arg0, Description arg1) ` | ||

## Public constructors

### CountingTaskExecutorRule

added in[version 1.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
CountingTaskExecutorRule ()
```

<br />

<br />

## Public methods

### drainTasks

added in[version 1.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void drainTasks (int time, 
                TimeUnit timeUnit)
```

Waits until all active tasks are finished.

<br />

|                             Parameters                              ||
|------------|---------------------------------------------------------|
| `time`     | `int`: The duration to wait <br />                      |
| `timeUnit` | `TimeUnit`: The time unit for the`time`parameter <br /> |

|                                Throws                                ||
|------------------------|----------------------------------------------|
| `InterruptedException` | If thread is interrupted while waiting       |
| `TimeoutException`     | If tasks cannot be drained at the given time |

### isIdle

added in[version 1.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
boolean isIdle ()
```

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

<br />

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

**See also:**

- [onIdle()](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule#onIdle())

## Protected methods

### finished

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

<br />

<br />

|             Parameters              ||
|---------------|----------------------|
| `description` | `Description` <br /> |

### onIdle

added in[version 1.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void onIdle ()
```

Called when the number of awaiting tasks reaches to 0.

<br />

**See also:**

- [isIdle()](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule#isIdle())  

### starting

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

<br />

<br />

|             Parameters              ||
|---------------|----------------------|
| `description` | `Description` <br /> |

-

  ## Classes

  - [CountingTaskExecutorRule](https://developer.android.com/reference/android/arch/core/executor/testing/CountingTaskExecutorRule)
  - [InstantTaskExecutorRule](https://developer.android.com/reference/android/arch/core/executor/testing/InstantTaskExecutorRule)