1919import static com .google .common .base .Preconditions .checkNotNull ;
2020import static com .google .common .truth .Truth .assertThat ;
2121import static com .google .common .util .concurrent .MoreExecutors .directExecutor ;
22+ import static java .util .concurrent .TimeUnit .SECONDS ;
2223import static org .robolectric .Shadows .shadowOf ;
2324
2425import android .app .Application ;
@@ -154,17 +155,17 @@ public void tearDown() {
154155 @ Test
155156 public void testAsyncServerSecurityPolicy_failed_returnsFailureStatus () throws Exception {
156157 ListenableFuture <Status > status = makeCall ();
157- statusesToSet . take ( ).set (Status .ALREADY_EXISTS );
158+ awaitNext ( statusesToSet ).set (Status .ALREADY_EXISTS );
158159
159- assertThat (status . get ( ).getCode ()).isEqualTo (Status .Code .ALREADY_EXISTS );
160+ assertThat (awaitResult ( status ).getCode ()).isEqualTo (Status .Code .ALREADY_EXISTS );
160161 }
161162
162163 @ Test
163164 public void testAsyncServerSecurityPolicy_failedFuture_failsWithCodeInternal () throws Exception {
164165 ListenableFuture <Status > status = makeCall ();
165- statusesToSet . take ( ).setException (new IllegalStateException ("oops" ));
166+ awaitNext ( statusesToSet ).setException (new IllegalStateException ("oops" ));
166167
167- Status failureStatus = status . get ( );
168+ Status failureStatus = awaitResult ( status );
168169 assertThat (failureStatus .getCode ()).isEqualTo (Status .Code .INTERNAL );
169170 assertThat (failureStatus .getDescription ()).isEqualTo ("Authorization future failed" );
170171 }
@@ -173,18 +174,18 @@ public void testAsyncServerSecurityPolicy_failedFuture_failsWithCodeInternal() t
173174 public void testAsyncServerSecurityPolicy_failedFuture_subsequentCallHasOpaqueFailure ()
174175 throws Exception {
175176 ListenableFuture <Status > firstStatusFuture = makeCall ();
176- statusesToSet . take ( ).setException (new IOException ("ouch" ));
177+ awaitNext ( statusesToSet ).setException (new IOException ("ouch" ));
177178
178- Status firstStatus = firstStatusFuture . get ( );
179+ Status firstStatus = awaitResult ( firstStatusFuture );
179180 assertThat (firstStatus .getCode ()).isEqualTo (Status .Code .INTERNAL );
180181 assertThat (firstStatus .getDescription ()).isEqualTo ("Authorization future failed" );
181182
182183 // TransportAuthorizationState evicts failed futures so the second call triggers a fresh
183184 // authorization check. Both calls must surface an opaque transport-level failure.
184185 ListenableFuture <Status > secondStatusFuture = makeCall ();
185- statusesToSet . take ( ).setException (new IOException ("ouch" ));
186+ awaitNext ( statusesToSet ).setException (new IOException ("ouch" ));
186187
187- Status secondStatus = secondStatusFuture . get ( );
188+ Status secondStatus = awaitResult ( secondStatusFuture );
188189 assertThat (secondStatus .getCode ()).isEqualTo (Status .Code .INTERNAL );
189190 assertThat (secondStatus .getDescription ()).isEqualTo ("Authorization future failed" );
190191 }
@@ -193,19 +194,19 @@ public void testAsyncServerSecurityPolicy_failedFuture_subsequentCallHasOpaqueFa
193194 public void testAsyncServerSecurityPolicy_failedFuture_cancelledFutureIsOpaque ()
194195 throws Exception {
195196 ListenableFuture <Status > statusFuture = makeCall ();
196- statusesToSet . take ( ).cancel (false );
197+ awaitNext ( statusesToSet ).cancel (false );
197198
198- Status failureStatus = statusFuture . get ( );
199+ Status failureStatus = awaitResult ( statusFuture );
199200 assertThat (failureStatus .getCode ()).isEqualTo (Status .Code .INTERNAL );
200201 assertThat (failureStatus .getDescription ()).isEqualTo ("Authorization future failed" );
201202 }
202203
203204 @ Test
204205 public void testAsyncServerSecurityPolicy_allowed_returnsOkStatus () throws Exception {
205206 ListenableFuture <Status > status = makeCall ();
206- statusesToSet . take ( ).set (Status .OK );
207+ awaitNext ( statusesToSet ).set (Status .OK );
207208
208- assertThat (status . get ( ).getCode ()).isEqualTo (Status .Code .OK );
209+ assertThat (awaitResult ( status ).getCode ()).isEqualTo (Status .Code .OK );
209210 }
210211
211212 private ListenableFuture <Status > makeCall () {
@@ -220,6 +221,18 @@ private ListenableFuture<Status> makeCall() {
220221 directExecutor ());
221222 }
222223
224+ private static <T > T awaitNext (BlockingQueue <T > queue ) throws Exception {
225+ T item = queue .poll (10 , SECONDS );
226+ if (item == null ) {
227+ throw new TimeoutException ("Queue timed out waiting for item" );
228+ }
229+ return item ;
230+ }
231+
232+ private static <T > T awaitResult (Future <T > future ) throws Exception {
233+ return future .get (10 , SECONDS );
234+ }
235+
223236 private static MethodDescriptor <Empty , Empty > getMethodDescriptor () {
224237 MethodDescriptor .Marshaller <Empty > marshaller =
225238 ProtoLiteUtils .marshaller (Empty .getDefaultInstance ());
0 commit comments