Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeUnsubscribeOnTest.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.maybe;
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 MaybeUnsubscribeOnTest 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 .singleElement()
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 Maybe.just(1)
68 .unsubscribeOn(Schedulers.single())
69 .test()
70 .assertResult(1);
71 }
72
73 @Test
74 public void error() {
75 Maybe.<Integer>error(new TestException())
76 .unsubscribeOn(Schedulers.single())
77 .test()
78 .assertFailure(TestException.class);
79 }
80
81 @Test
82 public void empty() {
83 Maybe.empty()
84 .unsubscribeOn(Schedulers.single())
85 .test()
86 .assertResult();
87 }
88
89 @Test
90 public void dispose() {
91 TestHelper.checkDisposed(Maybe.just(1)
92 .unsubscribeOn(Schedulers.single()));
93 }
94
95 @Test
96 public void doubleOnSubscribe() {
97 TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() {
98 @Override
99 public MaybeSource<Object> apply(Maybe<Object> v) throws Exception {
100 return v.unsubscribeOn(Schedulers.single());
101 }
102 });
103 }
104
105 @Test
106 public void disposeRace() {
107 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
108 PublishProcessor<Integer> pp = PublishProcessor.create();
109
110 final Disposable[] ds = { null };
111 pp.singleElement().unsubscribeOn(Schedulers.computation())
112 .subscribe(new MaybeObserver<Integer>() {
113 @Override
114 public void onSubscribe(Disposable d) {
115 ds[0] = d;
116 }
117
118 @Override
119 public void onSuccess(Integer value) {
120
121 }
122
123 @Override
124 public void onError(Throwable e) {
125
126 }
127
128 @Override
129 public void onComplete() {
130
131 }
132 });
133
134 Runnable r = new Runnable() {
135 @Override
136 public void run() {
137 ds[0].dispose();
138 }
139 };
140
141 TestHelper.race(r, r);
142 }
143 }
144 }