public class

IdlingScheduledThreadPoolExecutor

extends java.util.concurrent.ScheduledThreadPoolExecutor

implements IdlingResource

 java.lang.Object

↳java.util.concurrent.AbstractExecutorService

↳java.util.concurrent.ThreadPoolExecutor

↳java.util.concurrent.ScheduledThreadPoolExecutor

↳androidx.test.espresso.idling.concurrent.IdlingScheduledThreadPoolExecutor

Gradle dependencies

compile group: 'androidx.test.espresso.idling', name: 'idling-concurrent', version: '3.5.0-alpha06'

  • groupId: androidx.test.espresso.idling
  • artifactId: idling-concurrent
  • version: 3.5.0-alpha06

Artifact androidx.test.espresso.idling:idling-concurrent:3.5.0-alpha06 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso.idling:idling-concurrent com.android.support.test.espresso.idling:idling-concurrent

Androidx class mapping:

androidx.test.espresso.idling.concurrent.IdlingScheduledThreadPoolExecutor android.support.test.espresso.idling.concurrent.IdlingScheduledThreadPoolExecutor

Overview

A java.util.concurrent.ScheduledThreadPoolExecutor that can be registered as an IdlingResource with Espresso.

Overrides parent methods to monitor threads starting, and finishing execution. Uses CountingIdlingResource to track number of active tasks.

This API is currently in beta.

Summary

Constructors
publicIdlingScheduledThreadPoolExecutor(java.lang.String resourceName, int corePoolSize, java.util.concurrent.ThreadFactory threadFactory)

Creates a new IdlingScheduledThreadPoolExecutor with the given initial parameters.

publicIdlingScheduledThreadPoolExecutor(java.lang.String resourceName, int corePoolSize, java.util.concurrent.ThreadFactory threadFactory, boolean debugCounting)

Creates a new IdlingScheduledThreadPoolExecutor with the given initial parameters.

Methods
protected voidafterExecute(java.lang.Runnable r, java.lang.Throwable t)

protected voidbeforeExecute(java.lang.Thread t, java.lang.Runnable r)

public java.lang.StringgetName()

public booleanisIdleNow()

public voidregisterIdleTransitionCallback(IdlingResource.ResourceCallback callback)

public java.util.concurrent.ScheduledFuture<java.lang.Object>schedule(java.util.concurrent.Callable<java.lang.Object> callable, long delay, java.util.concurrent.TimeUnit unit)

public java.util.concurrent.ScheduledFuture<java.lang.Object>schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)

protected voidterminated()

from java.util.concurrent.ScheduledThreadPoolExecutordecorateTask, decorateTask, execute, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, getRemoveOnCancelPolicy, scheduleAtFixedRate, scheduleWithFixedDelay, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, setRemoveOnCancelPolicy, shutdown, shutdownNow, submit, submit, submit
from java.util.concurrent.ThreadPoolExecutorallowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, toString
from java.util.concurrent.AbstractExecutorServiceinvokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor
from java.lang.Objectclone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructors

public IdlingScheduledThreadPoolExecutor(java.lang.String resourceName, int corePoolSize, java.util.concurrent.ThreadFactory threadFactory)

Creates a new IdlingScheduledThreadPoolExecutor with the given initial parameters.

Parameters:

resourceName: the name of the executor (used for logging and idempotency of registration).
corePoolSize: the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set.
threadFactory: the factory to use when the executor creates a new thread.

public IdlingScheduledThreadPoolExecutor(java.lang.String resourceName, int corePoolSize, java.util.concurrent.ThreadFactory threadFactory, boolean debugCounting)

Creates a new IdlingScheduledThreadPoolExecutor with the given initial parameters.

Parameters:

resourceName: the name of the executor (used for logging and idempotency of registration).
corePoolSize: the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set.
threadFactory: the factory to use when the executor creates a new thread.
debugCounting: if true increment & decrement calls will print trace information to logs.

Methods

public java.lang.String getName()

public boolean isIdleNow()

public void registerIdleTransitionCallback(IdlingResource.ResourceCallback callback)

public java.util.concurrent.ScheduledFuture<java.lang.Object> schedule(java.lang.Runnable command, long delay, java.util.concurrent.TimeUnit unit)

public java.util.concurrent.ScheduledFuture<java.lang.Object> schedule(java.util.concurrent.Callable<java.lang.Object> callable, long delay, java.util.concurrent.TimeUnit unit)

protected void beforeExecute(java.lang.Thread t, java.lang.Runnable r)

protected void afterExecute(java.lang.Runnable r, java.lang.Throwable t)

protected void terminated()

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.test.espresso.idling.concurrent;

import android.util.Log;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.IdlingResource;
import androidx.test.espresso.idling.CountingIdlingResource;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * A {@link ScheduledThreadPoolExecutor} that can be registered as an {@link IdlingResource} with
 * Espresso.
 *
 * <p>Overrides parent methods to monitor threads starting, and finishing execution. Uses {@link
 * CountingIdlingResource} to track number of active tasks.
 *
 * <p><b>This API is currently in beta.</b>
 */
public class IdlingScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor
    implements IdlingResource {

  private static final String LOG_TAG = "IdlingThreadPoolExec";

  private CountingIdlingResource countingIdlingResource;

  /**
   * Creates a new {@code IdlingScheduledThreadPoolExecutor} with the given initial parameters.
   *
   * @param resourceName the name of the executor (used for logging and idempotency of
   *     registration).
   * @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless
   *     allowCoreThreadTimeOut is set.
   * @param threadFactory the factory to use when the executor creates a new thread.
   */
  public IdlingScheduledThreadPoolExecutor(
      String resourceName, int corePoolSize, ThreadFactory threadFactory) {
    this(resourceName, corePoolSize, threadFactory, false);
  }

  /**
   * Creates a new {@code IdlingScheduledThreadPoolExecutor} with the given initial parameters.
   *
   * @param resourceName the name of the executor (used for logging and idempotency of
   *     registration).
   * @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless
   *     allowCoreThreadTimeOut is set.
   * @param threadFactory the factory to use when the executor creates a new thread.
   * @param debugCounting if true increment & decrement calls will print trace information to logs.
   */
  public IdlingScheduledThreadPoolExecutor(
      String resourceName, int corePoolSize, ThreadFactory threadFactory, boolean debugCounting) {
    super(corePoolSize, threadFactory);
    countingIdlingResource = new CountingIdlingResource(resourceName, debugCounting);
    Log.i(LOG_TAG, "Register idling resource for scheduled thread pool " + resourceName);
    IdlingRegistry.getInstance().register(this);
  }

  @Override
  public String getName() {
    return countingIdlingResource.getName();
  }

  @Override
  public boolean isIdleNow() {
    return countingIdlingResource.isIdleNow();
  }

  @Override
  public void registerIdleTransitionCallback(ResourceCallback callback) {
    countingIdlingResource.registerIdleTransitionCallback(callback);
  }

  @Override
  public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
    countingIdlingResource.increment();
    return super.schedule(command, delay, unit);
  }

  @Override
  public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
    countingIdlingResource.increment();
    return super.schedule(callable, delay, unit);
  }

  @Override
  protected void beforeExecute(Thread t, Runnable r) {}

  @Override
  protected void afterExecute(Runnable r, Throwable t) {
    countingIdlingResource.decrement();
  }

  @Override
  protected void terminated() {
    super.terminated();
    Log.i(LOG_TAG, "Thread pool terminated, unregistering " + countingIdlingResource.getName());
    IdlingRegistry.getInstance().unregister(this);
  }
}