1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.modules.utils;
18 
19 import androidx.test.filters.SmallTest;
20 import junit.framework.TestCase;
21 
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 import java.time.Duration;
28 import java.util.concurrent.TimeoutException;
29 
30 @RunWith(JUnit4.class)
31 @SmallTest
32 public class SynchronousResultReceiverTest extends TestCase {
33     private static final Duration OK_TIME = Duration.ofMillis(100);
34     private static final Duration NEG_TIME = Duration.ofSeconds(-1);
35 
36     @Test
testSimpleData()37     public void testSimpleData() throws Exception {
38         final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
39         recv.send(true);
40         final boolean result = recv.awaitResultNoInterrupt(OK_TIME).getValue(false);
41         assertTrue(result);
42     }
43 
44     @Test
testDoubleComplete()45     public void testDoubleComplete() throws Exception {
46         final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
47         recv.send(true);
48         Assert.assertThrows(IllegalStateException.class,
49                 () -> recv.send(true));
50     }
51 
52     @Test
testDefaultValue()53     public void testDefaultValue() throws Exception {
54         final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
55         recv.send(null);
56         assertTrue(recv.awaitResultNoInterrupt(OK_TIME).getValue(true));
57     }
58 
59     @Test
testPropagateException()60     public void testPropagateException() throws Exception {
61         final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
62         recv.propagateException(new RuntimeException("Placeholder exception"));
63         Assert.assertThrows(RuntimeException.class,
64                 () -> recv.awaitResultNoInterrupt(OK_TIME).getValue(false));
65     }
66 
67     @Test
testTimeout()68     public void testTimeout() throws Exception {
69         final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
70         Assert.assertThrows(TimeoutException.class,
71                 () -> recv.awaitResultNoInterrupt(OK_TIME));
72     }
73 
74     @Test
testNegativeTime()75     public void testNegativeTime() throws Exception {
76         final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
77         recv.send(false);
78         Assert.assertThrows(TimeoutException.class,
79                 () -> recv.awaitResultNoInterrupt(NEG_TIME));
80     }
81 }
82