1 /*
2  * Copyright 2025 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 androidx.appfunctions.integration.tests
18 
19 import kotlinx.coroutines.CoroutineScope
20 import kotlinx.coroutines.delay
21 import kotlinx.coroutines.runBlocking
22 
23 internal object TestUtil {
doBlockingnull24     fun doBlocking(block: suspend CoroutineScope.() -> Unit) = runBlocking(block = block)
25 
26     fun interface ThrowRunnable {
27         @Throws(Throwable::class) suspend fun run()
28     }
29 
30     /** Retries an assertion with a delay between attempts. */
31     @Throws(Throwable::class)
retryAssertnull32     suspend fun retryAssert(runnable: ThrowRunnable) {
33         var lastError: Throwable? = null
34 
35         for (attempt in 0 until RETRY_MAX_INTERVALS) {
36             try {
37                 runnable.run()
38                 return
39             } catch (e: Throwable) {
40                 lastError = e
41                 delay(RETRY_CHECK_INTERVAL_MILLIS)
42             }
43         }
44         throw lastError!!
45     }
46 
47     private const val RETRY_CHECK_INTERVAL_MILLIS: Long = 500
48     private const val RETRY_MAX_INTERVALS: Long = 10
49 }
50