use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableGroupByTest method groupByUnsubscribe.
@Test
public void groupByUnsubscribe() {
final Disposable upstream = mock(Disposable.class);
Observable<Integer> o = Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> observer) {
observer.onSubscribe(upstream);
}
});
TestObserver<Object> to = new TestObserver<>();
o.groupBy(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return null;
}
}).subscribe(to);
to.dispose();
verify(upstream).dispose();
}
use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class SerializedObserverTest method infinite.
private static Observable<String> infinite(final AtomicInteger produced) {
return Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> observer) {
Disposable bs = Disposable.empty();
observer.onSubscribe(bs);
while (!bs.isDisposed()) {
observer.onNext("onNext");
produced.incrementAndGet();
}
}
}).subscribeOn(Schedulers.newThread());
}
use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableSubscribeOnTest method unsubscribeInfiniteStream.
@Test
public void unsubscribeInfiniteStream() throws InterruptedException {
TestObserver<Integer> to = new TestObserver<>();
final AtomicInteger count = new AtomicInteger();
Observable.unsafeCreate(new ObservableSource<Integer>() {
@Override
public void subscribe(Observer<? super Integer> sub) {
Disposable d = Disposable.empty();
sub.onSubscribe(d);
for (int i = 1; !d.isDisposed(); i++) {
count.incrementAndGet();
sub.onNext(i);
}
}
}).subscribeOn(Schedulers.newThread()).take(10).subscribe(to);
to.awaitDone(1000, TimeUnit.MILLISECONDS);
to.dispose();
// give time for the loop to continue
Thread.sleep(200);
to.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
assertEquals(10, count.get());
}
use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableSwitchTest method switchWithError.
@Test
public void switchWithError() {
Observable<Observable<String>> source = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {
@Override
public void subscribe(Observer<? super Observable<String>> outerObserver) {
outerObserver.onSubscribe(Disposable.empty());
publishNext(outerObserver, 50, Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> innerObserver) {
innerObserver.onSubscribe(Disposable.empty());
publishNext(innerObserver, 50, "one");
publishNext(innerObserver, 100, "two");
}
}));
publishNext(outerObserver, 200, Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> innerObserver) {
innerObserver.onSubscribe(Disposable.empty());
publishNext(innerObserver, 0, "three");
publishNext(innerObserver, 100, "four");
}
}));
publishError(outerObserver, 250, new TestException());
}
});
Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);
InOrder inOrder = inOrder(observer);
scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyString());
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext("one");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(175, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext("two");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext("three");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyString());
verify(observer, never()).onComplete();
verify(observer, times(1)).onError(any(TestException.class));
}
use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.
the class ObservableSwitchTest method switchWithComplete.
@Test
public void switchWithComplete() {
Observable<Observable<String>> source = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {
@Override
public void subscribe(Observer<? super Observable<String>> outerObserver) {
outerObserver.onSubscribe(Disposable.empty());
publishNext(outerObserver, 50, Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> innerObserver) {
innerObserver.onSubscribe(Disposable.empty());
publishNext(innerObserver, 60, "one");
publishNext(innerObserver, 100, "two");
}
}));
publishNext(outerObserver, 200, Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> innerObserver) {
innerObserver.onSubscribe(Disposable.empty());
publishNext(innerObserver, 0, "three");
publishNext(innerObserver, 100, "four");
}
}));
publishCompleted(outerObserver, 250);
}
});
Observable<String> sampled = Observable.switchOnNext(source);
sampled.subscribe(observer);
InOrder inOrder = inOrder(observer);
scheduler.advanceTimeTo(90, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyString());
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(125, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext("one");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(175, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext("two");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(225, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext("three");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext("four");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
}
Aggregations