Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/subscribers/SubscriberResourceWrapperTest.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.subscribers;
15
16 import static org.junit.Assert.*;
17
18 import org.junit.Test;
19 import org.reactivestreams.Subscriber;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
26 import io.reactivex.rxjava3.subscribers.TestSubscriber;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class SubscriberResourceWrapperTest extends RxJavaTest {
30
31 TestSubscriber<Integer> ts = new TestSubscriber<>();
32
33 SubscriberResourceWrapper<Integer> s = new SubscriberResourceWrapper<>(ts);
34
35 @Test
36 public void cancel() {
37 BooleanSubscription bs = new BooleanSubscription();
38 Disposable d = Disposable.empty();
39
40 s.setResource(d);
41
42 s.onSubscribe(bs);
43
44 assertFalse(d.isDisposed());
45 assertFalse(s.isDisposed());
46
47 ts.cancel();
48
49 assertTrue(bs.isCancelled());
50 assertTrue(d.isDisposed());
51 assertTrue(s.isDisposed());
52 }
53
54 @Test
55 public void error() {
56 BooleanSubscription bs = new BooleanSubscription();
57 Disposable d = Disposable.empty();
58
59 s.setResource(d);
60
61 s.onSubscribe(bs);
62
63 s.onError(new TestException());
64
65 assertTrue(d.isDisposed());
66 assertFalse(bs.isCancelled());
67
68 ts.assertFailure(TestException.class);
69 }
70
71 @Test
72 public void complete() {
73 BooleanSubscription bs = new BooleanSubscription();
74 Disposable d = Disposable.empty();
75
76 s.setResource(d);
77
78 s.onSubscribe(bs);
79
80 s.onComplete();
81
82 assertTrue(d.isDisposed());
83 assertFalse(bs.isCancelled());
84
85 ts.assertResult();
86 }
87
88 @Test
89 public void doubleOnSubscribe() {
90 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() {
91 @Override
92 public Flowable<Object> apply(Flowable<Object> f) throws Exception {
93 return f.lift(new FlowableOperator<Object, Object>() {
94 @Override
95 public Subscriber<? super Object> apply(
96 Subscriber<? super Object> s) throws Exception {
97 return new SubscriberResourceWrapper<>(s);
98 }
99 });
100 }
101 });
102 }
103
104 @Test
105 public void badRequest() {
106 TestHelper.assertBadRequestReported(Flowable.never().lift(new FlowableOperator<Object, Object>() {
107 @Override
108 public Subscriber<? super Object> apply(
109 Subscriber<? super Object> s) throws Exception {
110 return new SubscriberResourceWrapper<>(s);
111 }
112 }));
113 }
114 }