Class FakeTimeLimiter
- java.lang.Object
-
- com.google.common.util.concurrent.FakeTimeLimiter
-
- All Implemented Interfaces:
TimeLimiter
@Beta public final class FakeTimeLimiter extends java.lang.Object implements TimeLimiter
A TimeLimiter implementation which actually does not attempt to limit time at all. This may be desirable to use in some unit tests. More importantly, attempting to debug a call which is time-limited would be extremely annoying, so this gives you a time-limiter you can easily swap in for your real time-limiter while you're debugging.- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description FakeTimeLimiter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> T
callWithTimeout(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit, boolean amInterruptible)
Invokes a specified Callable, timing out after the specified time limit.<T> T
newProxy(T target, java.lang.Class<T> interfaceType, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)
Returns an instance ofinterfaceType
that delegates all method calls to thetarget
object, enforcing the specified time limit on each call.
-
-
-
Method Detail
-
newProxy
public <T> T newProxy(T target, java.lang.Class<T> interfaceType, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)
Description copied from interface:TimeLimiter
Returns an instance ofinterfaceType
that delegates all method calls to thetarget
object, enforcing the specified time limit on each call. This time-limited delegation is also performed for calls toObject.equals(java.lang.Object)
,Object.hashCode()
, andObject.toString()
.If the target method call finishes before the limit is reached, the return value or exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is reached, the proxy will attempt to abort the call to the target, and will throw an
UncheckedTimeoutException
to the caller.It is important to note that the primary purpose of the proxy object is to return control to the caller when the timeout elapses; aborting the target method call is of secondary concern. The particular nature and strength of the guarantees made by the proxy is implementation-dependent. However, it is important that each of the methods on the target object behaves appropriately when its thread is interrupted.
- Specified by:
newProxy
in interfaceTimeLimiter
- Parameters:
target
- the object to proxyinterfaceType
- the interface you wish the returned proxy to implementtimeoutDuration
- with timeoutUnit, the maximum length of time that callers are willing to wait on each method call to the proxytimeoutUnit
- with timeoutDuration, the maximum length of time that callers are willing to wait on each method call to the proxy- Returns:
- a time-limiting proxy
-
callWithTimeout
public <T> T callWithTimeout(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit, boolean amInterruptible) throws java.lang.Exception
Description copied from interface:TimeLimiter
Invokes a specified Callable, timing out after the specified time limit. If the target method call finished before the limit is reached, the return value or exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw anUncheckedTimeoutException
to the caller.Warning: The future of this method is in doubt. It may be nuked, or changed significantly.
- Specified by:
callWithTimeout
in interfaceTimeLimiter
- Parameters:
callable
- the Callable to executetimeoutDuration
- with timeoutUnit, the maximum length of time to waittimeoutUnit
- with timeoutDuration, the maximum length of time to waitamInterruptible
- whether to respond to thread interruption by aborting the operation and throwing InterruptedException; if false, the operation is allowed to complete or time out, and the current thread's interrupt status is re-asserted.- Returns:
- the result returned by the Callable
- Throws:
java.lang.InterruptedException
- ifinterruptible
is true and our thread is interrupted during executionUncheckedTimeoutException
- if the time limit is reachedjava.lang.Exception
-
-