View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.core5.concurrent;
28  
29  import java.util.concurrent.CancellationException;
30  import java.util.concurrent.ExecutionException;
31  import java.util.concurrent.TimeUnit;
32  import java.util.concurrent.TimeoutException;
33  
34  import org.apache.hc.core5.util.TimeoutValueException;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class TestBasicFuture {
39  
40      @Test
41      public void testCompleted() throws Exception {
42          final BasicFutureCallback<Object> callback = new BasicFutureCallback<>();
43          final BasicFuture<Object> future = new BasicFuture<>(callback);
44  
45          Assert.assertFalse(future.isDone());
46  
47          final Object result = new Object();
48          final Exception boom = new Exception();
49          future.completed(result);
50          future.failed(boom);
51          Assert.assertTrue(callback.isCompleted());
52          Assert.assertSame(result, callback.getResult());
53          Assert.assertFalse(callback.isFailed());
54          Assert.assertNull(callback.getException());
55          Assert.assertFalse(callback.isCancelled());
56  
57          Assert.assertSame(result, future.get());
58          Assert.assertTrue(future.isDone());
59          Assert.assertFalse(future.isCancelled());
60  
61      }
62  
63      @Test
64      public void testCompletedWithTimeout() throws Exception {
65          final BasicFutureCallback<Object> callback = new BasicFutureCallback<>();
66          final BasicFuture<Object> future = new BasicFuture<>(callback);
67  
68          Assert.assertFalse(future.isDone());
69  
70          final Object result = new Object();
71          final Exception boom = new Exception();
72          future.completed(result);
73          future.failed(boom);
74          Assert.assertTrue(callback.isCompleted());
75          Assert.assertSame(result, callback.getResult());
76          Assert.assertFalse(callback.isFailed());
77          Assert.assertNull(callback.getException());
78          Assert.assertFalse(callback.isCancelled());
79  
80          Assert.assertSame(result, future.get(1, TimeUnit.MILLISECONDS));
81          Assert.assertTrue(future.isDone());
82          Assert.assertFalse(future.isCancelled());
83      }
84  
85      @Test
86      public void testFailed() throws Exception {
87          final BasicFutureCallback<Object> callback = new BasicFutureCallback<>();
88          final BasicFuture<Object> future = new BasicFuture<>(callback);
89          final Object result = new Object();
90          final Exception boom = new Exception();
91          future.failed(boom);
92          future.completed(result);
93          Assert.assertFalse(callback.isCompleted());
94          Assert.assertNull(callback.getResult());
95          Assert.assertTrue(callback.isFailed());
96          Assert.assertSame(boom, callback.getException());
97          Assert.assertFalse(callback.isCancelled());
98  
99          try {
100             future.get();
101         } catch (final ExecutionException ex) {
102             Assert.assertSame(boom, ex.getCause());
103         }
104         Assert.assertTrue(future.isDone());
105         Assert.assertFalse(future.isCancelled());
106     }
107 
108     @Test
109     public void testCancelled() throws Exception {
110         final BasicFutureCallback<Object> callback = new BasicFutureCallback<>();
111         final BasicFuture<Object> future = new BasicFuture<>(callback);
112         final Object result = new Object();
113         final Exception boom = new Exception();
114         future.cancel(true);
115         future.failed(boom);
116         future.completed(result);
117         Assert.assertFalse(callback.isCompleted());
118         Assert.assertNull(callback.getResult());
119         Assert.assertFalse(callback.isFailed());
120         Assert.assertNull(callback.getException());
121         Assert.assertTrue(callback.isCancelled());
122 
123         try {
124             future.get();
125             Assert.fail("CancellationException expected");
126         } catch (final CancellationException ex) {
127         }
128         Assert.assertTrue(future.isDone());
129         Assert.assertTrue(future.isCancelled());
130     }
131 
132     @Test
133     public void testAsyncCompleted() throws Exception {
134         final BasicFuture<Object> future = new BasicFuture<>(null);
135         final Object result = new Object();
136 
137         final Thread t = new Thread() {
138 
139             @Override
140             public void run() {
141                 try {
142                     Thread.sleep(100);
143                     future.completed(result);
144                 } catch (final InterruptedException boom) {
145                 }
146             }
147 
148         };
149         t.setDaemon(true);
150         t.start();
151         Assert.assertSame(result, future.get(60, TimeUnit.SECONDS));
152         Assert.assertTrue(future.isDone());
153         Assert.assertFalse(future.isCancelled());
154     }
155 
156     @Test
157     public void testAsyncFailed() throws Exception {
158         final BasicFuture<Object> future = new BasicFuture<>(null);
159         final Exception boom = new Exception();
160 
161         final Thread t = new Thread() {
162 
163             @Override
164             public void run() {
165                 try {
166                     Thread.sleep(100);
167                     future.failed(boom);
168                 } catch (final InterruptedException ex) {
169                 }
170             }
171 
172         };
173         t.setDaemon(true);
174         t.start();
175         try {
176             future.get(60, TimeUnit.SECONDS);
177         } catch (final ExecutionException ex) {
178             Assert.assertSame(boom, ex.getCause());
179         }
180         Assert.assertTrue(future.isDone());
181         Assert.assertFalse(future.isCancelled());
182     }
183 
184     @Test(expected = CancellationException.class)
185     public void testAsyncCancelled() throws Exception {
186         final BasicFuture<Object> future = new BasicFuture<>(null);
187 
188         final Thread t = new Thread() {
189 
190             @Override
191             public void run() {
192                 try {
193                     Thread.sleep(100);
194                     future.cancel(true);
195                 } catch (final InterruptedException ex) {
196                 }
197             }
198 
199         };
200         t.setDaemon(true);
201         t.start();
202         future.get(60, TimeUnit.SECONDS);
203     }
204 
205     @Test(expected=TimeoutException.class)
206     public void testAsyncTimeout() throws Exception {
207         final BasicFuture<Object> future = new BasicFuture<>(null);
208         final Object result = new Object();
209 
210         final Thread t = new Thread() {
211 
212             @Override
213             public void run() {
214                 try {
215                     Thread.sleep(200);
216                     future.completed(result);
217                 } catch (final InterruptedException ex) {
218                 }
219             }
220 
221         };
222         t.setDaemon(true);
223         t.start();
224         future.get(1, TimeUnit.MILLISECONDS);
225     }
226 
227     @Test(expected=TimeoutValueException.class)
228     public void testAsyncNegativeTimeout() throws Exception {
229         final BasicFuture<Object> future = new BasicFuture<>(null);
230         future.get(-1, TimeUnit.MILLISECONDS);
231     }
232 
233 }