Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/schedulers/AbstractDirectTaskTest.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.concurrent.FutureTask;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.RxJavaTest;
23 import io.reactivex.rxjava3.internal.functions.Functions;
24 import io.reactivex.rxjava3.testsupport.TestHelper;
25
26 public class AbstractDirectTaskTest extends RxJavaTest {
27
28 @Test
29 public void cancelSetFuture() {
30 AbstractDirectTask task = new AbstractDirectTask(Functions.EMPTY_RUNNABLE, true) {
31 private static final long serialVersionUID = 208585707945686116L;
32 };
33 final Boolean[] interrupted = { null };
34
35 assertFalse(task.isDisposed());
36
37 task.dispose();
38
39 assertTrue(task.isDisposed());
40
41 task.dispose();
42
43 assertTrue(task.isDisposed());
44
45 FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null) {
46 @Override
47 public boolean cancel(boolean mayInterruptIfRunning) {
48 interrupted[0] = mayInterruptIfRunning;
49 return super.cancel(mayInterruptIfRunning);
50 }
51 };
52 task.setFuture(ft);
53
54 assertTrue(interrupted[0]);
55
56 assertTrue(task.isDisposed());
57 }
58
59 @Test
60 public void cancelSetFutureCurrentThread() {
61 AbstractDirectTask task = new AbstractDirectTask(Functions.EMPTY_RUNNABLE, true) {
62 private static final long serialVersionUID = 208585707945686116L;
63 };
64 final Boolean[] interrupted = { null };
65
66 assertFalse(task.isDisposed());
67
68 task.runner = Thread.currentThread();
69
70 task.dispose();
71
72 assertTrue(task.isDisposed());
73
74 task.dispose();
75
76 assertTrue(task.isDisposed());
77
78 FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null) {
79 @Override
80 public boolean cancel(boolean mayInterruptIfRunning) {
81 interrupted[0] = mayInterruptIfRunning;
82 return super.cancel(mayInterruptIfRunning);
83 }
84 };
85 task.setFuture(ft);
86
87 assertFalse(interrupted[0]);
88
89 assertTrue(task.isDisposed());
90 }
91
92 @Test
93 public void setFutureCancel() {
94 AbstractDirectTask task = new AbstractDirectTask(Functions.EMPTY_RUNNABLE, true) {
95 private static final long serialVersionUID = 208585707945686116L;
96 };
97 final Boolean[] interrupted = { null };
98
99 FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null) {
100 @Override
101 public boolean cancel(boolean mayInterruptIfRunning) {
102 interrupted[0] = mayInterruptIfRunning;
103 return super.cancel(mayInterruptIfRunning);
104 }
105 };
106
107 assertFalse(task.isDisposed());
108
109 task.setFuture(ft);
110
111 assertFalse(task.isDisposed());
112
113 task.dispose();
114
115 assertTrue(task.isDisposed());
116
117 assertTrue(interrupted[0]);
118 }
119
120 @Test
121 public void setFutureCancelSameThread() {
122 AbstractDirectTask task = new AbstractDirectTask(Functions.EMPTY_RUNNABLE, true) {
123 private static final long serialVersionUID = 208585707945686116L;
124 };
125 final Boolean[] interrupted = { null };
126
127 FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null) {
128 @Override
129 public boolean cancel(boolean mayInterruptIfRunning) {
130 interrupted[0] = mayInterruptIfRunning;
131 return super.cancel(mayInterruptIfRunning);
132 }
133 };
134 assertFalse(task.isDisposed());
135
136 task.setFuture(ft);
137
138 task.runner = Thread.currentThread();
139
140 assertFalse(task.isDisposed());
141
142 task.dispose();
143
144 assertTrue(task.isDisposed());
145
146 assertFalse(interrupted[0]);
147 }
148
149 @Test
150 public void finished() {
151 AbstractDirectTask task = new AbstractDirectTask(Functions.EMPTY_RUNNABLE, true) {
152 private static final long serialVersionUID = 208585707945686116L;
153 };
154 final Boolean[] interrupted = { null };
155 FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null) {
156 @Override
157 public boolean cancel(boolean mayInterruptIfRunning) {
158 interrupted[0] = mayInterruptIfRunning;
159 return super.cancel(mayInterruptIfRunning);
160 }
161 };
162
163 task.set(AbstractDirectTask.FINISHED);
164
165 task.setFuture(ft);
166
167 assertTrue(task.isDisposed());
168
169 assertNull(interrupted[0]);
170
171 task.dispose();
172
173 assertTrue(task.isDisposed());
174
175 assertNull(interrupted[0]);
176 }
177
178 @Test
179 public void finishedCancel() {
180 AbstractDirectTask task = new AbstractDirectTask(Functions.EMPTY_RUNNABLE, true) {
181 private static final long serialVersionUID = 208585707945686116L;
182 };
183 final Boolean[] interrupted = { null };
184 FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null) {
185 @Override
186 public boolean cancel(boolean mayInterruptIfRunning) {
187 interrupted[0] = mayInterruptIfRunning;
188 return super.cancel(mayInterruptIfRunning);
189 }
190 };
191
192 task.set(AbstractDirectTask.FINISHED);
193
194 assertTrue(task.isDisposed());
195
196 task.dispose();
197
198 assertTrue(task.isDisposed());
199
200 task.setFuture(ft);
201
202 assertTrue(task.isDisposed());
203
204 assertNull(interrupted[0]);
205
206 assertTrue(task.isDisposed());
207
208 assertNull(interrupted[0]);
209 }
210
211 @Test
212 public void disposeSetFutureRace() {
213 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
214 final AbstractDirectTask task = new AbstractDirectTask(Functions.EMPTY_RUNNABLE, true) {
215 private static final long serialVersionUID = 208585707945686116L;
216 };
217
218 final Boolean[] interrupted = { null };
219 final FutureTask<Void> ft = new FutureTask<Void>(Functions.EMPTY_RUNNABLE, null) {
220 @Override
221 public boolean cancel(boolean mayInterruptIfRunning) {
222 interrupted[0] = mayInterruptIfRunning;
223 return super.cancel(mayInterruptIfRunning);
224 }
225 };
226
227 Runnable r1 = new Runnable() {
228 @Override
229 public void run() {
230 task.dispose();
231 }
232 };
233
234 Runnable r2 = new Runnable() {
235 @Override
236 public void run() {
237 task.setFuture(ft);
238 }
239 };
240
241 TestHelper.race(r1, r2);
242 }
243 }
244
245 static class TestDirectTask extends AbstractDirectTask {
246 private static final long serialVersionUID = 587679821055711738L;
247
248 TestDirectTask() {
249 super(Functions.EMPTY_RUNNABLE, true);
250 }
251 }
252
253 @Test
254 public void toStringStates() {
255 TestDirectTask task = new TestDirectTask();
256
257 assertEquals("TestDirectTask[Waiting]", task.toString());
258
259 task.runner = Thread.currentThread();
260
261 assertEquals("TestDirectTask[Running on " + Thread.currentThread() + "]", task.toString());
262
263 task.dispose();
264
265 assertEquals("TestDirectTask[Disposed]", task.toString());
266
267 task.set(AbstractDirectTask.FINISHED);
268
269 assertEquals("TestDirectTask[Finished]", task.toString());
270 }
271 }