| 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.assertEquals; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.disposables.*; |
| 26 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 27 |
import io.reactivex.rxjava3.schedulers.*; |
| 28 |
import io.reactivex.rxjava3.testsupport.*; |
| 29 |
|
| 30 |
public class ObservableSubscribeOnTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void issue813() throws InterruptedException { |
| 34 |
// https://github.com/ReactiveX/RxJava/issues/813 |
| 35 |
final CountDownLatch scheduled = new CountDownLatch(1); |
| 36 |
final CountDownLatch latch = new CountDownLatch(1); |
| 37 |
final CountDownLatch doneLatch = new CountDownLatch(1); |
| 38 |
|
| 39 |
TestObserver<Integer> to = new TestObserver<>(); |
| 40 |
|
| 41 |
Observable |
| 42 |
.unsafeCreate(new ObservableSource<Integer>() { |
| 43 |
@Override |
| 44 |
public void subscribe( |
| 45 |
final Observer<? super Integer> observer) { |
| 46 |
observer.onSubscribe(Disposable.empty()); |
| 47 |
scheduled.countDown(); |
| 48 |
try { |
| 49 |
try { |
| 50 |
latch.await(); |
| 51 |
} catch (InterruptedException e) { |
| 52 |
// this means we were unsubscribed (Scheduler shut down and interrupts) |
| 53 |
// ... but we'll pretend we are like many Observables that ignore interrupts |
| 54 |
} |
| 55 |
|
| 56 |
observer.onComplete(); |
| 57 |
} catch (Throwable e) { |
| 58 |
observer.onError(e); |
| 59 |
} finally { |
| 60 |
doneLatch.countDown(); |
| 61 |
} |
| 62 |
} |
| 63 |
}).subscribeOn(Schedulers.computation()).subscribe(to); |
| 64 |
|
| 65 |
// wait for scheduling |
| 66 |
scheduled.await(); |
| 67 |
// trigger unsubscribe |
| 68 |
to.dispose(); |
| 69 |
latch.countDown(); |
| 70 |
doneLatch.await(); |
| 71 |
to.assertNoErrors(); |
| 72 |
to.assertComplete(); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void onError() { |
| 77 |
TestObserverEx<String> to = new TestObserverEx<>(); |
| 78 |
Observable.unsafeCreate(new ObservableSource<String>() { |
| 79 |
|
| 80 |
@Override |
| 81 |
public void subscribe(Observer<? super String> observer) { |
| 82 |
observer.onSubscribe(Disposable.empty()); |
| 83 |
observer.onError(new RuntimeException("fail")); |
| 84 |
} |
| 85 |
|
| 86 |
}).subscribeOn(Schedulers.computation()).subscribe(to); |
| 87 |
to.awaitDone(1000, TimeUnit.MILLISECONDS); |
| 88 |
to.assertTerminated(); |
| 89 |
} |
| 90 |
|
| 91 |
public static class SlowScheduler extends Scheduler { |
| 92 |
final Scheduler actual; |
| 93 |
final long delay; |
| 94 |
final TimeUnit unit; |
| 95 |
|
| 96 |
public SlowScheduler() { |
| 97 |
this(Schedulers.computation(), 2, TimeUnit.SECONDS); |
| 98 |
} |
| 99 |
|
| 100 |
public SlowScheduler(Scheduler actual, long delay, TimeUnit unit) { |
| 101 |
this.actual = actual; |
| 102 |
this.delay = delay; |
| 103 |
this.unit = unit; |
| 104 |
} |
| 105 |
|
| 106 |
@NonNull |
| 107 |
@Override |
| 108 |
public Worker createWorker() { |
| 109 |
return new SlowInner(actual.createWorker()); |
| 110 |
} |
| 111 |
|
| 112 |
private final class SlowInner extends Worker { |
| 113 |
|
| 114 |
private final Scheduler.Worker actualInner; |
| 115 |
|
| 116 |
private SlowInner(Worker actual) { |
| 117 |
this.actualInner = actual; |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void dispose() { |
| 122 |
actualInner.dispose(); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean isDisposed() { |
| 127 |
return actualInner.isDisposed(); |
| 128 |
} |
| 129 |
|
| 130 |
@NonNull |
| 131 |
@Override |
| 132 |
public Disposable schedule(@NonNull final Runnable action) { |
| 133 |
return actualInner.schedule(action, delay, unit); |
| 134 |
} |
| 135 |
|
| 136 |
@NonNull |
| 137 |
@Override |
| 138 |
public Disposable schedule(@NonNull final Runnable action, final long delayTime, @NonNull final TimeUnit delayUnit) { |
| 139 |
TimeUnit common = delayUnit.compareTo(unit) < 0 ? delayUnit : unit; |
| 140 |
long t = common.convert(delayTime, delayUnit) + common.convert(delay, unit); |
| 141 |
return actualInner.schedule(action, t, common); |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void unsubscribeInfiniteStream() throws InterruptedException { |
| 150 |
TestObserver<Integer> to = new TestObserver<>(); |
| 151 |
final AtomicInteger count = new AtomicInteger(); |
| 152 |
Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 153 |
|
| 154 |
@Override |
| 155 |
public void subscribe(Observer<? super Integer> sub) { |
| 156 |
Disposable d = Disposable.empty(); |
| 157 |
sub.onSubscribe(d); |
| 158 |
for (int i = 1; !d.isDisposed(); i++) { |
| 159 |
count.incrementAndGet(); |
| 160 |
sub.onNext(i); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
}).subscribeOn(Schedulers.newThread()).take(10).subscribe(to); |
| 165 |
|
| 166 |
to.awaitDone(1000, TimeUnit.MILLISECONDS); |
| 167 |
to.dispose(); |
| 168 |
Thread.sleep(200); // give time for the loop to continue |
| 169 |
to.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 170 |
assertEquals(10, count.get()); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void cancelBeforeActualSubscribe() { |
| 175 |
TestScheduler test = new TestScheduler(); |
| 176 |
|
| 177 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 178 |
|
| 179 |
Observable.just(1).hide() |
| 180 |
.subscribeOn(test).subscribe(to); |
| 181 |
|
| 182 |
to.dispose(); |
| 183 |
|
| 184 |
test.advanceTimeBy(1, TimeUnit.SECONDS); |
| 185 |
|
| 186 |
to |
| 187 |
.assertSubscribed() |
| 188 |
.assertNoValues() |
| 189 |
.assertNotTerminated(); |
| 190 |
} |
| 191 |
|
| 192 |
@Test |
| 193 |
public void dispose() { |
| 194 |
TestHelper.checkDisposed(Observable.just(1).subscribeOn(Schedulers.single())); |
| 195 |
} |
| 196 |
} |