Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDelaySubscriptionTest.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.List;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.Test;
22 import org.reactivestreams.Subscriber;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.functions.Function;
27 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
28 import io.reactivex.rxjava3.observers.TestObserver;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.PublishProcessor;
31 import io.reactivex.rxjava3.schedulers.TestScheduler;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class MaybeDelaySubscriptionTest extends RxJavaTest {
35
36 @Test
37 public void normal() {
38 PublishProcessor<Object> pp = PublishProcessor.create();
39
40 TestObserver<Integer> to = Maybe.just(1).delaySubscription(pp)
41 .test();
42
43 assertTrue(pp.hasSubscribers());
44
45 to.assertEmpty();
46
47 pp.onNext("one");
48
49 assertFalse(pp.hasSubscribers());
50
51 to.assertResult(1);
52 }
53
54 @Test
55 public void timed() {
56 Maybe.just(1).delaySubscription(100, TimeUnit.MILLISECONDS)
57 .test()
58 .awaitDone(5, TimeUnit.SECONDS)
59 .assertResult(1);
60 }
61
62 @Test
63 public void timedEmpty() {
64 Maybe.<Integer>empty().delaySubscription(100, TimeUnit.MILLISECONDS)
65 .test()
66 .awaitDone(5, TimeUnit.SECONDS)
67 .assertResult();
68 }
69
70 @Test
71 public void timedTestScheduler() {
72 TestScheduler scheduler = new TestScheduler();
73
74 TestObserver<Integer> to = Maybe.just(1)
75 .delaySubscription(100, TimeUnit.MILLISECONDS, scheduler)
76 .test();
77
78 to.assertEmpty();
79
80 scheduler.advanceTimeBy(99, TimeUnit.MILLISECONDS);
81
82 to.assertEmpty();
83
84 scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
85
86 to.assertResult(1);
87 }
88
89 @Test
90 public void otherError() {
91 Maybe.just(1).delaySubscription(Flowable.error(new TestException()))
92 .test()
93 .assertFailure(TestException.class);
94 }
95
96 @Test
97 public void mainError() {
98 Maybe.error(new TestException())
99 .delaySubscription(Flowable.empty())
100 .test()
101 .assertFailure(TestException.class);
102 }
103
104 @Test
105 public void withPublisherDispose() {
106 TestHelper.checkDisposed(Maybe.just(1).delaySubscription(Flowable.never()));
107 }
108
109 @Test
110 public void withPublisherDoubleOnSubscribe() {
111 TestHelper.checkDoubleOnSubscribeMaybe(new Function<Maybe<Object>, MaybeSource<Object>>() {
112 @Override
113 public MaybeSource<Object> apply(Maybe<Object> m) throws Exception {
114 return m.delaySubscription(Flowable.just(1));
115 }
116 });
117 }
118
119 @Test
120 public void withPublisherCallAfterTerminalEvent() {
121 List<Throwable> errors = TestHelper.trackPluginErrors();
122
123 try {
124 Flowable<Integer> f = new Flowable<Integer>() {
125 @Override
126 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
127 subscriber.onSubscribe(new BooleanSubscription());
128 subscriber.onNext(1);
129 subscriber.onError(new TestException());
130 subscriber.onComplete();
131 subscriber.onNext(2);
132 }
133 };
134
135 Maybe.just(1).delaySubscription(f)
136 .test()
137 .assertResult(1);
138
139 TestHelper.assertUndeliverable(errors, 0, TestException.class);
140 } finally {
141 RxJavaPlugins.reset();
142 }
143 }
144
145 @Test
146 public void doubleOnSubscribePublisher() {
147 TestHelper.checkDoubleOnSubscribeFlowableToMaybe(f -> Maybe.just(1).delaySubscription(f));
148 }
149 }