| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.mockito.InOrder; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.core.Observable; |
| 29 |
import io.reactivex.rxjava3.core.Observer; |
| 30 |
import io.reactivex.rxjava3.disposables.*; |
| 31 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 32 |
import io.reactivex.rxjava3.functions.*; |
| 33 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 34 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 35 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 36 |
import io.reactivex.rxjava3.subjects.PublishSubject; |
| 37 |
import io.reactivex.rxjava3.testsupport.*; |
| 38 |
|
| 39 |
public class ObservableTakeTest extends RxJavaTest { |
| 40 |
|
| 41 |
@Test |
| 42 |
public void take1() { |
| 43 |
Observable<String> w = Observable.fromIterable(Arrays.asList("one", "two", "three")); |
| 44 |
Observable<String> take = w.take(2); |
| 45 |
|
| 46 |
Observer<String> observer = TestHelper.mockObserver(); |
| 47 |
take.subscribe(observer); |
| 48 |
verify(observer, times(1)).onNext("one"); |
| 49 |
verify(observer, times(1)).onNext("two"); |
| 50 |
verify(observer, never()).onNext("three"); |
| 51 |
verify(observer, never()).onError(any(Throwable.class)); |
| 52 |
verify(observer, times(1)).onComplete(); |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void take2() { |
| 57 |
Observable<String> w = Observable.fromIterable(Arrays.asList("one", "two", "three")); |
| 58 |
Observable<String> take = w.take(1); |
| 59 |
|
| 60 |
Observer<String> observer = TestHelper.mockObserver(); |
| 61 |
take.subscribe(observer); |
| 62 |
verify(observer, times(1)).onNext("one"); |
| 63 |
verify(observer, never()).onNext("two"); |
| 64 |
verify(observer, never()).onNext("three"); |
| 65 |
verify(observer, never()).onError(any(Throwable.class)); |
| 66 |
verify(observer, times(1)).onComplete(); |
| 67 |
} |
| 68 |
|
| 69 |
@Test(expected = IllegalArgumentException.class) |
| 70 |
public void takeWithError() { |
| 71 |
Observable.fromIterable(Arrays.asList(1, 2, 3)).take(1) |
| 72 |
.map(new Function<Integer, Integer>() { |
| 73 |
@Override |
| 74 |
public Integer apply(Integer t1) { |
| 75 |
throw new IllegalArgumentException("some error"); |
| 76 |
} |
| 77 |
}).blockingSingle(); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void takeWithErrorHappeningInOnNext() { |
| 82 |
Observable<Integer> w = Observable.fromIterable(Arrays.asList(1, 2, 3)) |
| 83 |
.take(2).map(new Function<Integer, Integer>() { |
| 84 |
@Override |
| 85 |
public Integer apply(Integer t1) { |
| 86 |
throw new IllegalArgumentException("some error"); |
| 87 |
} |
| 88 |
}); |
| 89 |
|
| 90 |
Observer<Integer> observer = TestHelper.mockObserver(); |
| 91 |
w.subscribe(observer); |
| 92 |
InOrder inOrder = inOrder(observer); |
| 93 |
inOrder.verify(observer, times(1)).onError(any(IllegalArgumentException.class)); |
| 94 |
inOrder.verifyNoMoreInteractions(); |
| 95 |
} |
| 96 |
|
| 97 |
@Test |
| 98 |
public void takeWithErrorHappeningInTheLastOnNext() { |
| 99 |
Observable<Integer> w = Observable.fromIterable(Arrays.asList(1, 2, 3)).take(1).map(new Function<Integer, Integer>() { |
| 100 |
@Override |
| 101 |
public Integer apply(Integer t1) { |
| 102 |
throw new IllegalArgumentException("some error"); |
| 103 |
} |
| 104 |
}); |
| 105 |
|
| 106 |
Observer<Integer> observer = TestHelper.mockObserver(); |
| 107 |
w.subscribe(observer); |
| 108 |
InOrder inOrder = inOrder(observer); |
| 109 |
inOrder.verify(observer, times(1)).onError(any(IllegalArgumentException.class)); |
| 110 |
inOrder.verifyNoMoreInteractions(); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
@SuppressUndeliverable |
| 115 |
public void takeDoesntLeakErrors() { |
| 116 |
Observable<String> source = Observable.unsafeCreate(new ObservableSource<String>() { |
| 117 |
@Override |
| 118 |
public void subscribe(Observer<? super String> observer) { |
| 119 |
observer.onSubscribe(Disposable.empty()); |
| 120 |
observer.onNext("one"); |
| 121 |
observer.onError(new Throwable("test failed")); |
| 122 |
} |
| 123 |
}); |
| 124 |
|
| 125 |
Observer<String> observer = TestHelper.mockObserver(); |
| 126 |
|
| 127 |
source.take(1).subscribe(observer); |
| 128 |
|
| 129 |
verify(observer).onSubscribe((Disposable)notNull()); |
| 130 |
verify(observer, times(1)).onNext("one"); |
| 131 |
// even though onError is called we take(1) so shouldn't see it |
| 132 |
verify(observer, never()).onError(any(Throwable.class)); |
| 133 |
verify(observer, times(1)).onComplete(); |
| 134 |
verifyNoMoreInteractions(observer); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void unsubscribeAfterTake() { |
| 139 |
TestObservableFunc f = new TestObservableFunc("one", "two", "three"); |
| 140 |
Observable<String> w = Observable.unsafeCreate(f); |
| 141 |
|
| 142 |
Observer<String> observer = TestHelper.mockObserver(); |
| 143 |
|
| 144 |
Observable<String> take = w.take(1); |
| 145 |
take.subscribe(observer); |
| 146 |
|
| 147 |
// wait for the Observable to complete |
| 148 |
try { |
| 149 |
f.t.join(); |
| 150 |
} catch (Throwable e) { |
| 151 |
e.printStackTrace(); |
| 152 |
fail(e.getMessage()); |
| 153 |
} |
| 154 |
|
| 155 |
System.out.println("TestObservable thread finished"); |
| 156 |
verify(observer).onSubscribe((Disposable)notNull()); |
| 157 |
verify(observer, times(1)).onNext("one"); |
| 158 |
verify(observer, never()).onNext("two"); |
| 159 |
verify(observer, never()).onNext("three"); |
| 160 |
verify(observer, times(1)).onComplete(); |
| 161 |
verifyNoMoreInteractions(observer); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void unsubscribeFromSynchronousInfiniteObservable() { |
| 166 |
final AtomicLong count = new AtomicLong(); |
| 167 |
INFINITE_OBSERVABLE.take(10).subscribe(new Consumer<Long>() { |
| 168 |
|
| 169 |
@Override |
| 170 |
public void accept(Long l) { |
| 171 |
count.set(l); |
| 172 |
} |
| 173 |
|
| 174 |
}); |
| 175 |
assertEquals(10, count.get()); |
| 176 |
} |
| 177 |
|
| 178 |
@Test |
| 179 |
public void multiTake() { |
| 180 |
final AtomicInteger count = new AtomicInteger(); |
| 181 |
Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 182 |
|
| 183 |
@Override |
| 184 |
public void subscribe(Observer<? super Integer> observer) { |
| 185 |
Disposable bs = Disposable.empty(); |
| 186 |
observer.onSubscribe(bs); |
| 187 |
for (int i = 0; !bs.isDisposed(); i++) { |
| 188 |
System.out.println("Emit: " + i); |
| 189 |
count.incrementAndGet(); |
| 190 |
observer.onNext(i); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
}).take(100).take(1).blockingForEach(new Consumer<Integer>() { |
| 195 |
|
| 196 |
@Override |
| 197 |
public void accept(Integer t1) { |
| 198 |
System.out.println("Receive: " + t1); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
}); |
| 203 |
|
| 204 |
assertEquals(1, count.get()); |
| 205 |
} |
| 206 |
|
| 207 |
static class TestObservableFunc implements ObservableSource<String> { |
| 208 |
|
| 209 |
final String[] values; |
| 210 |
Thread t; |
| 211 |
|
| 212 |
TestObservableFunc(String... values) { |
| 213 |
this.values = values; |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public void subscribe(final Observer<? super String> observer) { |
| 218 |
observer.onSubscribe(Disposable.empty()); |
| 219 |
System.out.println("TestObservable subscribed to ..."); |
| 220 |
t = new Thread(new Runnable() { |
| 221 |
|
| 222 |
@Override |
| 223 |
public void run() { |
| 224 |
try { |
| 225 |
System.out.println("running TestObservable thread"); |
| 226 |
for (String s : values) { |
| 227 |
System.out.println("TestObservable onNext: " + s); |
| 228 |
observer.onNext(s); |
| 229 |
} |
| 230 |
observer.onComplete(); |
| 231 |
} catch (Throwable e) { |
| 232 |
throw new RuntimeException(e); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
}); |
| 237 |
System.out.println("starting TestObservable thread"); |
| 238 |
t.start(); |
| 239 |
System.out.println("done starting TestObservable thread"); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
private static Observable<Long> INFINITE_OBSERVABLE = Observable.unsafeCreate(new ObservableSource<Long>() { |
| 244 |
|
| 245 |
@Override |
| 246 |
public void subscribe(Observer<? super Long> op) { |
| 247 |
Disposable d = Disposable.empty(); |
| 248 |
op.onSubscribe(d); |
| 249 |
long l = 1; |
| 250 |
while (!d.isDisposed()) { |
| 251 |
op.onNext(l++); |
| 252 |
} |
| 253 |
op.onComplete(); |
| 254 |
} |
| 255 |
|
| 256 |
}); |
| 257 |
|
| 258 |
@Test |
| 259 |
public void takeObserveOn() { |
| 260 |
Observer<Object> o = TestHelper.mockObserver(); |
| 261 |
TestObserver<Object> to = new TestObserver<>(o); |
| 262 |
|
| 263 |
INFINITE_OBSERVABLE |
| 264 |
.observeOn(Schedulers.newThread()).take(1).subscribe(to); |
| 265 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 266 |
to.assertNoErrors(); |
| 267 |
|
| 268 |
verify(o).onNext(1L); |
| 269 |
verify(o, never()).onNext(2L); |
| 270 |
verify(o).onComplete(); |
| 271 |
verify(o, never()).onError(any(Throwable.class)); |
| 272 |
} |
| 273 |
|
| 274 |
@Test |
| 275 |
public void interrupt() throws InterruptedException { |
| 276 |
final AtomicReference<Object> exception = new AtomicReference<>(); |
| 277 |
final CountDownLatch latch = new CountDownLatch(1); |
| 278 |
Observable.just(1).subscribeOn(Schedulers.computation()).take(1) |
| 279 |
.subscribe(new Consumer<Integer>() { |
| 280 |
|
| 281 |
@Override |
| 282 |
public void accept(Integer t1) { |
| 283 |
try { |
| 284 |
Thread.sleep(100); |
| 285 |
} catch (Exception e) { |
| 286 |
exception.set(e); |
| 287 |
e.printStackTrace(); |
| 288 |
} finally { |
| 289 |
latch.countDown(); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
}); |
| 294 |
|
| 295 |
latch.await(); |
| 296 |
assertNull(exception.get()); |
| 297 |
} |
| 298 |
|
| 299 |
@Test |
| 300 |
public void takeFinalValueThrows() { |
| 301 |
Observable<Integer> source = Observable.just(1).take(1); |
| 302 |
|
| 303 |
TestObserver<Integer> to = new TestObserver<Integer>() { |
| 304 |
@Override |
| 305 |
public void onNext(Integer t) { |
| 306 |
throw new TestException(); |
| 307 |
} |
| 308 |
}; |
| 309 |
|
| 310 |
source.safeSubscribe(to); |
| 311 |
|
| 312 |
to.assertNoValues(); |
| 313 |
to.assertError(TestException.class); |
| 314 |
to.assertNotComplete(); |
| 315 |
} |
| 316 |
|
| 317 |
@Test |
| 318 |
public void reentrantTake() { |
| 319 |
final PublishSubject<Integer> source = PublishSubject.create(); |
| 320 |
|
| 321 |
TestObserver<Integer> to = new TestObserver<>(); |
| 322 |
|
| 323 |
source.take(1).doOnNext(new Consumer<Integer>() { |
| 324 |
@Override |
| 325 |
public void accept(Integer v) { |
| 326 |
source.onNext(2); |
| 327 |
} |
| 328 |
}).subscribe(to); |
| 329 |
|
| 330 |
source.onNext(1); |
| 331 |
|
| 332 |
to.assertValue(1); |
| 333 |
to.assertNoErrors(); |
| 334 |
to.assertComplete(); |
| 335 |
} |
| 336 |
|
| 337 |
@Test |
| 338 |
public void takeNegative() { |
| 339 |
try { |
| 340 |
Observable.just(1).take(-99); |
| 341 |
fail("Should have thrown"); |
| 342 |
} catch (IllegalArgumentException ex) { |
| 343 |
assertEquals("count >= 0 required but it was -99", ex.getMessage()); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
@Test |
| 348 |
public void takeZero() { |
| 349 |
Observable.just(1) |
| 350 |
.take(0) |
| 351 |
.test() |
| 352 |
.assertResult(); |
| 353 |
} |
| 354 |
|
| 355 |
@Test |
| 356 |
public void dispose() { |
| 357 |
TestHelper.checkDisposed(PublishSubject.create().take(2)); |
| 358 |
} |
| 359 |
|
| 360 |
@Test |
| 361 |
public void doubleOnSubscribe() { |
| 362 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 363 |
@Override |
| 364 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 365 |
return o.take(2); |
| 366 |
} |
| 367 |
}); |
| 368 |
} |
| 369 |
|
| 370 |
@Test |
| 371 |
public void errorAfterLimitReached() { |
| 372 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 373 |
try { |
| 374 |
Observable.error(new TestException()) |
| 375 |
.take(0) |
| 376 |
.test() |
| 377 |
.assertResult(); |
| 378 |
|
| 379 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 380 |
} finally { |
| 381 |
RxJavaPlugins.reset(); |
| 382 |
} |
| 383 |
} |
| 384 |
} |