• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Guava Authors
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.google.common.util.concurrent;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import junit.framework.TestCase;
25 
26 /**
27  * Unit test for {@link FakeTimeLimiter}.
28  *
29  * @author Jens Nyman
30  */
31 public class FakeTimeLimiterTest extends TestCase {
32 
33   private static final int DELAY_MS = 50;
34   private static final String RETURN_VALUE = "abc";
35 
36   private TimeLimiter timeLimiter;
37 
38   @Override
setUp()39   protected void setUp() throws Exception {
40     super.setUp();
41     timeLimiter = new FakeTimeLimiter();
42   }
43 
testCallWithTimeout_propagatesReturnValue()44   public void testCallWithTimeout_propagatesReturnValue() throws Exception {
45     String result =
46         timeLimiter.callWithTimeout(
47             Callables.returning(RETURN_VALUE), DELAY_MS, TimeUnit.MILLISECONDS);
48 
49     assertThat(result).isEqualTo(RETURN_VALUE);
50   }
51 
testCallWithTimeout_wrapsCheckedException()52   public void testCallWithTimeout_wrapsCheckedException() throws Exception {
53     Exception exception = new SampleCheckedException();
54     try {
55       timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
56       fail("Expected ExecutionException");
57     } catch (ExecutionException e) {
58       assertThat(e.getCause()).isEqualTo(exception);
59     }
60   }
61 
testCallWithTimeout_wrapsUncheckedException()62   public void testCallWithTimeout_wrapsUncheckedException() throws Exception {
63     Exception exception = new RuntimeException("test");
64     try {
65       timeLimiter.callWithTimeout(callableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
66       fail("Expected UncheckedExecutionException");
67     } catch (UncheckedExecutionException e) {
68       assertThat(e.getCause()).isEqualTo(exception);
69     }
70   }
71 
testCallUninterruptiblyWithTimeout_propagatesReturnValue()72   public void testCallUninterruptiblyWithTimeout_propagatesReturnValue() throws Exception {
73     String result =
74         timeLimiter.callUninterruptiblyWithTimeout(
75             Callables.returning(RETURN_VALUE), DELAY_MS, TimeUnit.MILLISECONDS);
76 
77     assertThat(result).isEqualTo(RETURN_VALUE);
78   }
79 
testRunWithTimeout_returnsWithoutException()80   public void testRunWithTimeout_returnsWithoutException() throws Exception {
81     timeLimiter.runWithTimeout(Runnables.doNothing(), DELAY_MS, TimeUnit.MILLISECONDS);
82   }
83 
testRunWithTimeout_wrapsUncheckedException()84   public void testRunWithTimeout_wrapsUncheckedException() throws Exception {
85     RuntimeException exception = new RuntimeException("test");
86     try {
87       timeLimiter.runWithTimeout(runnableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
88       fail("Expected UncheckedExecutionException");
89     } catch (UncheckedExecutionException e) {
90       assertThat(e.getCause()).isEqualTo(exception);
91     }
92   }
93 
testRunUninterruptiblyWithTimeout_wrapsUncheckedException()94   public void testRunUninterruptiblyWithTimeout_wrapsUncheckedException() throws Exception {
95     RuntimeException exception = new RuntimeException("test");
96     try {
97       timeLimiter.runUninterruptiblyWithTimeout(
98           runnableThrowing(exception), DELAY_MS, TimeUnit.MILLISECONDS);
99       fail("Expected UncheckedExecutionException");
100     } catch (UncheckedExecutionException e) {
101       assertThat(e.getCause()).isEqualTo(exception);
102     }
103   }
104 
callableThrowing(final Exception exception)105   public static <T> Callable<T> callableThrowing(final Exception exception) {
106     return new Callable<T>() {
107       @Override
108       public T call() throws Exception {
109         throw exception;
110       }
111     };
112   }
113 
114   private static Runnable runnableThrowing(final RuntimeException e) {
115     return new Runnable() {
116       @Override
117       public void run() {
118         throw e;
119       }
120     };
121   }
122 
123   @SuppressWarnings("serial")
124   private static class SampleCheckedException extends Exception {}
125 }
126