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.http.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.junit.Assert;
35  import org.junit.Test;
36  
37  public class TestBasicFuture {
38  
39      @Test
40      public void testCompleted() throws Exception {
41          final BasicFutureCallback<Object> callback = new BasicFutureCallback<Object>();
42          final BasicFuture<Object> future = new BasicFuture<Object>(callback);
43  
44          Assert.assertFalse(future.isDone());
45  
46          final Object result = new Object();
47          final Exception boom = new Exception();
48          future.completed(result);
49          future.failed(boom);
50          Assert.assertTrue(callback.isCompleted());
51          Assert.assertSame(result, callback.getResult());
52          Assert.assertFalse(callback.isFailed());
53          Assert.assertNull(callback.getException());
54          Assert.assertFalse(callback.isCancelled());
55  
56          Assert.assertSame(result, future.get());
57          Assert.assertTrue(future.isDone());
58          Assert.assertFalse(future.isCancelled());
59  
60      }
61  
62      @Test
63      public void testCompletedWithTimeout() throws Exception {
64          final BasicFutureCallback<Object> callback = new BasicFutureCallback<Object>();
65          final BasicFuture<Object> future = new BasicFuture<Object>(callback);
66  
67          Assert.assertFalse(future.isDone());
68  
69          final Object result = new Object();
70          final Exception boom = new Exception();
71          future.completed(result);
72          future.failed(boom);
73          Assert.assertTrue(callback.isCompleted());
74          Assert.assertSame(result, callback.getResult());
75          Assert.assertFalse(callback.isFailed());
76          Assert.assertNull(callback.getException());
77          Assert.assertFalse(callback.isCancelled());
78  
79          Assert.assertSame(result, future.get(1, TimeUnit.MILLISECONDS));
80          Assert.assertTrue(future.isDone());
81          Assert.assertFalse(future.isCancelled());
82      }
83  
84      @Test
85      public void testFailed() throws Exception {
86          final BasicFutureCallback<Object> callback = new BasicFutureCallback<Object>();
87          final BasicFuture<Object> future = new BasicFuture<Object>(callback);
88          final Object result = new Object();
89          final Exception boom = new Exception();
90          future.failed(boom);
91          future.completed(result);
92          Assert.assertFalse(callback.isCompleted());
93          Assert.assertNull(callback.getResult());
94          Assert.assertTrue(callback.isFailed());
95          Assert.assertSame(boom, callback.getException());
96          Assert.assertFalse(callback.isCancelled());
97  
98          try {
99              future.get();
100         } catch (final ExecutionException ex) {
101             Assert.assertSame(boom, ex.getCause());
102         }
103         Assert.assertTrue(future.isDone());
104         Assert.assertFalse(future.isCancelled());
105     }
106 
107     @Test
108     public void testCancelled() throws Exception {
109         final BasicFutureCallback<Object> callback = new BasicFutureCallback<Object>();
110         final BasicFuture<Object> future = new BasicFuture<Object>(callback);
111         final Object result = new Object();
112         final Exception boom = new Exception();
113         future.cancel(true);
114         future.failed(boom);
115         future.completed(result);
116         Assert.assertFalse(callback.isCompleted());
117         Assert.assertNull(callback.getResult());
118         Assert.assertFalse(callback.isFailed());
119         Assert.assertNull(callback.getException());
120         Assert.assertTrue(callback.isCancelled());
121 
122         try {
123             future.get();
124             Assert.fail("CancellationException expected");
125         } catch (final CancellationException ex) {
126         }
127         Assert.assertTrue(future.isDone());
128         Assert.assertTrue(future.isCancelled());
129     }
130 
131     @Test
132     public void testAsyncCompleted() throws Exception {
133         final BasicFuture<Object> future = new BasicFuture<Object>(null);
134         final Object result = new Object();
135 
136         final Thread t = new Thread() {
137 
138             @Override
139             public void run() {
140                 try {
141                     Thread.sleep(100);
142                     future.completed(result);
143                 } catch (final InterruptedException boom) {
144                 }
145             }
146 
147         };
148         t.setDaemon(true);
149         t.start();
150         Assert.assertSame(result, future.get(60, TimeUnit.SECONDS));
151         Assert.assertTrue(future.isDone());
152         Assert.assertFalse(future.isCancelled());
153     }
154 
155     @Test
156     public void testAsyncFailed() throws Exception {
157         final BasicFuture<Object> future = new BasicFuture<Object>(null);
158         final Exception boom = new Exception();
159 
160         final Thread t = new Thread() {
161 
162             @Override
163             public void run() {
164                 try {
165                     Thread.sleep(100);
166                     future.failed(boom);
167                 } catch (final InterruptedException ex) {
168                 }
169             }
170 
171         };
172         t.setDaemon(true);
173         t.start();
174         try {
175             future.get(60, TimeUnit.SECONDS);
176         } catch (final ExecutionException ex) {
177             Assert.assertSame(boom, ex.getCause());
178         }
179         Assert.assertTrue(future.isDone());
180         Assert.assertFalse(future.isCancelled());
181     }
182 
183     @Test(expected = CancellationException.class)
184     public void testAsyncCancelled() throws Exception {
185         final BasicFuture<Object> future = new BasicFuture<Object>(null);
186 
187         final Thread t = new Thread() {
188 
189             @Override
190             public void run() {
191                 try {
192                     Thread.sleep(100);
193                     future.cancel(true);
194                 } catch (final InterruptedException ex) {
195                 }
196             }
197 
198         };
199         t.setDaemon(true);
200         t.start();
201         future.get(60, TimeUnit.SECONDS);
202     }
203 
204     @Test(expected=TimeoutException.class)
205     public void testAsyncTimeout() throws Exception {
206         final BasicFuture<Object> future = new BasicFuture<Object>(null);
207         final Object result = new Object();
208 
209         final Thread t = new Thread() {
210 
211             @Override
212             public void run() {
213                 try {
214                     Thread.sleep(200);
215                     future.completed(result);
216                 } catch (final InterruptedException ex) {
217                 }
218             }
219 
220         };
221         t.setDaemon(true);
222         t.start();
223         future.get(1, TimeUnit.MILLISECONDS);
224     }
225 
226     @Test(expected=TimeoutException.class)
227     public void testAsyncNegativeTimeout() throws Exception {
228         final BasicFuture<Object> future = new BasicFuture<Object>(null);
229         future.get(-1, TimeUnit.MILLISECONDS);
230     }
231 
232 }