Skip to content

Commit 552132b

Browse files
authored
ConfigCacheClientTest.java: fix flaky test due to race condition (#6306)
1 parent cb132ab commit 552132b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

firebase-config/src/test/java/com/google/firebase/remoteconfig/internal/ConfigCacheClientTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.mockito.ArgumentCaptor;
4343
import org.mockito.Mock;
4444
import org.mockito.MockitoAnnotations;
45+
import org.mockito.stubbing.Answer;
4546
import org.robolectric.RobolectricTestRunner;
4647
import org.robolectric.annotation.Config;
4748

@@ -220,7 +221,7 @@ public void getBlocking_hasCachedValue_returnsCache() throws Exception {
220221

221222
@Test
222223
public void getBlocking_hasNoCachedValueAndFileReadTimesOut_returnsNull() throws Exception {
223-
when(mockStorageClient.read()).thenReturn(configContainer);
224+
when(mockStorageClient.read()).thenAnswer(BLOCK_INDEFINITELY);
224225

225226
ConfigContainer container = cacheClient.getBlocking(/* diskReadTimeoutInSeconds= */ 0L);
226227

@@ -329,4 +330,18 @@ public void cleanUp() {
329330
cacheThreadPool.shutdownNow();
330331
testingThreadPool.shutdownNow();
331332
}
333+
334+
/**
335+
* A Mockito "answer" that blocks indefinitely. The only way that {@link Answer#answer} will
336+
* return is if its thread is interrupted. This may be useful to cause a method to never return,
337+
* which should result in a timeout waiting for the operation to complete.
338+
* <p>
339+
* Example:
340+
* {@code when(foo.get()).thenAnswer(BLOCK_INDEFINITELY); }
341+
*/
342+
private static final Answer<ConfigContainer> BLOCK_INDEFINITELY =
343+
invocation -> {
344+
Thread.sleep(Long.MAX_VALUE);
345+
throw new RuntimeException("BLOCK_INDEFINITELY.answer() should never get here");
346+
};
332347
}

0 commit comments

Comments
 (0)