| 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 |
|
| 17 |
package com.google.common.util.concurrent.testing; |
| 18 |
|
| 19 |
import com.google.common.collect.ImmutableList; |
| 20 |
import com.google.common.util.concurrent.ListeningScheduledExecutorService; |
| 21 |
import java.util.List; |
| 22 |
import java.util.concurrent.Callable; |
| 23 |
import java.util.concurrent.CancellationException; |
| 24 |
import java.util.concurrent.ExecutionException; |
| 25 |
import java.util.concurrent.Future; |
| 26 |
import java.util.concurrent.ScheduledFuture; |
| 27 |
import java.util.concurrent.TimeUnit; |
| 28 |
import junit.framework.TestCase; |
| 29 |
|
| 30 |
/** |
| 31 |
* Tests for TestingExecutors. |
| 32 |
* |
| 33 |
* @author Eric Chang |
| 34 |
*/ |
| 35 |
public class TestingExecutorsTest extends TestCase { |
| 36 |
private volatile boolean taskDone; |
| 37 |
|
| 38 |
public void testNoOpScheduledExecutor() throws InterruptedException { |
| 39 |
taskDone = false; |
| 40 |
Runnable task = |
| 41 |
new Runnable() { |
| 42 |
@Override |
| 43 |
public void run() { |
| 44 |
taskDone = true; |
| 45 |
} |
| 46 |
}; |
| 47 |
ScheduledFuture<?> future = |
| 48 |
TestingExecutors.noOpScheduledExecutor().schedule(task, 10, TimeUnit.MILLISECONDS); |
| 49 |
Thread.sleep(20); |
| 50 |
assertFalse(taskDone); |
| 51 |
assertFalse(future.isDone()); |
| 52 |
} |
| 53 |
|
| 54 |
public void testNoOpScheduledExecutorShutdown() { |
| 55 |
ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor(); |
| 56 |
assertFalse(executor.isShutdown()); |
| 57 |
assertFalse(executor.isTerminated()); |
| 58 |
executor.shutdown(); |
| 59 |
assertTrue(executor.isShutdown()); |
| 60 |
assertTrue(executor.isTerminated()); |
| 61 |
} |
| 62 |
|
| 63 |
public void testNoOpScheduledExecutorInvokeAll() throws ExecutionException, InterruptedException { |
| 64 |
ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor(); |
| 65 |
taskDone = false; |
| 66 |
Callable<Boolean> task = |
| 67 |
new Callable<Boolean>() { |
| 68 |
@Override |
| 69 |
public Boolean call() { |
| 70 |
taskDone = true; |
| 71 |
return taskDone; |
| 72 |
} |
| 73 |
}; |
| 74 |
List<Future<Boolean>> futureList = |
| 75 |
executor.invokeAll(ImmutableList.of(task), 10, TimeUnit.MILLISECONDS); |
| 76 |
Future<Boolean> future = futureList.get(0); |
| 77 |
assertFalse(taskDone); |
| 78 |
assertTrue(future.isDone()); |
| 79 |
try { |
| 80 |
future.get(); |
| 81 |
fail(); |
| 82 |
} catch (CancellationException e) { |
| 83 |
// pass |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public void testSameThreadScheduledExecutor() throws ExecutionException, InterruptedException { |
| 88 |
taskDone = false; |
| 89 |
Callable<Integer> task = |
| 90 |
new Callable<Integer>() { |
| 91 |
@Override |
| 92 |
public Integer call() { |
| 93 |
taskDone = true; |
| 94 |
return 6; |
| 95 |
} |
| 96 |
}; |
| 97 |
Future<Integer> future = |
| 98 |
TestingExecutors.sameThreadScheduledExecutor().schedule(task, 10000, TimeUnit.MILLISECONDS); |
| 99 |
assertTrue("Should run callable immediately", taskDone); |
| 100 |
assertEquals(6, (int) future.get()); |
| 101 |
} |
| 102 |
|
| 103 |
public void testSameThreadScheduledExecutorWithException() throws InterruptedException { |
| 104 |
Runnable runnable = |
| 105 |
new Runnable() { |
| 106 |
@Override |
| 107 |
public void run() { |
| 108 |
throw new RuntimeException("Oh no!"); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
Future<?> future = TestingExecutors.sameThreadScheduledExecutor().submit(runnable); |
| 113 |
try { |
| 114 |
future.get(); |
| 115 |
fail("Should have thrown exception"); |
| 116 |
} catch (ExecutionException e) { |
| 117 |
// pass |
| 118 |
} |
| 119 |
} |
| 120 |
} |