Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/mixed/MaybeFlatMapPublisherTest.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.mixed;
15
16 import static org.junit.Assert.*;
17
18 import org.junit.Test;
19 import org.reactivestreams.Publisher;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.exceptions.TestException;
23 import io.reactivex.rxjava3.functions.Function;
24 import io.reactivex.rxjava3.internal.functions.Functions;
25 import io.reactivex.rxjava3.processors.PublishProcessor;
26 import io.reactivex.rxjava3.subjects.MaybeSubject;
27 import io.reactivex.rxjava3.subscribers.TestSubscriber;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class MaybeFlatMapPublisherTest extends RxJavaTest {
31
32 @Test
33 public void cancelMain() {
34 MaybeSubject<Integer> ms = MaybeSubject.create();
35 PublishProcessor<Integer> pp = PublishProcessor.create();
36
37 TestSubscriber<Integer> ts = ms.flatMapPublisher(Functions.justFunction(pp))
38 .test();
39
40 assertTrue(ms.hasObservers());
41 assertFalse(pp.hasSubscribers());
42
43 ts.cancel();
44
45 assertFalse(ms.hasObservers());
46 assertFalse(pp.hasSubscribers());
47 }
48
49 @Test
50 public void cancelOther() {
51 MaybeSubject<Integer> ms = MaybeSubject.create();
52 PublishProcessor<Integer> pp = PublishProcessor.create();
53
54 TestSubscriber<Integer> ts = ms.flatMapPublisher(Functions.justFunction(pp))
55 .test();
56
57 assertTrue(ms.hasObservers());
58 assertFalse(pp.hasSubscribers());
59
60 ms.onSuccess(1);
61
62 assertFalse(ms.hasObservers());
63 assertTrue(pp.hasSubscribers());
64
65 ts.cancel();
66
67 assertFalse(ms.hasObservers());
68 assertFalse(pp.hasSubscribers());
69 }
70
71 @Test
72 public void mapperCrash() {
73 Maybe.just(1).flatMapPublisher(new Function<Integer, Publisher<? extends Object>>() {
74 @Override
75 public Publisher<? extends Object> apply(Integer v) throws Exception {
76 throw new TestException();
77 }
78 })
79 .test()
80 .assertFailure(TestException.class);
81 }
82
83 @Test
84 public void doubleOnSubscribe() {
85 TestHelper.checkDoubleOnSubscribeMaybeToFlowable(new Function<Maybe<Object>, Publisher<Object>>() {
86 @Override
87 public Publisher<Object> apply(Maybe<Object> m) throws Exception {
88 return m.flatMapPublisher(Functions.justFunction(Flowable.never()));
89 }
90 });
91 }
92 }