TestingExecutors.java revision 3c77433663281544363151bf284b0240dfd22a42
1/*
2 * Copyright (C) 2012 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
17package com.google.common.util.concurrent.testing;
18
19import com.google.common.annotations.Beta;
20import com.google.common.collect.ImmutableList;
21import com.google.common.primitives.Longs;
22import com.google.common.util.concurrent.AbstractFuture;
23import com.google.common.util.concurrent.AbstractListeningExecutorService;
24import com.google.common.util.concurrent.ListeningScheduledExecutorService;
25
26import java.util.List;
27import java.util.concurrent.Callable;
28import java.util.concurrent.Delayed;
29import java.util.concurrent.ScheduledFuture;
30import java.util.concurrent.TimeUnit;
31
32/**
33 * Factory methods for {@link ExecutorService} for testing.
34 *
35 * @author Chris Nokleberg
36 * @since 14.0
37 */
38@Beta
39public final class TestingExecutors {
40  private TestingExecutors() {}
41
42  /**
43   * Returns a {@link ScheduledExecutorService} that never executes anything.
44   *
45   * <p>The {@code shutdownNow} method of the returned executor always returns an empty list despite
46   * the fact that everything is still technically awaiting execution.
47   * The {@code getDelay} method of any {@link ScheduledFuture} returned by the executor will always
48   * return the max long value instead of the time until the user-specified delay.
49   */
50  public static ListeningScheduledExecutorService noOpScheduledExecutor() {
51    return new NoOpScheduledExecutorService();
52  }
53
54  private static final class NoOpScheduledExecutorService
55      extends AbstractListeningExecutorService implements ListeningScheduledExecutorService {
56
57    private volatile boolean shutdown;
58
59    @Override public void shutdown() {
60      shutdown = true;
61    }
62
63    @Override public List<Runnable> shutdownNow() {
64      shutdown();
65      return ImmutableList.of();
66    }
67
68    @Override public boolean isShutdown() {
69      return shutdown;
70    }
71
72    @Override public boolean isTerminated() {
73      return shutdown;
74    }
75
76    @Override public boolean awaitTermination(long timeout, TimeUnit unit) {
77      return true;
78    }
79
80    @Override public void execute(Runnable runnable) {}
81
82    @Override public <V> ScheduledFuture<V> schedule(
83        Callable<V> callable, long delay, TimeUnit unit) {
84      return NeverScheduledFuture.create();
85    }
86
87    @Override public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
88      return NeverScheduledFuture.create();
89    }
90
91    @Override public ScheduledFuture<?> scheduleAtFixedRate(
92        Runnable command, long initialDelay, long period, TimeUnit unit) {
93      return NeverScheduledFuture.create();
94    }
95
96    @Override public ScheduledFuture<?> scheduleWithFixedDelay(
97        Runnable command, long initialDelay, long delay, TimeUnit unit) {
98      return NeverScheduledFuture.create();
99    }
100
101    private static class NeverScheduledFuture<V>
102        extends AbstractFuture<V> implements ScheduledFuture<V> {
103
104      static <V> NeverScheduledFuture<V> create() {
105        return new NeverScheduledFuture<V>();
106      }
107
108      @Override public long getDelay(TimeUnit unit) {
109        return Long.MAX_VALUE;
110      }
111
112      @Override public int compareTo(Delayed other) {
113        return Longs.compare(getDelay(TimeUnit.NANOSECONDS), other.getDelay(TimeUnit.NANOSECONDS));
114      }
115    }
116  }
117}
118