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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/*
* Copyright (C) 2006 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;
import static com.google.common.truth.Truth.assertThat;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Range;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;
import junit.framework.TestCase;
/**
* Unit test for {@link SimpleTimeLimiter}.
*
* @author kevinb
* @author Jens Nyman
*/
public class SimpleTimeLimiterTest extends TestCase {
private static final long DELAY_MS = 50;
private static final long ENOUGH_MS = 10000;
private static final long NOT_ENOUGH_MS = 5;
private static final String GOOD_CALLABLE_RESULT = "good callable result";
private static final Callable<String> GOOD_CALLABLE =
new Callable<String>() {
@Override
public String call() throws InterruptedException {
MILLISECONDS.sleep(DELAY_MS);
return GOOD_CALLABLE_RESULT;
}
};
private static final Callable<String> BAD_CALLABLE =
new Callable<String>() {
@Override
public String call() throws InterruptedException, SampleException {
MILLISECONDS.sleep(DELAY_MS);
throw new SampleException();
}
};
private static final Runnable GOOD_RUNNABLE =
new Runnable() {
@Override
public void run() {
try {
MILLISECONDS.sleep(DELAY_MS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};
private static final Runnable BAD_RUNNABLE =
new Runnable() {
@Override
public void run() {
try {
MILLISECONDS.sleep(DELAY_MS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
throw new SampleRuntimeException();
}
};
private TimeLimiter service;
private static final ExecutorService executor = Executors.newFixedThreadPool(1);
@Override
protected void setUp() throws Exception {
super.setUp();
service = SimpleTimeLimiter.create(executor);
}
public void testNewProxy_goodMethodWithEnoughTime() throws Exception {
SampleImpl target = new SampleImpl(DELAY_MS);
Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS);
Stopwatch stopwatch = Stopwatch.createStarted();
String result = proxy.sleepThenReturnInput("x");
assertThat(result).isEqualTo("x");
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS));
assertThat(target.finished).isTrue();
}
public void testNewProxy_goodMethodWithNotEnoughTime() throws Exception {
SampleImpl target = new SampleImpl(9999);
Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS);
Stopwatch stopwatch = Stopwatch.createStarted();
try {
proxy.sleepThenReturnInput("x");
fail("no exception thrown");
} catch (UncheckedTimeoutException expected) {
}
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2));
// Is it still computing away anyway?
assertThat(target.finished).isFalse();
MILLISECONDS.sleep(ENOUGH_MS);
assertThat(target.finished).isFalse();
}
public void testNewProxy_badMethodWithEnoughTime() throws Exception {
SampleImpl target = new SampleImpl(DELAY_MS);
Sample proxy = service.newProxy(target, Sample.class, ENOUGH_MS, MILLISECONDS);
Stopwatch stopwatch = Stopwatch.createStarted();
try {
proxy.sleepThenThrowException();
fail("no exception thrown");
} catch (SampleException expected) {
}
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS));
}
public void testNewProxy_badMethodWithNotEnoughTime() throws Exception {
SampleImpl target = new SampleImpl(9999);
Sample proxy = service.newProxy(target, Sample.class, NOT_ENOUGH_MS, MILLISECONDS);
Stopwatch stopwatch = Stopwatch.createStarted();
try {
proxy.sleepThenThrowException();
fail("no exception thrown");
} catch (UncheckedTimeoutException expected) {
}
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(NOT_ENOUGH_MS, DELAY_MS * 2));
}
public void testCallWithTimeout_goodCallableWithEnoughTime() throws Exception {
Stopwatch stopwatch = Stopwatch.createStarted();
String result = service.callWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS);
assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT);
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS));
}
public void testCallWithTimeout_goodCallableWithNotEnoughTime() throws Exception {
try {
service.callWithTimeout(GOOD_CALLABLE, NOT_ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (TimeoutException expected) {
}
}
public void testCallWithTimeout_badCallableWithEnoughTime() throws Exception {
try {
service.callWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (ExecutionException expected) {
assertThat(expected.getCause()).isInstanceOf(SampleException.class);
}
}
public void testCallUninterruptiblyWithTimeout_goodCallableWithEnoughTime() throws Exception {
Stopwatch stopwatch = Stopwatch.createStarted();
String result = service.callUninterruptiblyWithTimeout(GOOD_CALLABLE, ENOUGH_MS, MILLISECONDS);
assertThat(result).isEqualTo(GOOD_CALLABLE_RESULT);
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS));
}
public void testCallUninterruptiblyWithTimeout_goodCallableWithNotEnoughTime() throws Exception {
try {
service.callUninterruptiblyWithTimeout(GOOD_CALLABLE, NOT_ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (TimeoutException expected) {
}
}
public void testCallUninterruptiblyWithTimeout_badCallableWithEnoughTime() throws Exception {
try {
service.callUninterruptiblyWithTimeout(BAD_CALLABLE, ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (ExecutionException expected) {
assertThat(expected.getCause()).isInstanceOf(SampleException.class);
}
}
public void testRunWithTimeout_goodRunnableWithEnoughTime() throws Exception {
Stopwatch stopwatch = Stopwatch.createStarted();
service.runWithTimeout(GOOD_RUNNABLE, ENOUGH_MS, MILLISECONDS);
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS));
}
public void testRunWithTimeout_goodRunnableWithNotEnoughTime() throws Exception {
try {
service.runWithTimeout(GOOD_RUNNABLE, NOT_ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (TimeoutException expected) {
}
}
public void testRunWithTimeout_badRunnableWithEnoughTime() throws Exception {
try {
service.runWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (UncheckedExecutionException expected) {
assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class);
}
}
public void testRunUninterruptiblyWithTimeout_goodRunnableWithEnoughTime() throws Exception {
Stopwatch stopwatch = Stopwatch.createStarted();
service.runUninterruptiblyWithTimeout(GOOD_RUNNABLE, ENOUGH_MS, MILLISECONDS);
assertThat(stopwatch.elapsed(MILLISECONDS)).isIn(Range.closed(DELAY_MS, ENOUGH_MS));
}
public void testRunUninterruptiblyWithTimeout_goodRunnableWithNotEnoughTime() throws Exception {
try {
service.runUninterruptiblyWithTimeout(GOOD_RUNNABLE, NOT_ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (TimeoutException expected) {
}
}
public void testRunUninterruptiblyWithTimeout_badRunnableWithEnoughTime() throws Exception {
try {
service.runUninterruptiblyWithTimeout(BAD_RUNNABLE, ENOUGH_MS, MILLISECONDS);
fail("no exception thrown");
} catch (UncheckedExecutionException expected) {
assertThat(expected.getCause()).isInstanceOf(SampleRuntimeException.class);
}
}
private interface Sample {
String sleepThenReturnInput(String input);
void sleepThenThrowException() throws SampleException;
}
@SuppressWarnings("serial")
private static class SampleException extends Exception {}
@SuppressWarnings("serial")
private static class SampleRuntimeException extends RuntimeException {}
private static class SampleImpl implements Sample {
final long delayMillis;
boolean finished;
SampleImpl(long delayMillis) {
this.delayMillis = delayMillis;
}
@Override
public String sleepThenReturnInput(String input) {
try {
MILLISECONDS.sleep(delayMillis);
finished = true;
return input;
} catch (InterruptedException e) {
return null;
}
}
@Override
public void sleepThenThrowException() throws SampleException {
try {
MILLISECONDS.sleep(delayMillis);
} catch (InterruptedException e) {
}
throw new SampleException();
}
}
}
|