1 /* 2 * Copyright (C) 2013 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.google.common.util.concurrent; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import java.util.Collection; 22 import java.util.List; 23 import java.util.concurrent.Callable; 24 import java.util.concurrent.ExecutionException; 25 import java.util.concurrent.Executors; 26 import java.util.concurrent.Future; 27 import java.util.concurrent.ScheduledExecutorService; 28 import java.util.concurrent.ScheduledFuture; 29 import java.util.concurrent.TimeUnit; 30 import java.util.concurrent.TimeoutException; 31 import junit.framework.TestCase; 32 33 /** 34 * Test for {@link WrappingScheduledExecutorService} 35 * 36 * @author Luke Sandberg 37 */ 38 public class WrappingScheduledExecutorServiceTest extends TestCase { 39 private static final Runnable DO_NOTHING = 40 new Runnable() { 41 @Override 42 public void run() {} 43 }; 44 testSchedule()45 public void testSchedule() { 46 MockExecutor mock = new MockExecutor(); 47 TestExecutor testExecutor = new TestExecutor(mock); 48 49 @SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored 50 Future<?> possiblyIgnoredError = testExecutor.schedule(DO_NOTHING, 10, TimeUnit.MINUTES); 51 mock.assertLastMethodCalled("scheduleRunnable", 10, TimeUnit.MINUTES); 52 53 @SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored 54 Future<?> possiblyIgnoredError1 = 55 testExecutor.schedule(Executors.callable(DO_NOTHING), 5, TimeUnit.SECONDS); 56 mock.assertLastMethodCalled("scheduleCallable", 5, TimeUnit.SECONDS); 57 } 58 testSchedule_repeating()59 public void testSchedule_repeating() { 60 MockExecutor mock = new MockExecutor(); 61 TestExecutor testExecutor = new TestExecutor(mock); 62 @SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored 63 Future<?> possiblyIgnoredError = 64 testExecutor.scheduleWithFixedDelay(DO_NOTHING, 100, 10, TimeUnit.MINUTES); 65 mock.assertLastMethodCalled("scheduleWithFixedDelay", 100, 10, TimeUnit.MINUTES); 66 67 @SuppressWarnings("unused") // https://errorprone.info/bugpattern/FutureReturnValueIgnored 68 Future<?> possiblyIgnoredError1 = 69 testExecutor.scheduleAtFixedRate(DO_NOTHING, 3, 7, TimeUnit.SECONDS); 70 mock.assertLastMethodCalled("scheduleAtFixedRate", 3, 7, TimeUnit.SECONDS); 71 } 72 73 private static final class WrappedCallable<T> implements Callable<T> { 74 private final Callable<T> delegate; 75 WrappedCallable(Callable<T> delegate)76 public WrappedCallable(Callable<T> delegate) { 77 this.delegate = delegate; 78 } 79 80 @Override call()81 public T call() throws Exception { 82 return delegate.call(); 83 } 84 } 85 86 private static final class WrappedRunnable implements Runnable { 87 private final Runnable delegate; 88 WrappedRunnable(Runnable delegate)89 public WrappedRunnable(Runnable delegate) { 90 this.delegate = delegate; 91 } 92 93 @Override run()94 public void run() { 95 delegate.run(); 96 } 97 } 98 99 private static final class TestExecutor extends WrappingScheduledExecutorService { TestExecutor(MockExecutor mock)100 public TestExecutor(MockExecutor mock) { 101 super(mock); 102 } 103 104 @Override wrapTask(Callable<T> callable)105 protected <T> Callable<T> wrapTask(Callable<T> callable) { 106 return new WrappedCallable<T>(callable); 107 } 108 109 @Override wrapTask(Runnable command)110 protected Runnable wrapTask(Runnable command) { 111 return new WrappedRunnable(command); 112 } 113 } 114 115 private static final class MockExecutor implements ScheduledExecutorService { 116 String lastMethodCalled = ""; 117 long lastInitialDelay; 118 long lastDelay; 119 TimeUnit lastUnit; 120 assertLastMethodCalled(String method, long delay, TimeUnit unit)121 void assertLastMethodCalled(String method, long delay, TimeUnit unit) { 122 assertEquals(method, lastMethodCalled); 123 assertEquals(delay, lastDelay); 124 assertEquals(unit, lastUnit); 125 } 126 assertLastMethodCalled(String method, long initialDelay, long delay, TimeUnit unit)127 void assertLastMethodCalled(String method, long initialDelay, long delay, TimeUnit unit) { 128 assertEquals(method, lastMethodCalled); 129 assertEquals(initialDelay, lastInitialDelay); 130 assertEquals(delay, lastDelay); 131 assertEquals(unit, lastUnit); 132 } 133 134 @Override schedule(Runnable command, long delay, TimeUnit unit)135 public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { 136 assertThat(command).isInstanceOf(WrappedRunnable.class); 137 lastMethodCalled = "scheduleRunnable"; 138 lastDelay = delay; 139 lastUnit = unit; 140 return null; 141 } 142 143 @Override schedule(Callable<V> callable, long delay, TimeUnit unit)144 public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { 145 assertThat(callable).isInstanceOf(WrappedCallable.class); 146 lastMethodCalled = "scheduleCallable"; 147 lastDelay = delay; 148 lastUnit = unit; 149 return null; 150 } 151 152 @Override scheduleAtFixedRate( Runnable command, long initialDelay, long period, TimeUnit unit)153 public ScheduledFuture<?> scheduleAtFixedRate( 154 Runnable command, long initialDelay, long period, TimeUnit unit) { 155 assertThat(command).isInstanceOf(WrappedRunnable.class); 156 lastMethodCalled = "scheduleAtFixedRate"; 157 lastInitialDelay = initialDelay; 158 lastDelay = period; 159 lastUnit = unit; 160 return null; 161 } 162 163 @Override scheduleWithFixedDelay( Runnable command, long initialDelay, long delay, TimeUnit unit)164 public ScheduledFuture<?> scheduleWithFixedDelay( 165 Runnable command, long initialDelay, long delay, TimeUnit unit) { 166 assertThat(command).isInstanceOf(WrappedRunnable.class); 167 lastMethodCalled = "scheduleWithFixedDelay"; 168 lastInitialDelay = initialDelay; 169 lastDelay = delay; 170 lastUnit = unit; 171 return null; 172 } 173 174 // No need to test these methods as they are handled by WrappingExecutorServiceTest 175 @Override awaitTermination(long timeout, TimeUnit unit)176 public boolean awaitTermination(long timeout, TimeUnit unit) { 177 throw new UnsupportedOperationException(); 178 } 179 180 @Override invokeAll(Collection<? extends Callable<T>> tasks)181 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) 182 throws InterruptedException { 183 throw new UnsupportedOperationException(); 184 } 185 186 @Override invokeAll( Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)187 public <T> List<Future<T>> invokeAll( 188 Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 189 throws InterruptedException { 190 throw new UnsupportedOperationException(); 191 } 192 193 @Override invokeAny(Collection<? extends Callable<T>> tasks)194 public <T> T invokeAny(Collection<? extends Callable<T>> tasks) 195 throws ExecutionException, InterruptedException { 196 throw new UnsupportedOperationException(); 197 } 198 199 @Override invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)200 public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 201 throws ExecutionException, InterruptedException, TimeoutException { 202 throw new UnsupportedOperationException(); 203 } 204 205 @Override isShutdown()206 public boolean isShutdown() { 207 throw new UnsupportedOperationException(); 208 } 209 210 @Override isTerminated()211 public boolean isTerminated() { 212 throw new UnsupportedOperationException(); 213 } 214 215 @Override shutdown()216 public void shutdown() { 217 throw new UnsupportedOperationException(); 218 } 219 220 @Override shutdownNow()221 public List<Runnable> shutdownNow() { 222 throw new UnsupportedOperationException(); 223 } 224 225 @Override submit(Callable<T> task)226 public <T> Future<T> submit(Callable<T> task) { 227 throw new UnsupportedOperationException(); 228 } 229 230 @Override submit(Runnable task)231 public Future<?> submit(Runnable task) { 232 throw new UnsupportedOperationException(); 233 } 234 235 @Override submit(Runnable task, T result)236 public <T> Future<T> submit(Runnable task, T result) { 237 throw new UnsupportedOperationException(); 238 } 239 240 @Override execute(Runnable command)241 public void execute(Runnable command) { 242 throw new UnsupportedOperationException(); 243 } 244 } 245 } 246