28
28
import com .google .android .gms .tasks .Tasks ;
29
29
import com .google .firebase .appdistribution .FirebaseAppDistributionException ;
30
30
import com .google .firebase .appdistribution .FirebaseAppDistributionException .Status ;
31
- import com .google .firebase .appdistribution .UpdateProgress ;
32
31
import com .google .firebase .appdistribution .impl .FirebaseAppDistributionLifecycleNotifier .ActivityConsumer ;
33
32
import com .google .firebase .appdistribution .impl .FirebaseAppDistributionLifecycleNotifier .ActivityFunction ;
34
- import com .google .firebase .concurrent .TestOnlyExecutors ;
35
33
import java .io .IOException ;
36
34
import java .io .InputStream ;
37
- import java .util .Collection ;
38
35
import java .util .concurrent .ExecutionException ;
39
36
import java .util .concurrent .ExecutorService ;
40
37
import java .util .concurrent .Executors ;
41
38
import java .util .concurrent .TimeUnit ;
39
+ import java .util .function .BooleanSupplier ;
42
40
import org .json .JSONException ;
43
41
import org .json .JSONObject ;
44
42
import org .mockito .stubbing .Answer ;
45
43
46
44
final class TestUtils {
45
+
46
+ private static final int AWAIT_TERMINATION_TIMEOUT_MS = 100 ;
47
+ private static final int AWAIT_CONDITION_TIMEOUT_MS = 500 ;
48
+ private static final int SLEEP_MS = 50 ;
49
+
47
50
private TestUtils () {}
48
51
49
52
static void awaitTaskFailure (Task task , Status status , String messageSubstring ) {
@@ -86,7 +89,7 @@ static <T> T awaitTask(Task<T> task)
86
89
}
87
90
88
91
static void awaitTermination (ExecutorService executorService ) throws InterruptedException {
89
- executorService .awaitTermination (100 , TimeUnit .MILLISECONDS );
92
+ executorService .awaitTermination (AWAIT_TERMINATION_TIMEOUT_MS , TimeUnit .MILLISECONDS );
90
93
}
91
94
92
95
static void awaitAsyncOperations (ExecutorService executorService ) throws InterruptedException {
@@ -98,22 +101,19 @@ static void awaitAsyncOperations(ExecutorService executorService) throws Interru
98
101
shadowOf (getMainLooper ()).idle ();
99
102
}
100
103
101
- /** Await a specified number of progress events being added to the given collection. */
102
- static void awaitProgressEvents (Collection <UpdateProgress > progressEvents , int count )
103
- throws InterruptedException {
104
- ExecutorService executor = TestOnlyExecutors .blocking ();
105
- executor .execute (
106
- () -> {
107
- while (progressEvents .size () < count ) {
108
- try {
109
- Thread .sleep (50 );
110
- } catch (InterruptedException e ) {
111
- throw new RuntimeException ("Interrupted while waiting for progress events" , e );
112
- }
113
- }
114
- });
115
- executor .awaitTermination (500 , TimeUnit .MILLISECONDS );
116
- assertThat (progressEvents ).hasSize (count );
104
+ static void awaitCondition (BooleanSupplier condition ) throws InterruptedException {
105
+ long start = System .currentTimeMillis ();
106
+ while (elapsedTime (start ) < AWAIT_CONDITION_TIMEOUT_MS ) {
107
+ if (condition .getAsBoolean ()) {
108
+ return ;
109
+ }
110
+ Thread .sleep (SLEEP_MS );
111
+ }
112
+ throw new AssertionError ("Timed out waiting for condition" );
113
+ }
114
+
115
+ private static long elapsedTime (long start ) {
116
+ return System .currentTimeMillis () - start ;
117
117
}
118
118
119
119
/**
0 commit comments