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.Semaphore;
23 import java.util.concurrent.TimeUnit;
24 
25 /**
26  * Provides common Mockito calls for core Java classes.
27  */
28 public final class JavaMockitoHelper {
29 
30     private static final String TAG = JavaMockitoHelper.class.getSimpleName();
31 
32     /**
33      * Waits for a latch to be counted down.
34      *
35      * @param timeoutMs how long to wait for
36      *
37      * @throws {@link IllegalStateException} if it times out.
38      */
await(@onNull CountDownLatch latch, long timeoutMs)39     public static void await(@NonNull CountDownLatch latch, long timeoutMs)
40             throws InterruptedException {
41         if (!latch.await(timeoutMs, TimeUnit.MILLISECONDS)) {
42             throw new IllegalStateException(latch + " not called in " + timeoutMs + " ms");
43         }
44     }
45 
46     /**
47      * Waits for a semaphore.
48      *
49      * @param timeoutMs how long to wait for
50      *
51      * @throws {@link IllegalStateException} if it times out.
52      */
await(@onNull Semaphore semaphore, long timeoutMs)53     public static void await(@NonNull Semaphore semaphore, long timeoutMs)
54             throws InterruptedException {
55         if (!semaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS)) {
56             throw new IllegalStateException(semaphore + " not released in " + timeoutMs + " ms");
57         }
58     }
59 
60     /**
61      * Silently waits for a latch to be counted down, without throwing any exception if it isn't.
62      *
63      * @param timeoutMs how long to wait for
64      *
65      * @return whether the latch was counted down.
66      */
silentAwait(@onNull CountDownLatch latch, long timeoutMs)67     public static boolean silentAwait(@NonNull CountDownLatch latch, long timeoutMs) {
68         boolean called;
69         try {
70             called = latch.await(timeoutMs, TimeUnit.MILLISECONDS);
71             if (!called) {
72                 Log.w(TAG, latch + " not called in " + timeoutMs + " ms");
73             }
74         } catch (InterruptedException e) {
75             Thread.currentThread().interrupt();
76             Log.w(TAG, latch + " interrupted", e);
77             return false;
78         }
79         return called;
80     }
81 
JavaMockitoHelper()82     private JavaMockitoHelper() {
83         throw new UnsupportedOperationException("contains only static methods");
84     }
85 }
86