• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.documentsui.testing;
2 
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertTrue;
5 
6 import java.util.concurrent.CountDownLatch;
7 import java.util.concurrent.TimeUnit;
8 import java.util.function.Consumer;
9 
10 /**
11  * Helper class for testing async processes.
12  */
13 public class LatchedConsumer<T> implements Consumer<T> {
14 
15     private T value;
16     private CountDownLatch latch;
17 
LatchedConsumer(int expectedCount)18     public LatchedConsumer(int expectedCount) {
19         latch = new CountDownLatch(expectedCount);
20     }
21 
getLatch()22     public CountDownLatch getLatch() { return latch; }
getValue()23     public T getValue() { return value; }
24 
25 
26     @Override
accept(T value)27     public void accept(T value) {
28         this.value = value;
29         latch.countDown();
30     }
31 
assertNotCalled(long timeout, TimeUnit unit)32     public void assertNotCalled(long timeout, TimeUnit unit)
33             throws InterruptedException {
34         assertFalse(latch.await(timeout, unit));
35     }
36 
assertCalled(long timeout, TimeUnit unit)37     public void assertCalled(long timeout, TimeUnit unit)
38             throws InterruptedException {
39         assertTrue(latch.await(timeout, unit));
40     }
41 }
42