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
- import com .google .firebase .appdistribution .UpdateTask ;
33
31
import com .google .firebase .appdistribution .impl .FirebaseAppDistributionLifecycleNotifier .ActivityConsumer ;
34
32
import com .google .firebase .appdistribution .impl .FirebaseAppDistributionLifecycleNotifier .ActivityFunction ;
35
- import com .google .firebase .concurrent .FirebaseExecutors ;
36
- import com .google .firebase .concurrent .TestOnlyExecutors ;
37
33
import java .io .IOException ;
38
34
import java .io .InputStream ;
39
- import java .util .ArrayList ;
40
- import java .util .List ;
41
35
import java .util .concurrent .ExecutionException ;
42
36
import java .util .concurrent .ExecutorService ;
43
37
import java .util .concurrent .Executors ;
44
38
import java .util .concurrent .TimeUnit ;
39
+ import java .util .function .BooleanSupplier ;
45
40
import org .json .JSONException ;
46
41
import org .json .JSONObject ;
47
42
import org .mockito .stubbing .Answer ;
48
43
49
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
+
50
50
private TestUtils () {}
51
51
52
52
static void awaitTaskFailure (Task task , Status status , String messageSubstring ) {
@@ -89,7 +89,7 @@ static <T> T awaitTask(Task<T> task)
89
89
}
90
90
91
91
static void awaitTermination (ExecutorService executorService ) throws InterruptedException {
92
- executorService .awaitTermination (100 , TimeUnit .MILLISECONDS );
92
+ executorService .awaitTermination (AWAIT_TERMINATION_TIMEOUT_MS , TimeUnit .MILLISECONDS );
93
93
}
94
94
95
95
static void awaitAsyncOperations (ExecutorService executorService ) throws InterruptedException {
@@ -101,41 +101,19 @@ static void awaitAsyncOperations(ExecutorService executorService) throws Interru
101
101
shadowOf (getMainLooper ()).idle ();
102
102
}
103
103
104
- /** Await a specified number of progress events on an {@link UpdateTask}. */
105
- static List <UpdateProgress > awaitProgressEvents (UpdateTask updateTask , int count )
106
- throws InterruptedException {
107
- return awaitProgressEvents (updateTask , count , /* setupFunction= */ () -> {});
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" );
108
113
}
109
114
110
- /**
111
- * Await a specified number of progress events on an {@link UpdateTask}, executing a {@link
112
- * Runnable} after adding the listeners.
113
- *
114
- * @param updateTask the update task
115
- * @param count the number of progress events to await
116
- * @param setupFunction a function to run after adding the listeners to the update task, but
117
- * before we await the progress events. This is useful for kicking off delayed processes that
118
- * would otherwise start emitting progress events that need to be captured.
119
- */
120
- static List <UpdateProgress > awaitProgressEvents (
121
- UpdateTask updateTask , int count , Runnable setupFunction ) throws InterruptedException {
122
- List <UpdateProgress > progressEvents = new ArrayList <>();
123
- updateTask .addOnProgressListener (FirebaseExecutors .directExecutor (), progressEvents ::add );
124
- setupFunction .run ();
125
- ExecutorService executor = TestOnlyExecutors .blocking ();
126
- executor .execute (
127
- () -> {
128
- while (progressEvents .size () < count ) {
129
- try {
130
- Thread .sleep (50 );
131
- } catch (InterruptedException e ) {
132
- throw new RuntimeException ("Interrupted while waiting for progress events" , e );
133
- }
134
- }
135
- });
136
- executor .awaitTermination (500 , TimeUnit .MILLISECONDS );
137
- assertThat (progressEvents ).hasSize (count );
138
- return progressEvents ;
115
+ private static long elapsedTime (long start ) {
116
+ return System .currentTimeMillis () - start ;
139
117
}
140
118
141
119
/**
0 commit comments