Repository navigation
Expand file tree
/
Copy pathFlowableRepeatTest.java
More file actions
246 lines (193 loc) · 7.69 KB
/
Copy pathFlowableRepeatTest.java
File metadata and controls
246 lines (193 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.internal.operators.flowable;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.exceptions.TestException;
import io.reactivex.functions.Function;
import io.reactivex.internal.subscriptions.BooleanSubscription;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.TestSubscriber;
public class FlowableRepeatTest {
@Test(timeout = 2000)
public void testRepetition() {
int NUM = 10;
final AtomicInteger count = new AtomicInteger();
int value = Flowable.unsafeCreate(new Publisher<Integer>() {
@Override
public void subscribe(final Subscriber<? super Integer> o) {
o.onNext(count.incrementAndGet());
o.onComplete();
}
}).repeat().subscribeOn(Schedulers.computation())
.take(NUM).blockingLast();
assertEquals(NUM, value);
}
@Test(timeout = 2000)
public void testRepeatTake() {
Flowable<Integer> xs = Flowable.just(1, 2);
Object[] ys = xs.repeat().subscribeOn(Schedulers.newThread()).take(4).toList().blockingLast().toArray();
assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys);
}
@Test(timeout = 20000)
public void testNoStackOverFlow() {
Flowable.just(1).repeat().subscribeOn(Schedulers.newThread()).take(100000).blockingLast();
}
@Test
public void testRepeatTakeWithSubscribeOn() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Flowable<Integer> oi = Flowable.unsafeCreate(new Publisher<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer> sub) {
sub.onSubscribe(new BooleanSubscription());
counter.incrementAndGet();
sub.onNext(1);
sub.onNext(2);
sub.onComplete();
}
}).subscribeOn(Schedulers.newThread());
Object[] ys = oi.repeat().subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer t1) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
return t1;
}
}).take(4).toList().blockingLast().toArray();
assertEquals(2, counter.get());
assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys);
}
@Test(timeout = 2000)
public void testRepeatAndTake() {
Subscriber<Object> o = TestHelper.mockSubscriber();
Flowable.just(1).repeat().take(10).subscribe(o);
verify(o, times(10)).onNext(1);
verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
@Test(timeout = 2000)
public void testRepeatLimited() {
Subscriber<Object> o = TestHelper.mockSubscriber();
Flowable.just(1).repeat(10).subscribe(o);
verify(o, times(10)).onNext(1);
verify(o).onComplete();
verify(o, never()).onError(any(Throwable.class));
}
@Test(timeout = 2000)
public void testRepeatError() {
Subscriber<Object> o = TestHelper.mockSubscriber();
Flowable.error(new TestException()).repeat(10).subscribe(o);
verify(o).onError(any(TestException.class));
verify(o, never()).onNext(any());
verify(o, never()).onComplete();
}
@Test(timeout = 2000)
public void testRepeatZero() {
Subscriber<Object> o = TestHelper.mockSubscriber();
Flowable.just(1).repeat(0).subscribe(o);
verify(o).onComplete();
verify(o, never()).onNext(any());
verify(o, never()).onError(any(Throwable.class));
}
@Test(timeout = 2000)
public void testRepeatOne() {
Subscriber<Object> o = TestHelper.mockSubscriber();
Flowable.just(1).repeat(1).subscribe(o);
verify(o).onComplete();
verify(o, times(1)).onNext(any());
verify(o, never()).onError(any(Throwable.class));
}
/** Issue #2587. */
@Test
public void testRepeatAndDistinctUnbounded() {
Flowable<Integer> src = Flowable.fromIterable(Arrays.asList(1, 2, 3, 4, 5))
.take(3)
.repeat(3)
.distinct();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
src.subscribe(ts);
ts.assertNoErrors();
ts.assertTerminated();
ts.assertValues(1, 2, 3);
}
/** Issue #2844: wrong target of request. */
@Test(timeout = 3000)
public void testRepeatRetarget() {
final List<Integer> concatBase = new ArrayList<Integer>();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
Flowable.just(1, 2)
.repeat(5)
.concatMap(new Function<Integer, Flowable<Integer>>() {
@Override
public Flowable<Integer> apply(Integer x) {
System.out.println("testRepeatRetarget -> " + x);
concatBase.add(x);
return Flowable.<Integer>empty()
.delay(200, TimeUnit.MILLISECONDS);
}
})
.subscribe(ts);
ts.awaitTerminalEvent();
ts.assertNoErrors();
ts.assertNoValues();
assertEquals(Arrays.asList(1, 2, 1, 2, 1, 2, 1, 2, 1, 2), concatBase);
}
@Test
public void repeatScheduled() {
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.just(1).subscribeOn(Schedulers.computation()).repeat(5).subscribe(ts);
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
ts.assertValues(1, 1, 1, 1, 1);
ts.assertNoErrors();
ts.assertComplete();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void repeatWhenDefaultScheduler() {
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.just(1).repeatWhen((Function)new Function<Flowable, Flowable>() {
@Override
public Flowable apply(Flowable o) {
return o.take(2);
}
}).subscribe(ts);
ts.assertValues(1, 1);
ts.assertNoErrors();
ts.assertComplete();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void repeatWhenTrampolineScheduler() {
TestSubscriber<Integer> ts = TestSubscriber.create();
Flowable.just(1).subscribeOn(Schedulers.trampoline())
.repeatWhen((Function)new Function<Flowable, Flowable>() {
@Override
public Flowable apply(Flowable o) {
return o.take(2);
}
}).subscribe(ts);
ts.assertValues(1, 1);
ts.assertNoErrors();
ts.assertComplete();
}
}