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