1 /* 2 * Copyright (C) 2018 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 com.google.android.setupcompat.internal; 18 19 import androidx.annotation.Nullable; 20 import androidx.annotation.VisibleForTesting; 21 import java.util.concurrent.ArrayBlockingQueue; 22 import java.util.concurrent.Executor; 23 import java.util.concurrent.ExecutorService; 24 import java.util.concurrent.ThreadPoolExecutor; 25 import java.util.concurrent.TimeUnit; 26 27 /** 28 * Utility class to provide executors. 29 * 30 * <p>It allows the executors to be mocked in Robolectric, redirecting to Robolectric's schedulers 31 * rather than using real threads. 32 */ 33 public final class ExecutorProvider<T extends Executor> { 34 35 private static final int SETUP_METRICS_LOGGER_MAX_QUEUED = 50; 36 /** 37 * Creates a single threaded {@link ExecutorService} with a maximum pool size {@code maxSize}. 38 * Jobs submitted when the pool is full causes {@link 39 * java.util.concurrent.RejectedExecutionException} to be thrown. 40 */ 41 public static final ExecutorProvider<ExecutorService> setupCompatServiceInvoker = 42 new ExecutorProvider<>( 43 createSizeBoundedExecutor("SetupCompatServiceInvoker", SETUP_METRICS_LOGGER_MAX_QUEUED)); 44 45 private final T executor; 46 47 @Nullable private T injectedExecutor; 48 ExecutorProvider(T executor)49 private ExecutorProvider(T executor) { 50 this.executor = executor; 51 } 52 get()53 public T get() { 54 if (injectedExecutor != null) { 55 return injectedExecutor; 56 } 57 return executor; 58 } 59 60 /** 61 * Injects an executor for testing use for this provider. Subsequent calls to {@link #get} will 62 * return this instance instead, until {@link #resetExecutors()} is called. 63 */ 64 @VisibleForTesting injectExecutor(T executor)65 public void injectExecutor(T executor) { 66 this.injectedExecutor = executor; 67 } 68 69 @VisibleForTesting resetExecutors()70 public static void resetExecutors() { 71 setupCompatServiceInvoker.injectedExecutor = null; 72 } 73 74 @VisibleForTesting createSizeBoundedExecutor(String threadName, int maxSize)75 public static ExecutorService createSizeBoundedExecutor(String threadName, int maxSize) { 76 return new ThreadPoolExecutor( 77 /* corePoolSize= */ 1, 78 /* maximumPoolSize= */ 1, 79 /* keepAliveTime= */ 0, 80 TimeUnit.SECONDS, 81 new ArrayBlockingQueue<>(maxSize), 82 runnable -> new Thread(runnable, threadName)); 83 } 84 } 85