View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.concurrent.internal;
20  
21  import static org.testng.Assert.assertEquals;
22  
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.io.UnsupportedEncodingException;
26  import java.util.Set;
27  import java.util.concurrent.Callable;
28  import java.util.concurrent.ConcurrentHashMap;
29  import java.util.concurrent.ExecutorService;
30  import java.util.concurrent.Executors;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.jclouds.concurrent.Futures;
34  import org.jclouds.concurrent.Timeout;
35  import org.jclouds.internal.ClassMethodArgs;
36  import org.testng.annotations.BeforeTest;
37  import org.testng.annotations.Test;
38  
39  import com.google.common.collect.ImmutableMap;
40  import com.google.common.collect.ImmutableSet;
41  import com.google.common.util.concurrent.ListenableFuture;
42  import com.google.inject.Provides;
43  
44  /**
45   * Tests behavior of ListenableFutureExceptionParser
46   * 
47   * @author Adrian Cole
48   */
49  @Test(groups = "unit", singleThreaded = true)
50  public class SyncProxyTest {
51  
52     @Test
53     void testConvertNanos() {
54        assertEquals(SyncProxy.convertToNanos(Sync.class.getAnnotation(Timeout.class)), 40000000);
55     }
56  
57     @Timeout(duration = 40, timeUnit = TimeUnit.MILLISECONDS)
58     private static interface Sync {
59        String getString();
60  
61        String newString();
62  
63        @Provides
64        Set<String> string();
65  
66        String getRuntimeException();
67  
68        String getTypedException() throws FileNotFoundException;
69  
70        String take20Milliseconds();
71  
72        String take200MillisecondsAndTimeout();
73  
74        @Timeout(duration = 300, timeUnit = TimeUnit.MILLISECONDS)
75        String take200MillisecondsAndOverride();
76  
77     }
78  
79     static ExecutorService executorService = Executors.newCachedThreadPool();
80  
81     public static class Async {
82        public String toString() {
83           return "async";
84        }
85  
86        public ListenableFuture<String> getString() {
87           return Futures.makeListenable(executorService.submit(new Callable<String>() {
88  
89              public String call() throws Exception {
90                 return "foo";
91              }
92  
93           }), executorService);
94        }
95  
96        public ListenableFuture<String> getRuntimeException() {
97           return Futures.makeListenable(executorService.submit(new Callable<String>() {
98  
99              public String call() throws Exception {
100                throw new RuntimeException();
101             }
102 
103          }), executorService);
104       }
105 
106       public ListenableFuture<String> getTypedException() throws FileNotFoundException {
107          return Futures.makeListenable(executorService.submit(new Callable<String>() {
108 
109             public String call() throws FileNotFoundException {
110                throw new FileNotFoundException();
111             }
112 
113          }), executorService);
114       }
115 
116       public String newString() {
117          return "new";
118       }
119 
120       @Provides
121       public Set<String> string() {
122          return ImmutableSet.of("new");
123       }
124       
125       public ListenableFuture<String> take20Milliseconds() {
126          return Futures.makeListenable(executorService.submit(new Callable<String>() {
127 
128             public String call() {
129                try {
130                   Thread.sleep(20);
131                } catch (InterruptedException e) {
132                   e.printStackTrace();
133                }
134                return "foo";
135             }
136 
137          }), executorService);
138       }
139 
140       public ListenableFuture<String> take200MillisecondsAndTimeout() {
141          return Futures.makeListenable(executorService.submit(new Callable<String>() {
142 
143             public String call() {
144                try {
145                   Thread.sleep(200);
146                } catch (InterruptedException e) {
147                   e.printStackTrace();
148                }
149                return "foo";
150             }
151 
152          }), executorService);
153       }
154 
155       public ListenableFuture<String> take200MillisecondsAndOverride() {
156          return take200MillisecondsAndTimeout();
157       }
158 
159    }
160 
161    private Sync sync;
162 
163    @BeforeTest
164    public void setUp() throws IllegalArgumentException, SecurityException, NoSuchMethodException {
165       sync = SyncProxy.proxy(Sync.class, new SyncProxy(Sync.class, new Async(),
166                new ConcurrentHashMap<ClassMethodArgs, Object>(), ImmutableMap.<Class<?>, Class<?>> of()));
167       // just to warm up
168       sync.string();
169    }
170 
171    @Test
172    public void testUnwrapListenableFuture() {
173       assertEquals(sync.getString(), "foo");
174    }
175 
176    @Test
177    public void testPassSync() {
178       assertEquals(sync.newString(), "new");
179       assertEquals(sync.string(), ImmutableSet.of("new"));
180    }
181 
182    @Test
183    public void testTake20Milliseconds() {
184       assertEquals(sync.take20Milliseconds(), "foo");
185 
186    }
187 
188    @Test(expectedExceptions = RuntimeException.class)
189    public void testTake200MillisecondsAndTimeout() {
190       assertEquals(sync.take200MillisecondsAndTimeout(), "foo");
191    }
192 
193    @Test
194    public void testTake200MillisecondsAndOverride() {
195       assertEquals(sync.take200MillisecondsAndOverride(), "foo");
196    }
197 
198    @Test
199    public void testToString() {
200       assertEquals(sync.toString(), "Sync Proxy for: Async");
201    }
202 
203    @Test(expectedExceptions = RuntimeException.class)
204    public void testUnwrapRuntimeException() {
205       sync.getRuntimeException();
206    }
207 
208    @Test(expectedExceptions = FileNotFoundException.class)
209    public void testUnwrapTypedException() throws FileNotFoundException {
210       sync.getTypedException();
211    }
212 
213    @Timeout(duration = 30, timeUnit = TimeUnit.SECONDS)
214    private static interface SyncWrongException {
215       String getString();
216 
217       String newString();
218 
219       String getRuntimeException();
220 
221       String getTypedException() throws UnsupportedEncodingException;
222 
223    }
224 
225    @Test(expectedExceptions = IllegalArgumentException.class)
226    public void testWrongTypedException() throws IllegalArgumentException, SecurityException, NoSuchMethodException,
227             IOException {
228       SyncProxy.proxy(SyncWrongException.class, new SyncProxy(SyncWrongException.class, new Async(),
229                new ConcurrentHashMap<ClassMethodArgs, Object>(), ImmutableMap.<Class<?>, Class<?>> of()));
230    }
231 
232    private static interface SyncNoTimeOut {
233       String getString();
234 
235       String newString();
236 
237       String getRuntimeException();
238 
239       String getTypedException() throws UnsupportedEncodingException;
240 
241    }
242 
243    @Test(expectedExceptions = IllegalArgumentException.class)
244    public void testNoTimeOutException() throws IllegalArgumentException, SecurityException, NoSuchMethodException,
245             IOException {
246       SyncProxy.proxy(SyncNoTimeOut.class, new SyncProxy(SyncNoTimeOut.class, new Async(),
247                new ConcurrentHashMap<ClassMethodArgs, Object>(), ImmutableMap.<Class<?>, Class<?>> of()));
248    }
249 
250 }