Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/schedulers/InstantPeriodicTaskTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.schedulers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19 import java.util.concurrent.*;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.RxJavaTest;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.internal.functions.Functions;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class InstantPeriodicTaskTest extends RxJavaTest {
30
31 @Test
32 public void taskCrash() throws Exception {
33 ExecutorService exec = Executors.newSingleThreadExecutor();
34 List<Throwable> errors = TestHelper.trackPluginErrors();
35 try {
36
37 InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
38 @Override
39 public void run() {
40 throw new TestException();
41 }
42 }, exec);
43
44 try {
45 task.call();
46 fail("Should have thrown!");
47 } catch (TestException excepted) {
48 // excepted
49 }
50
51 TestHelper.assertUndeliverable(errors, 0, TestException.class);
52 } finally {
53 exec.shutdownNow();
54 RxJavaPlugins.reset();
55 }
56 }
57
58 @Test
59 public void dispose() throws Exception {
60 ExecutorService exec = Executors.newSingleThreadExecutor();
61 try {
62
63 InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
64 @Override
65 public void run() {
66 throw new TestException();
67 }
68 }, exec);
69
70 assertFalse(task.isDisposed());
71
72 task.dispose();
73
74 assertTrue(task.isDisposed());
75
76 task.dispose();
77
78 assertTrue(task.isDisposed());
79 } finally {
80 exec.shutdownNow();
81 RxJavaPlugins.reset();
82 }
83 }
84
85 @Test
86 public void dispose2() throws Exception {
87 ExecutorService exec = Executors.newSingleThreadExecutor();
88 try {
89
90 InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
91 @Override
92 public void run() {
93 throw new TestException();
94 }
95 }, exec);
96
97 task.setFirst(new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null));
98 task.setRest(new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null));
99
100 assertFalse(task.isDisposed());
101
102 task.dispose();
103
104 assertTrue(task.isDisposed());
105
106 task.dispose();
107
108 assertTrue(task.isDisposed());
109 } finally {
110 exec.shutdownNow();
111 RxJavaPlugins.reset();
112 }
113 }
114
115 @Test
116 public void dispose2CurrentThread() throws Exception {
117 ExecutorService exec = Executors.newSingleThreadExecutor();
118 try {
119
120 InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
121 @Override
122 public void run() {
123 throw new TestException();
124 }
125 }, exec);
126
127 task.runner = Thread.currentThread();
128
129 task.setFirst(new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null));
130 task.setRest(new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null));
131
132 assertFalse(task.isDisposed());
133
134 task.dispose();
135
136 assertTrue(task.isDisposed());
137
138 task.dispose();
139
140 assertTrue(task.isDisposed());
141 } finally {
142 exec.shutdownNow();
143 RxJavaPlugins.reset();
144 }
145 }
146
147 @Test
148 public void dispose3() throws Exception {
149 ExecutorService exec = Executors.newSingleThreadExecutor();
150 try {
151
152 InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
153 @Override
154 public void run() {
155 throw new TestException();
156 }
157 }, exec);
158
159 task.dispose();
160
161 FutureTask<Void> f1 = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
162 task.setFirst(f1);
163
164 assertTrue(f1.isCancelled());
165
166 FutureTask<Void> f2 = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
167 task.setRest(f2);
168
169 assertTrue(f2.isCancelled());
170 } finally {
171 exec.shutdownNow();
172 RxJavaPlugins.reset();
173 }
174 }
175
176 @Test
177 public void disposeOnCurrentThread() throws Exception {
178 ExecutorService exec = Executors.newSingleThreadExecutor();
179 try {
180
181 InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
182 @Override
183 public void run() {
184 throw new TestException();
185 }
186 }, exec);
187
188 task.runner = Thread.currentThread();
189
190 task.dispose();
191
192 FutureTask<Void> f1 = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
193 task.setFirst(f1);
194
195 assertTrue(f1.isCancelled());
196
197 FutureTask<Void> f2 = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
198 task.setRest(f2);
199
200 assertTrue(f2.isCancelled());
201 } finally {
202 exec.shutdownNow();
203 RxJavaPlugins.reset();
204 }
205 }
206
207 @Test
208 public void firstCancelRace() throws Exception {
209 ExecutorService exec = Executors.newSingleThreadExecutor();
210 try {
211 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
212 final InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
213 @Override
214 public void run() {
215 throw new TestException();
216 }
217 }, exec);
218
219 final FutureTask<Void> f1 = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
220 Runnable r1 = new Runnable() {
221 @Override
222 public void run() {
223 task.setFirst(f1);
224 }
225 };
226 Runnable r2 = new Runnable() {
227 @Override
228 public void run() {
229 task.dispose();
230 }
231 };
232
233 TestHelper.race(r1, r2);
234
235 assertTrue(f1.isCancelled());
236 assertTrue(task.isDisposed());
237 }
238 } finally {
239 exec.shutdownNow();
240 RxJavaPlugins.reset();
241 }
242 }
243
244 @Test
245 public void restCancelRace() throws Exception {
246 ExecutorService exec = Executors.newSingleThreadExecutor();
247 try {
248 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
249 final InstantPeriodicTask task = new InstantPeriodicTask(new Runnable() {
250 @Override
251 public void run() {
252 throw new TestException();
253 }
254 }, exec);
255
256 final FutureTask<Void> f1 = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
257 Runnable r1 = new Runnable() {
258 @Override
259 public void run() {
260 task.setRest(f1);
261 }
262 };
263 Runnable r2 = new Runnable() {
264 @Override
265 public void run() {
266 task.dispose();
267 }
268 };
269
270 TestHelper.race(r1, r2);
271
272 assertTrue(f1.isCancelled());
273 assertTrue(task.isDisposed());
274 }
275 } finally {
276 exec.shutdownNow();
277 RxJavaPlugins.reset();
278 }
279 }
280 }