Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableDisposeOnTest.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.completable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.Action;
26 import io.reactivex.rxjava3.observers.TestObserver;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.schedulers.*;
29 import io.reactivex.rxjava3.subjects.PublishSubject;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class CompletableDisposeOnTest extends RxJavaTest {
33
34 @Test
35 public void cancelDelayed() {
36 TestScheduler scheduler = new TestScheduler();
37
38 PublishSubject<Integer> ps = PublishSubject.create();
39
40 ps.ignoreElements()
41 .unsubscribeOn(scheduler)
42 .test()
43 .dispose();
44
45 assertTrue(ps.hasObservers());
46
47 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
48
49 assertFalse(ps.hasObservers());
50 }
51
52 @Test
53 public void dispose() {
54 TestHelper.checkDisposed(PublishSubject.create().ignoreElements().unsubscribeOn(new TestScheduler()));
55 }
56
57 @Test
58 public void completeAfterCancel() {
59 TestScheduler scheduler = new TestScheduler();
60
61 PublishSubject<Integer> ps = PublishSubject.create();
62
63 TestObserver<Void> to = ps.ignoreElements()
64 .unsubscribeOn(scheduler)
65 .test();
66
67 to.dispose();
68
69 ps.onComplete();
70
71 to.assertEmpty();
72 }
73
74 @Test
75 public void errorAfterCancel() {
76 List<Throwable> errors = TestHelper.trackPluginErrors();
77 try {
78 TestScheduler scheduler = new TestScheduler();
79
80 PublishSubject<Integer> ps = PublishSubject.create();
81
82 TestObserver<Void> to = ps.ignoreElements()
83 .unsubscribeOn(scheduler)
84 .test();
85
86 to.dispose();
87
88 ps.onError(new TestException());
89
90 to.assertEmpty();
91
92 TestHelper.assertUndeliverable(errors, 0, TestException.class);
93 } finally {
94 RxJavaPlugins.reset();
95 }
96 }
97
98 @Test
99 public void normal() {
100 TestScheduler scheduler = new TestScheduler();
101
102 final int[] call = { 0 };
103
104 Completable.complete()
105 .doOnDispose(new Action() {
106 @Override
107 public void run() throws Exception {
108 call[0]++;
109 }
110 })
111 .unsubscribeOn(scheduler)
112 .test()
113 .assertResult();
114
115 scheduler.triggerActions();
116
117 assertEquals(0, call[0]);
118 }
119
120 @Test
121 public void error() {
122 TestScheduler scheduler = new TestScheduler();
123
124 final int[] call = { 0 };
125
126 Completable.error(new TestException())
127 .doOnDispose(new Action() {
128 @Override
129 public void run() throws Exception {
130 call[0]++;
131 }
132 })
133 .unsubscribeOn(scheduler)
134 .test()
135 .assertFailure(TestException.class);
136
137 scheduler.triggerActions();
138
139 assertEquals(0, call[0]);
140 }
141
142 @Test
143 public void doubleOnSubscribe() {
144 TestHelper.checkDoubleOnSubscribeCompletable(c -> c.unsubscribeOn(Schedulers.computation()));
145 }
146 }