Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTakeTest2.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.flowable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21 import org.reactivestreams.Subscriber;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.processors.PublishProcessor;
29 import io.reactivex.rxjava3.subscribers.TestSubscriber;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 // moved tests from FlowableLimitTest to here (limit removed as operator)
33 public class FlowableTakeTest2 extends RxJavaTest implements LongConsumer, Action {
34
35 final List<Long> requests = new ArrayList<>();
36
37 static final Long CANCELLED = -100L;
38
39 @Override
40 public void accept(long t) throws Exception {
41 requests.add(t);
42 }
43
44 @Override
45 public void run() throws Exception {
46 requests.add(CANCELLED);
47 }
48
49 @Test
50 public void shorterSequence() {
51 Flowable.range(1, 5)
52 .doOnRequest(this)
53 .take(6)
54 .test()
55 .assertResult(1, 2, 3, 4, 5);
56
57 assertEquals(6, requests.get(0).intValue());
58 }
59
60 @Test
61 public void exactSequence() {
62 Flowable.range(1, 5)
63 .doOnRequest(this)
64 .doOnCancel(this)
65 .take(5)
66 .test()
67 .assertResult(1, 2, 3, 4, 5);
68
69 assertEquals(2, requests.size());
70 assertEquals(5, requests.get(0).intValue());
71 assertEquals(CANCELLED, requests.get(1));
72 }
73
74 @Test
75 public void longerSequence() {
76 Flowable.range(1, 6)
77 .doOnRequest(this)
78 .take(5)
79 .test()
80 .assertResult(1, 2, 3, 4, 5);
81
82 assertEquals(5, requests.get(0).intValue());
83 }
84
85 @Test
86 public void error() {
87 Flowable.error(new TestException())
88 .take(5)
89 .test()
90 .assertFailure(TestException.class);
91 }
92
93 @Test
94 public void takeZero() {
95 Flowable.range(1, 5)
96 .doOnCancel(this)
97 .doOnRequest(this)
98 .take(0)
99 .test()
100 .assertResult();
101
102 assertEquals(1, requests.size());
103 assertEquals(CANCELLED, requests.get(0));
104 }
105
106 @Test
107 public void takeStep() {
108 TestSubscriber<Integer> ts = Flowable.range(1, 6)
109 .doOnRequest(this)
110 .take(5)
111 .test(0L);
112
113 assertEquals(0, requests.size());
114
115 ts.request(1);
116 ts.assertValue(1);
117
118 ts.request(2);
119 ts.assertValues(1, 2, 3);
120
121 ts.request(3);
122 ts.assertResult(1, 2, 3, 4, 5);
123
124 assertEquals(Arrays.asList(1L, 2L, 2L), requests);
125 }
126
127 @Test
128 public void takeThenTake() {
129 Flowable.range(1, 5)
130 .doOnCancel(this)
131 .doOnRequest(this)
132 .take(6)
133 .take(5)
134 .test()
135 .assertResult(1, 2, 3, 4, 5);
136
137 assertEquals(Arrays.asList(5L, CANCELLED), requests);
138 }
139
140 @Test
141 public void noOverrequest() {
142 PublishProcessor<Integer> pp = PublishProcessor.create();
143
144 TestSubscriber<Integer> ts = pp
145 .doOnRequest(this)
146 .take(5)
147 .test(0L);
148
149 ts.request(5);
150 ts.request(10);
151
152 assertTrue(pp.offer(1));
153 pp.onComplete();
154
155 ts.assertResult(1);
156 }
157
158 @Test
159 public void cancelIgnored() {
160 List<Throwable> errors = TestHelper.trackPluginErrors();
161 try {
162 new Flowable<Integer>() {
163 @Override
164 protected void subscribeActual(Subscriber<? super Integer> s) {
165 BooleanSubscription bs = new BooleanSubscription();
166 s.onSubscribe(bs);
167
168 assertTrue(bs.isCancelled());
169
170 s.onNext(1);
171 s.onComplete();
172 s.onError(new TestException());
173
174 s.onSubscribe(null);
175 }
176 }
177 .take(0)
178 .test()
179 .assertResult();
180
181 TestHelper.assertUndeliverable(errors, 0, TestException.class);
182 TestHelper.assertError(errors, 1, NullPointerException.class);
183 } finally {
184 RxJavaPlugins.reset();
185 }
186 }
187
188 @Test
189 public void badRequest() {
190 TestHelper.assertBadRequestReported(Flowable.range(1, 5).take(3));
191 }
192
193 @Test
194 public void requestRace() {
195 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
196 final TestSubscriber<Integer> ts = Flowable.range(1, 10)
197 .take(5)
198 .test(0L);
199
200 Runnable r = new Runnable() {
201 @Override
202 public void run() {
203 ts.request(3);
204 }
205 };
206
207 TestHelper.race(r, r);
208
209 ts.assertResult(1, 2, 3, 4, 5);
210 }
211 }
212
213 @Test
214 public void errorAfterLimitReached() {
215 List<Throwable> errors = TestHelper.trackPluginErrors();
216 try {
217 Flowable.error(new TestException())
218 .take(0)
219 .test()
220 .assertResult();
221
222 TestHelper.assertUndeliverable(errors, 0, TestException.class);
223 } finally {
224 RxJavaPlugins.reset();
225 }
226 }
227 }