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