aboutsummaryrefslogtreecommitdiffstats
path: root/guava-testlib/src/com/google/common/util/concurrent/testing/TestingExecutors.java
blob: 4e51d343221909d7fe93b253f2763f285833a7c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2012 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.util.concurrent.testing;

import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Longs;
import com.google.common.util.concurrent.AbstractFuture;
import com.google.common.util.concurrent.AbstractListeningExecutorService;
import com.google.common.util.concurrent.ListeningScheduledExecutorService;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Delayed;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * Factory methods for {@link ExecutorService} for testing.
 *
 * @author Chris Nokleberg
 * @since 14.0
 */
@Beta
public final class TestingExecutors {
  private TestingExecutors() {}

  /**
   * Returns a {@link ScheduledExecutorService} that never executes anything.
   *
   * <p>The {@code shutdownNow} method of the returned executor always returns an empty list despite
   * the fact that everything is still technically awaiting execution.
   * The {@code getDelay} method of any {@link ScheduledFuture} returned by the executor will always
   * return the max long value instead of the time until the user-specified delay.
   */
  public static ListeningScheduledExecutorService noOpScheduledExecutor() {
    return new NoOpScheduledExecutorService();
  }

  private static final class NoOpScheduledExecutorService
      extends AbstractListeningExecutorService implements ListeningScheduledExecutorService {

    private volatile boolean shutdown;

    @Override public void shutdown() {
      shutdown = true;
    }

    @Override public List<Runnable> shutdownNow() {
      shutdown();
      return ImmutableList.of();
    }

    @Override public boolean isShutdown() {
      return shutdown;
    }

    @Override public boolean isTerminated() {
      return shutdown;
    }

    @Override public boolean awaitTermination(long timeout, TimeUnit unit) {
      return true;
    }

    @Override public void execute(Runnable runnable) {}

    @Override public <V> ScheduledFuture<V> schedule(
        Callable<V> callable, long delay, TimeUnit unit) {
      return NeverScheduledFuture.create();
    }

    @Override public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
      return NeverScheduledFuture.create();
    }

    @Override public ScheduledFuture<?> scheduleAtFixedRate(
        Runnable command, long initialDelay, long period, TimeUnit unit) {
      return NeverScheduledFuture.create();
    }

    @Override public ScheduledFuture<?> scheduleWithFixedDelay(
        Runnable command, long initialDelay, long delay, TimeUnit unit) {
      return NeverScheduledFuture.create();
    }

    private static class NeverScheduledFuture<V>
        extends AbstractFuture<V> implements ScheduledFuture<V> {

      static <V> NeverScheduledFuture<V> create() {
        return new NeverScheduledFuture<V>();
      }

      @Override public long getDelay(TimeUnit unit) {
        return Long.MAX_VALUE;
      }

      @Override public int compareTo(Delayed other) {
        return Longs.compare(getDelay(TimeUnit.NANOSECONDS), other.getDelay(TimeUnit.NANOSECONDS));
      }
    }
  }
}