| 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 static org.junit.Assert.*; |
| 17 |
|
| 18 |
import java.util.Arrays; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
import org.reactivestreams.*; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.functions.Consumer; |
| 27 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 28 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 29 |
import io.reactivex.rxjava3.subscribers.*; |
| 30 |
|
| 31 |
public class FlowableSwitchIfEmptyTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void switchWhenNotEmpty() throws Exception { |
| 35 |
final AtomicBoolean subscribed = new AtomicBoolean(false); |
| 36 |
final Flowable<Integer> flowable = Flowable.just(4) |
| 37 |
.switchIfEmpty(Flowable.just(2) |
| 38 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 39 |
@Override |
| 40 |
public void accept(Subscription s) { |
| 41 |
subscribed.set(true); |
| 42 |
} |
| 43 |
})); |
| 44 |
|
| 45 |
assertEquals(4, flowable.blockingSingle().intValue()); |
| 46 |
assertFalse(subscribed.get()); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void switchWhenEmpty() throws Exception { |
| 51 |
final Flowable<Integer> flowable = Flowable.<Integer>empty() |
| 52 |
.switchIfEmpty(Flowable.fromIterable(Arrays.asList(42))); |
| 53 |
|
| 54 |
assertEquals(42, flowable.blockingSingle().intValue()); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void switchWithProducer() throws Exception { |
| 59 |
final AtomicBoolean emitted = new AtomicBoolean(false); |
| 60 |
Flowable<Long> withProducer = Flowable.unsafeCreate(new Publisher<Long>() { |
| 61 |
@Override |
| 62 |
public void subscribe(final Subscriber<? super Long> subscriber) { |
| 63 |
subscriber.onSubscribe(new Subscription() { |
| 64 |
@Override |
| 65 |
public void request(long n) { |
| 66 |
if (n > 0 && emitted.compareAndSet(false, true)) { |
| 67 |
emitted.set(true); |
| 68 |
subscriber.onNext(42L); |
| 69 |
subscriber.onComplete(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void cancel() { |
| 75 |
|
| 76 |
} |
| 77 |
}); |
| 78 |
} |
| 79 |
}); |
| 80 |
|
| 81 |
final Flowable<Long> flowable = Flowable.<Long>empty().switchIfEmpty(withProducer); |
| 82 |
assertEquals(42, flowable.blockingSingle().intValue()); |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void switchTriggerUnsubscribe() throws Exception { |
| 87 |
|
| 88 |
final BooleanSubscription bs = new BooleanSubscription(); |
| 89 |
|
| 90 |
Flowable<Long> withProducer = Flowable.unsafeCreate(new Publisher<Long>() { |
| 91 |
@Override |
| 92 |
public void subscribe(final Subscriber<? super Long> subscriber) { |
| 93 |
subscriber.onSubscribe(bs); |
| 94 |
subscriber.onNext(42L); |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
Flowable.<Long>empty() |
| 99 |
.switchIfEmpty(withProducer) |
| 100 |
.lift(new FlowableOperator<Long, Long>() { |
| 101 |
@Override |
| 102 |
public Subscriber<? super Long> apply(final Subscriber<? super Long> child) { |
| 103 |
return new DefaultSubscriber<Long>() { |
| 104 |
@Override |
| 105 |
public void onComplete() { |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void onError(Throwable e) { |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void onNext(Long aLong) { |
| 116 |
cancel(); |
| 117 |
} |
| 118 |
|
| 119 |
}; |
| 120 |
} |
| 121 |
}).subscribe(); |
| 122 |
|
| 123 |
assertTrue(bs.isCancelled()); |
| 124 |
// FIXME no longer assertable |
| 125 |
// assertTrue(sub.isUnsubscribed()); |
| 126 |
} |
| 127 |
|
| 128 |
@Test |
| 129 |
public void switchShouldNotTriggerUnsubscribe() { |
| 130 |
final BooleanSubscription bs = new BooleanSubscription(); |
| 131 |
|
| 132 |
Flowable.unsafeCreate(new Publisher<Long>() { |
| 133 |
@Override |
| 134 |
public void subscribe(final Subscriber<? super Long> subscriber) { |
| 135 |
subscriber.onSubscribe(bs); |
| 136 |
subscriber.onComplete(); |
| 137 |
} |
| 138 |
}).switchIfEmpty(Flowable.<Long>never()).subscribe(); |
| 139 |
assertFalse(bs.isCancelled()); |
| 140 |
} |
| 141 |
|
| 142 |
@Test |
| 143 |
public void switchRequestAlternativeObservableWithBackpressure() { |
| 144 |
|
| 145 |
TestSubscriber<Integer> ts = new TestSubscriber<>(1L); |
| 146 |
|
| 147 |
Flowable.<Integer>empty().switchIfEmpty(Flowable.just(1, 2, 3)).subscribe(ts); |
| 148 |
|
| 149 |
assertEquals(Arrays.asList(1), ts.values()); |
| 150 |
ts.assertNoErrors(); |
| 151 |
ts.request(1); |
| 152 |
ts.assertValueCount(2); |
| 153 |
ts.request(1); |
| 154 |
ts.assertValueCount(3); |
| 155 |
} |
| 156 |
|
| 157 |
@Test |
| 158 |
public void backpressureNoRequest() { |
| 159 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 160 |
Flowable.<Integer>empty().switchIfEmpty(Flowable.just(1, 2, 3)).subscribe(ts); |
| 161 |
ts.assertNoValues(); |
| 162 |
ts.assertNoErrors(); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void backpressureOnFirstObservable() { |
| 167 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0L); |
| 168 |
Flowable.just(1, 2, 3).switchIfEmpty(Flowable.just(4, 5, 6)).subscribe(ts); |
| 169 |
ts.assertNotComplete(); |
| 170 |
ts.assertNoErrors(); |
| 171 |
ts.assertNoValues(); |
| 172 |
} |
| 173 |
|
| 174 |
@Test |
| 175 |
public void requestsNotLost() throws InterruptedException { |
| 176 |
final TestSubscriber<Long> ts = new TestSubscriber<>(0L); |
| 177 |
Flowable.unsafeCreate(new Publisher<Long>() { |
| 178 |
|
| 179 |
@Override |
| 180 |
public void subscribe(final Subscriber<? super Long> subscriber) { |
| 181 |
subscriber.onSubscribe(new Subscription() { |
| 182 |
final AtomicBoolean completed = new AtomicBoolean(false); |
| 183 |
@Override |
| 184 |
public void request(long n) { |
| 185 |
if (n > 0 && completed.compareAndSet(false, true)) { |
| 186 |
Schedulers.io().createWorker().schedule(new Runnable() { |
| 187 |
@Override |
| 188 |
public void run() { |
| 189 |
subscriber.onComplete(); |
| 190 |
}}, 100, TimeUnit.MILLISECONDS); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void cancel() { |
| 196 |
|
| 197 |
} |
| 198 |
}); |
| 199 |
}}) |
| 200 |
.switchIfEmpty(Flowable.fromIterable(Arrays.asList(1L, 2L, 3L))) |
| 201 |
.subscribeOn(Schedulers.computation()) |
| 202 |
.subscribe(ts); |
| 203 |
|
| 204 |
Thread.sleep(50); |
| 205 |
//request while first observable is still finishing (as empty) |
| 206 |
ts.request(1); |
| 207 |
ts.request(1); |
| 208 |
Thread.sleep(500); |
| 209 |
ts.assertNotComplete(); |
| 210 |
ts.assertNoErrors(); |
| 211 |
ts.assertValueCount(2); |
| 212 |
ts.cancel(); |
| 213 |
} |
| 214 |
} |