Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleUnsubscribeOnTest.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.operators.single;
15
16 import static org.junit.Assert.*;
17
18 import java.util.concurrent.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.Disposable;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.processors.PublishProcessor;
27 import io.reactivex.rxjava3.schedulers.Schedulers;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class SingleUnsubscribeOnTest extends RxJavaTest {
31
32 @Test
33 public void normal() throws Exception {
34 PublishProcessor<Integer> pp = PublishProcessor.create();
35
36 final String[] name = { null };
37
38 final CountDownLatch cdl = new CountDownLatch(1);
39
40 pp.doOnCancel(new Action() {
41 @Override
42 public void run() throws Exception {
43 name[0] = Thread.currentThread().getName();
44 cdl.countDown();
45 }
46 })
47 .single(-99)
48 .unsubscribeOn(Schedulers.single())
49 .test(true)
50 ;
51
52 assertTrue(cdl.await(5, TimeUnit.SECONDS));
53
54 int times = 10;
55
56 while (times-- > 0 && pp.hasSubscribers()) {
57 Thread.sleep(100);
58 }
59
60 assertFalse(pp.hasSubscribers());
61
62 assertNotEquals(Thread.currentThread().getName(), name[0]);
63 }
64
65 @Test
66 public void just() {
67 Single.just(1)
68 .unsubscribeOn(Schedulers.single())
69 .test()
70 .assertResult(1);
71 }
72
73 @Test
74 public void error() {
75 Single.<Integer>error(new TestException())
76 .unsubscribeOn(Schedulers.single())
77 .test()
78 .assertFailure(TestException.class);
79 }
80
81 @Test
82 public void dispose() {
83 TestHelper.checkDisposed(Single.just(1)
84 .unsubscribeOn(Schedulers.single()));
85 }
86
87 @Test
88 public void doubleOnSubscribe() {
89 TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Object>, SingleSource<Object>>() {
90 @Override
91 public SingleSource<Object> apply(Single<Object> v) throws Exception {
92 return v.unsubscribeOn(Schedulers.single());
93 }
94 });
95 }
96
97 @Test
98 public void disposeRace() {
99 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
100 PublishProcessor<Integer> pp = PublishProcessor.create();
101
102 final Disposable[] ds = { null };
103 pp.single(-99).unsubscribeOn(Schedulers.computation())
104 .subscribe(new SingleObserver<Integer>() {
105 @Override
106 public void onSubscribe(Disposable d) {
107 ds[0] = d;
108 }
109
110 @Override
111 public void onSuccess(Integer value) {
112
113 }
114
115 @Override
116 public void onError(Throwable e) {
117
118 }
119 });
120
121 Runnable r = new Runnable() {
122 @Override
123 public void run() {
124 ds[0].dispose();
125 }
126 };
127
128 TestHelper.race(r, r);
129 }
130 }
131 }