-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
CachedThreadSchedulerTest.java
123 lines (94 loc) · 3.92 KB
/
CachedThreadSchedulerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.schedulers;
import java.lang.management.*;
import java.util.concurrent.*;
import junit.framework.Assert;
import org.junit.Test;
import rx.Observable;
import rx.Scheduler;
import rx.functions.*;
import rx.internal.schedulers.NewThreadWorker;
import static org.junit.Assert.assertTrue;
public class CachedThreadSchedulerTest extends AbstractSchedulerConcurrencyTests {
@Override
protected Scheduler getScheduler() {
return Schedulers.io();
}
/**
* IO scheduler defaults to using CachedThreadScheduler
*/
@Test
public final void testIOScheduler() {
Observable<Integer> o1 = Observable.just(1, 2, 3, 4, 5);
Observable<Integer> o2 = Observable.just(6, 7, 8, 9, 10);
Observable<String> o = Observable.merge(o1, o2).map(new Func1<Integer, String>() {
@Override
public String call(Integer t) {
assertTrue(Thread.currentThread().getName().startsWith("RxCachedThreadScheduler"));
return "Value_" + t + "_Thread_" + Thread.currentThread().getName();
}
});
o.subscribeOn(Schedulers.io()).toBlocking().forEach(new Action1<String>() {
@Override
public void call(String t) {
System.out.println("t: " + t);
}
});
}
@Test
public final void testUnhandledErrorIsDeliveredToThreadHandler() throws InterruptedException {
SchedulerTests.testUnhandledErrorIsDeliveredToThreadHandler(getScheduler());
}
@Test
public final void testHandledErrorIsNotDeliveredToThreadHandler() throws InterruptedException {
SchedulerTests.testHandledErrorIsNotDeliveredToThreadHandler(getScheduler());
}
@Test(timeout = 30000)
public void testCancelledTaskRetention() throws InterruptedException {
System.out.println("Wait before GC");
Thread.sleep(1000);
System.out.println("GC");
System.gc();
Thread.sleep(1000);
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memHeap = memoryMXBean.getHeapMemoryUsage();
long initial = memHeap.getUsed();
System.out.printf("Starting: %.3f MB%n", initial / 1024.0 / 1024.0);
Scheduler.Worker w = Schedulers.io().createWorker();
for (int i = 0; i < 750000; i++) {
if (i % 50000 == 0) {
System.out.println(" -> still scheduling: " + i);
}
w.schedule(Actions.empty(), 1, TimeUnit.DAYS);
}
memHeap = memoryMXBean.getHeapMemoryUsage();
long after = memHeap.getUsed();
System.out.printf("Peak: %.3f MB%n", after / 1024.0 / 1024.0);
w.unsubscribe();
System.out.println("Wait before second GC");
Thread.sleep(NewThreadWorker.PURGE_FREQUENCY + 2000);
System.out.println("Second GC");
System.gc();
Thread.sleep(1000);
memHeap = memoryMXBean.getHeapMemoryUsage();
long finish = memHeap.getUsed();
System.out.printf("After: %.3f MB%n", finish / 1024.0 / 1024.0);
if (finish > initial * 5) {
Assert.fail(String.format("Tasks retained: %.3f -> %.3f -> %.3f", initial / 1024 / 1024.0, after / 1024 / 1024.0, finish / 1024 / 1024d));
}
}
}