Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableOnBackpressureLatestTest.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 java.util.Random;
17 import java.util.concurrent.TimeUnit;
18
19 import org.junit.*;
20 import org.reactivestreams.Publisher;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.processors.PublishProcessor;
26 import io.reactivex.rxjava3.schedulers.Schedulers;
27 import io.reactivex.rxjava3.subscribers.TestSubscriber;
28 import io.reactivex.rxjava3.testsupport.*;
29
30 public class FlowableOnBackpressureLatestTest extends RxJavaTest {
31 @Test
32 public void simple() {
33 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
34
35 Flowable.range(1, 5).onBackpressureLatest().subscribe(ts);
36
37 ts.assertNoErrors();
38 ts.assertTerminated();
39 ts.assertValues(1, 2, 3, 4, 5);
40 }
41
42 @Test
43 public void simpleError() {
44 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
45
46 Flowable.range(1, 5).concatWith(Flowable.<Integer>error(new TestException()))
47 .onBackpressureLatest().subscribe(ts);
48
49 ts.assertTerminated();
50 ts.assertError(TestException.class);
51 ts.assertValues(1, 2, 3, 4, 5);
52 }
53
54 @Test
55 public void simpleBackpressure() {
56 TestSubscriber<Integer> ts = new TestSubscriber<>(2L);
57
58 Flowable.range(1, 5).onBackpressureLatest().subscribe(ts);
59
60 ts.assertNoErrors();
61 ts.assertValues(1, 2);
62 ts.assertNotComplete();
63 }
64
65 @Test
66 public void synchronousDrop() {
67 PublishProcessor<Integer> source = PublishProcessor.create();
68 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(0L);
69
70 source.onBackpressureLatest().subscribe(ts);
71
72 ts.assertNoValues();
73
74 source.onNext(1);
75 ts.request(2);
76
77 ts.assertValue(1);
78
79 source.onNext(2);
80
81 ts.assertValues(1, 2);
82
83 source.onNext(3);
84 source.onNext(4);
85 source.onNext(5);
86 source.onNext(6);
87
88 ts.request(2);
89
90 ts.assertValues(1, 2, 6);
91
92 source.onNext(7);
93
94 ts.assertValues(1, 2, 6, 7);
95
96 source.onNext(8);
97 source.onNext(9);
98 source.onComplete();
99
100 ts.request(1);
101
102 ts.assertValues(1, 2, 6, 7, 9);
103 ts.assertNoErrors();
104 ts.assertTerminated();
105 }
106
107 @Test
108 public void asynchronousDrop() throws InterruptedException {
109 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>(1L) {
110 final Random rnd = new Random();
111 @Override
112 public void onNext(Integer t) {
113 super.onNext(t);
114 if (rnd.nextDouble() < 0.001) {
115 try {
116 Thread.sleep(1);
117 } catch (InterruptedException ex) {
118 ex.printStackTrace();
119 }
120 }
121 request(1);
122 }
123 };
124 int m = 100000;
125 Flowable.range(1, m)
126 .subscribeOn(Schedulers.computation())
127 .onBackpressureLatest()
128 .observeOn(Schedulers.io())
129 .subscribe(ts);
130
131 ts.awaitDone(2, TimeUnit.SECONDS);
132 ts.assertTerminated();
133 int n = ts.values().size();
134 System.out.println("testAsynchronousDrop -> " + n);
135 Assert.assertTrue("All events received?", n < m);
136 int previous = 0;
137 for (Integer current : ts.values()) {
138 Assert.assertTrue("The sequence must be increasing [current value=" + previous +
139 ", previous value=" + current + "]", previous <= current);
140 previous = current;
141 }
142 }
143
144 @Test
145 public void doubleOnSubscribe() {
146 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() {
147 @Override
148 public Publisher<Object> apply(Flowable<Object> f) throws Exception {
149 return f.onBackpressureLatest();
150 }
151 });
152 }
153
154 @Test
155 public void take() {
156 Flowable.just(1, 2)
157 .onBackpressureLatest()
158 .take(1)
159 .test()
160 .assertResult(1);
161 }
162
163 @Test
164 public void dispose() {
165 TestHelper.checkDisposed(Flowable.never().onBackpressureLatest());
166 }
167
168 @Test
169 public void badRequest() {
170 TestHelper.assertBadRequestReported(Flowable.never().onBackpressureLatest());
171 }
172 }