1 /* 2 * Copyright (C) 2020 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 package android.car.test.mocks; 17 18 import android.annotation.NonNull; 19 import android.util.Log; 20 21 import java.util.concurrent.CountDownLatch; 22 import java.util.concurrent.ExecutionException; 23 import java.util.concurrent.Future; 24 import java.util.concurrent.Semaphore; 25 import java.util.concurrent.TimeUnit; 26 import java.util.concurrent.TimeoutException; 27 28 /** 29 * Provides common Mockito calls for core Java classes. 30 */ 31 public final class JavaMockitoHelper { 32 33 static final long ASYNC_TIMEOUT_MS = 500; 34 35 private static final String TAG = JavaMockitoHelper.class.getSimpleName(); 36 37 /** 38 * Waits for a latch to be counted down. 39 * 40 * @param timeoutMs how long to wait for 41 * 42 * @throws {@link IllegalStateException} if it times out. 43 */ await(@onNull CountDownLatch latch, long timeoutMs)44 public static void await(@NonNull CountDownLatch latch, long timeoutMs) 45 throws InterruptedException { 46 if (!latch.await(timeoutMs, TimeUnit.MILLISECONDS)) { 47 throw new IllegalStateException(latch + " not called in " + timeoutMs + " ms"); 48 } 49 } 50 51 /** 52 * Waits for a semaphore. 53 * 54 * @param timeoutMs how long to wait for 55 * 56 * @throws {@link IllegalStateException} if it times out. 57 */ await(@onNull Semaphore semaphore, long timeoutMs)58 public static void await(@NonNull Semaphore semaphore, long timeoutMs) 59 throws InterruptedException { 60 if (!semaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) { 61 throw new IllegalStateException(semaphore + " not released in " + timeoutMs + " ms"); 62 } 63 } 64 65 /** 66 * Silently waits for a latch to be counted down, without throwing any exception if it isn't. 67 * 68 * @param timeoutMs how long to wait for 69 * 70 * @return whether the latch was counted down. 71 */ silentAwait(@onNull CountDownLatch latch, long timeoutMs)72 public static boolean silentAwait(@NonNull CountDownLatch latch, long timeoutMs) { 73 boolean called; 74 try { 75 called = latch.await(timeoutMs, TimeUnit.MILLISECONDS); 76 if (!called) { 77 Log.w(TAG, latch + " not called in " + timeoutMs + " ms"); 78 } 79 } catch (InterruptedException e) { 80 Thread.currentThread().interrupt(); 81 Log.w(TAG, latch + " interrupted", e); 82 return false; 83 } 84 return called; 85 } 86 87 /** 88 * Gets the result of a future, or throw a {@link IllegalStateException} if it times out after 89 * {@value #ASYNC_TIMEOUT_MS} ms. 90 */ 91 @NonNull getResult(@onNull Future<T> future)92 public static <T> T getResult(@NonNull Future<T> future) { 93 return getResult(future, ASYNC_TIMEOUT_MS); 94 } 95 96 /** 97 * Gets the result of a future, or throw a {@link IllegalStateException} if it times out. 98 */ 99 @NonNull getResult(@onNull Future<T> future, long timeoutMs)100 public static <T> T getResult(@NonNull Future<T> future, long timeoutMs) { 101 try { 102 return future.get(timeoutMs, TimeUnit.MILLISECONDS); 103 } catch (InterruptedException e) { 104 Thread.currentThread().interrupt(); 105 throw new IllegalStateException("future interrupted", e); 106 } catch (TimeoutException e) { 107 throw new IllegalStateException("future not called in " + ASYNC_TIMEOUT_MS + "ms", e); 108 } catch (ExecutionException e) { 109 throw new IllegalStateException("failed to get future", e); 110 } 111 } 112 JavaMockitoHelper()113 private JavaMockitoHelper() { 114 throw new UnsupportedOperationException("contains only static methods"); 115 } 116 } 117