forked from google/guava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestingExecutorsTest.java
112 lines (102 loc) · 3.67 KB
/
TestingExecutorsTest.java
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
/*
* 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 static org.junit.Assert.assertThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
/**
* Tests for TestingExecutors.
*
* @author Eric Chang
*/
public class TestingExecutorsTest extends TestCase {
private volatile boolean taskDone;
public void testNoOpScheduledExecutor() throws InterruptedException {
taskDone = false;
Runnable task =
new Runnable() {
@Override
public void run() {
taskDone = true;
}
};
ScheduledFuture<?> future =
TestingExecutors.noOpScheduledExecutor().schedule(task, 10, TimeUnit.MILLISECONDS);
Thread.sleep(20);
assertFalse(taskDone);
assertFalse(future.isDone());
}
public void testNoOpScheduledExecutorShutdown() {
ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor();
assertFalse(executor.isShutdown());
assertFalse(executor.isTerminated());
executor.shutdown();
assertTrue(executor.isShutdown());
assertTrue(executor.isTerminated());
}
public void testNoOpScheduledExecutorInvokeAll() throws ExecutionException, InterruptedException {
ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor();
taskDone = false;
Callable<Boolean> task =
new Callable<Boolean>() {
@Override
public Boolean call() {
taskDone = true;
return taskDone;
}
};
List<Future<Boolean>> futureList =
executor.invokeAll(ImmutableList.of(task), 10, TimeUnit.MILLISECONDS);
Future<Boolean> future = futureList.get(0);
assertFalse(taskDone);
assertTrue(future.isDone());
assertThrows(CancellationException.class, () -> future.get());
}
public void testSameThreadScheduledExecutor() throws ExecutionException, InterruptedException {
taskDone = false;
Callable<Integer> task =
new Callable<Integer>() {
@Override
public Integer call() {
taskDone = true;
return 6;
}
};
Future<Integer> future =
TestingExecutors.sameThreadScheduledExecutor().schedule(task, 10000, TimeUnit.MILLISECONDS);
assertTrue("Should run callable immediately", taskDone);
assertEquals(6, (int) future.get());
}
public void testSameThreadScheduledExecutorWithException() throws InterruptedException {
Runnable runnable =
new Runnable() {
@Override
public void run() {
throw new RuntimeException("Oh no!");
}
};
Future<?> future = TestingExecutors.sameThreadScheduledExecutor().submit(runnable);
assertThrows(ExecutionException.class, () -> future.get());
}
}