Java

Форк
0
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

17
package com.google.common.util.concurrent.testing;
18

19
import static org.junit.Assert.assertThrows;
20

21
import com.google.common.collect.ImmutableList;
22
import com.google.common.util.concurrent.ListeningScheduledExecutorService;
23
import java.util.List;
24
import java.util.concurrent.Callable;
25
import java.util.concurrent.CancellationException;
26
import java.util.concurrent.ExecutionException;
27
import java.util.concurrent.Future;
28
import java.util.concurrent.ScheduledFuture;
29
import java.util.concurrent.TimeUnit;
30
import junit.framework.TestCase;
31

32
/**
33
 * Tests for TestingExecutors.
34
 *
35
 * @author Eric Chang
36
 */
37
public class TestingExecutorsTest extends TestCase {
38
  private volatile boolean taskDone;
39

40
  public void testNoOpScheduledExecutor() throws InterruptedException {
41
    taskDone = false;
42
    Runnable task =
43
        new Runnable() {
44
          @Override
45
          public void run() {
46
            taskDone = true;
47
          }
48
        };
49
    ScheduledFuture<?> future =
50
        TestingExecutors.noOpScheduledExecutor().schedule(task, 10, TimeUnit.MILLISECONDS);
51
    Thread.sleep(20);
52
    assertFalse(taskDone);
53
    assertFalse(future.isDone());
54
  }
55

56
  public void testNoOpScheduledExecutorShutdown() {
57
    ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor();
58
    assertFalse(executor.isShutdown());
59
    assertFalse(executor.isTerminated());
60
    executor.shutdown();
61
    assertTrue(executor.isShutdown());
62
    assertTrue(executor.isTerminated());
63
  }
64

65
  public void testNoOpScheduledExecutorInvokeAll() throws ExecutionException, InterruptedException {
66
    ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor();
67
    taskDone = false;
68
    Callable<Boolean> task =
69
        new Callable<Boolean>() {
70
          @Override
71
          public Boolean call() {
72
            taskDone = true;
73
            return taskDone;
74
          }
75
        };
76
    List<Future<Boolean>> futureList =
77
        executor.invokeAll(ImmutableList.of(task), 10, TimeUnit.MILLISECONDS);
78
    Future<Boolean> future = futureList.get(0);
79
    assertFalse(taskDone);
80
    assertTrue(future.isDone());
81
    assertThrows(CancellationException.class, () -> future.get());
82
  }
83

84
  public void testSameThreadScheduledExecutor() throws ExecutionException, InterruptedException {
85
    taskDone = false;
86
    Callable<Integer> task =
87
        new Callable<Integer>() {
88
          @Override
89
          public Integer call() {
90
            taskDone = true;
91
            return 6;
92
          }
93
        };
94
    Future<Integer> future =
95
        TestingExecutors.sameThreadScheduledExecutor().schedule(task, 10000, TimeUnit.MILLISECONDS);
96
    assertTrue("Should run callable immediately", taskDone);
97
    assertEquals(6, (int) future.get());
98
  }
99

100
  public void testSameThreadScheduledExecutorWithException() throws InterruptedException {
101
    Runnable runnable =
102
        new Runnable() {
103
          @Override
104
          public void run() {
105
            throw new RuntimeException("Oh no!");
106
          }
107
        };
108

109
    Future<?> future = TestingExecutors.sameThreadScheduledExecutor().submit(runnable);
110
    assertThrows(ExecutionException.class, () -> future.get());
111
  }
112
}
113

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.