Debian Codegraph @

src/test/java/io/reactivex/rxjava3/schedulers/SchedulerLifecycleTest.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.schedulers;
15
16 import static org.junit.Assert.fail;
17
18 import java.util.*;
19 import java.util.concurrent.*;
20
21 import org.junit.*;
22
23 import io.reactivex.rxjava3.core.RxJavaTest;
24 import io.reactivex.rxjava3.core.Scheduler.Worker;
25 import io.reactivex.rxjava3.disposables.CompositeDisposable;
26
27 public class SchedulerLifecycleTest extends RxJavaTest {
28 @Test
29 public void shutdown() throws InterruptedException {
30 tryOutSchedulers();
31
32 System.out.println("testShutdown >> Giving time threads to spin-up");
33 Thread.sleep(500);
34
35 Set<Thread> rxThreads = new HashSet<>();
36 for (Thread t : Thread.getAllStackTraces().keySet()) {
37 if (t.getName().startsWith("Rx")) {
38 rxThreads.add(t);
39 }
40 }
41 Schedulers.shutdown();
42 System.out.println("testShutdown >> Giving time to threads to stop");
43 Thread.sleep(500);
44
45 StringBuilder b = new StringBuilder();
46 for (Thread t : rxThreads) {
47 if (t.isAlive()) {
48 b.append("Thread " + t + " failed to shutdown\r\n");
49 for (StackTraceElement ste : t.getStackTrace()) {
50 b.append(" ").append(ste).append("\r\n");
51 }
52 }
53 }
54 if (b.length() > 0) {
55 System.out.print(b);
56 System.out.println("testShutdown >> Restarting schedulers...");
57 Schedulers.start(); // restart them anyways
58 fail("Rx Threads were still alive:\r\n" + b);
59 }
60
61 System.out.println("testShutdown >> Restarting schedulers...");
62 Schedulers.start();
63
64 tryOutSchedulers();
65 }
66
67 private void tryOutSchedulers() throws InterruptedException {
68 final CountDownLatch cdl = new CountDownLatch(4);
69
70 final Runnable countAction = new Runnable() {
71 @Override
72 public void run() {
73 cdl.countDown();
74 }
75 };
76
77 CompositeDisposable cd = new CompositeDisposable();
78
79 try {
80 Worker w1 = Schedulers.computation().createWorker();
81 cd.add(w1);
82 w1.schedule(countAction);
83
84 Worker w2 = Schedulers.io().createWorker();
85 cd.add(w2);
86 w2.schedule(countAction);
87
88 Worker w3 = Schedulers.newThread().createWorker();
89 cd.add(w3);
90 w3.schedule(countAction);
91
92 Worker w4 = Schedulers.single().createWorker();
93 cd.add(w4);
94 w4.schedule(countAction);
95
96 if (!cdl.await(3, TimeUnit.SECONDS)) {
97 fail("countAction was not run by every worker");
98 }
99 } finally {
100 cd.dispose();
101 }
102 }
103
104 @Test
105 public void startIdempotence() throws InterruptedException {
106 tryOutSchedulers();
107
108 System.out.println("testStartIdempotence >> giving some time");
109 Thread.sleep(500);
110
111 Set<Thread> rxThreadsBefore = new HashSet<>();
112 for (Thread t : Thread.getAllStackTraces().keySet()) {
113 if (t.getName().startsWith("Rx")) {
114 rxThreadsBefore.add(t);
115 System.out.println("testStartIdempotence >> " + t);
116 }
117 }
118 System.out.println("testStartIdempotence >> trying to start again");
119 Schedulers.start();
120 System.out.println("testStartIdempotence >> giving some time again");
121 Thread.sleep(500);
122
123 Set<Thread> rxThreadsAfter = new HashSet<>();
124 for (Thread t : Thread.getAllStackTraces().keySet()) {
125 if (t.getName().startsWith("Rx")) {
126 rxThreadsAfter.add(t);
127 System.out.println("testStartIdempotence >>>> " + t);
128 }
129 }
130
131 // cached threads may get dropped between the two checks
132 rxThreadsAfter.removeAll(rxThreadsBefore);
133
134 Assert.assertTrue("Some new threads appeared: " + rxThreadsAfter, rxThreadsAfter.isEmpty());
135 }
136 }