Class FakeTimeLimiter

  • All Implemented Interfaces:
    TimeLimiter

    @GwtIncompatible
    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
    Author:
    Kevin Bourrillion, Jens Nyman
    • Constructor Summary

      Constructors 
      Constructor Description
      FakeTimeLimiter()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T extends @Nullable java.lang.Object>
      T
      callUninterruptiblyWithTimeout​(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)
      Invokes a specified Callable, timing out after the specified time limit.
      <T extends @Nullable java.lang.Object>
      T
      callWithTimeout​(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)
      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 of interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call.
      void runUninterruptiblyWithTimeout​(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)
      Invokes a specified Runnable, timing out after the specified time limit.
      void runWithTimeout​(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)
      Invokes a specified Runnable, timing out after the specified time limit.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • newProxy

        @CanIgnoreReturnValue
        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 of interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call. This time-limited delegation is also performed for calls to Object.equals(java.lang.Object), Object.hashCode(), and Object.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.

        For example, to return the value of target.someMethod(), but substitute DEFAULT_VALUE if this method call takes over 50 ms, you can use this code:

           TimeLimiter limiter = . . .;
           TargetType proxy = limiter.newProxy(
               target, TargetType.class, 50, TimeUnit.MILLISECONDS);
           try {
             return proxy.someMethod();
           } catch (UncheckedTimeoutException e) {
             return DEFAULT_VALUE;
           }
         
        Specified by:
        newProxy in interface TimeLimiter
        Parameters:
        target - the object to proxy
        interfaceType - the interface you wish the returned proxy to implement
        timeoutDuration - with timeoutUnit, the maximum length of time that callers are willing to wait on each method call to the proxy
        timeoutUnit - 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

        @CanIgnoreReturnValue
        public <T extends @Nullable java.lang.Object> T callWithTimeout​(java.util.concurrent.Callable<T> callable,
                                                                        long timeoutDuration,
                                                                        java.util.concurrent.TimeUnit timeoutUnit)
                                                                 throws java.util.concurrent.ExecutionException
        Description copied from interface: TimeLimiter
        Invokes a specified Callable, timing out after the specified time limit. If the target method call finishes before the limit is reached, the return value or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw a TimeoutException to the caller.
        Specified by:
        callWithTimeout in interface TimeLimiter
        Parameters:
        callable - the Callable to execute
        timeoutDuration - with timeoutUnit, the maximum length of time to wait
        timeoutUnit - with timeoutDuration, the maximum length of time to wait
        Returns:
        the result returned by the Callable
        Throws:
        java.util.concurrent.ExecutionException - if callable throws a checked exception
      • callUninterruptiblyWithTimeout

        @CanIgnoreReturnValue
        public <T extends @Nullable java.lang.Object> T callUninterruptiblyWithTimeout​(java.util.concurrent.Callable<T> callable,
                                                                                       long timeoutDuration,
                                                                                       java.util.concurrent.TimeUnit timeoutUnit)
                                                                                throws java.util.concurrent.ExecutionException
        Description copied from interface: TimeLimiter
        Invokes a specified Callable, timing out after the specified time limit. If the target method call finishes before the limit is reached, the return value or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw a TimeoutException to the caller.

        The difference with TimeLimiter.callWithTimeout(Callable, long, TimeUnit) is that this method will ignore interrupts on the current thread.

        Specified by:
        callUninterruptiblyWithTimeout in interface TimeLimiter
        Parameters:
        callable - the Callable to execute
        timeoutDuration - with timeoutUnit, the maximum length of time to wait
        timeoutUnit - with timeoutDuration, the maximum length of time to wait
        Returns:
        the result returned by the Callable
        Throws:
        java.util.concurrent.ExecutionException - if callable throws a checked exception
      • runWithTimeout

        public void runWithTimeout​(java.lang.Runnable runnable,
                                   long timeoutDuration,
                                   java.util.concurrent.TimeUnit timeoutUnit)
        Description copied from interface: TimeLimiter
        Invokes a specified Runnable, timing out after the specified time limit. If the target method run finishes before the limit is reached, this method returns or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and throw a TimeoutException to the caller.
        Specified by:
        runWithTimeout in interface TimeLimiter
        Parameters:
        runnable - the Runnable to execute
        timeoutDuration - with timeoutUnit, the maximum length of time to wait
        timeoutUnit - with timeoutDuration, the maximum length of time to wait
      • runUninterruptiblyWithTimeout

        public void runUninterruptiblyWithTimeout​(java.lang.Runnable runnable,
                                                  long timeoutDuration,
                                                  java.util.concurrent.TimeUnit timeoutUnit)
        Description copied from interface: TimeLimiter
        Invokes a specified Runnable, timing out after the specified time limit. If the target method run finishes before the limit is reached, this method returns or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and throw a TimeoutException to the caller.

        The difference with TimeLimiter.runWithTimeout(Runnable, long, TimeUnit) is that this method will ignore interrupts on the current thread.

        Specified by:
        runUninterruptiblyWithTimeout in interface TimeLimiter
        Parameters:
        runnable - the Runnable to execute
        timeoutDuration - with timeoutUnit, the maximum length of time to wait
        timeoutUnit - with timeoutDuration, the maximum length of time to wait