| 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 |
|
| 18 |
import java.util.*; |
| 19 |
import java.util.concurrent.TimeUnit; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.Observable; |
| 24 |
import io.reactivex.rxjava3.core.Observer; |
| 25 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 26 |
import io.reactivex.rxjava3.disposables.*; |
| 27 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 28 |
import io.reactivex.rxjava3.functions.*; |
| 29 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 30 |
import io.reactivex.rxjava3.internal.observers.BlockingFirstObserver; |
| 31 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 34 |
|
| 35 |
public class ObservableBlockingTest extends RxJavaTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void blockingFirst() { |
| 39 |
assertEquals(1, Observable.range(1, 10) |
| 40 |
.subscribeOn(Schedulers.computation()).blockingFirst().intValue()); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void blockingFirstDefault() { |
| 45 |
assertEquals(1, Observable.<Integer>empty() |
| 46 |
.subscribeOn(Schedulers.computation()).blockingFirst(1).intValue()); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void blockingSubscribeConsumer() { |
| 51 |
final List<Integer> list = new ArrayList<>(); |
| 52 |
|
| 53 |
Observable.range(1, 5) |
| 54 |
.subscribeOn(Schedulers.computation()) |
| 55 |
.blockingSubscribe(new Consumer<Integer>() { |
| 56 |
@Override |
| 57 |
public void accept(Integer v) throws Exception { |
| 58 |
list.add(v); |
| 59 |
} |
| 60 |
}); |
| 61 |
|
| 62 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void blockingSubscribeConsumerConsumer() { |
| 67 |
final List<Object> list = new ArrayList<>(); |
| 68 |
|
| 69 |
Observable.range(1, 5) |
| 70 |
.subscribeOn(Schedulers.computation()) |
| 71 |
.blockingSubscribe(new Consumer<Integer>() { |
| 72 |
@Override |
| 73 |
public void accept(Integer v) throws Exception { |
| 74 |
list.add(v); |
| 75 |
} |
| 76 |
}, Functions.emptyConsumer()); |
| 77 |
|
| 78 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5), list); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
public void blockingSubscribeConsumerConsumerError() { |
| 83 |
final List<Object> list = new ArrayList<>(); |
| 84 |
|
| 85 |
TestException ex = new TestException(); |
| 86 |
|
| 87 |
Consumer<Object> cons = new Consumer<Object>() { |
| 88 |
@Override |
| 89 |
public void accept(Object v) throws Exception { |
| 90 |
list.add(v); |
| 91 |
} |
| 92 |
}; |
| 93 |
|
| 94 |
Observable.range(1, 5).concatWith(Observable.<Integer>error(ex)) |
| 95 |
.subscribeOn(Schedulers.computation()) |
| 96 |
.blockingSubscribe(cons, cons); |
| 97 |
|
| 98 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, ex), list); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void blockingSubscribeConsumerConsumerAction() { |
| 103 |
final List<Object> list = new ArrayList<>(); |
| 104 |
|
| 105 |
Consumer<Object> cons = new Consumer<Object>() { |
| 106 |
@Override |
| 107 |
public void accept(Object v) throws Exception { |
| 108 |
list.add(v); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
Observable.range(1, 5) |
| 113 |
.subscribeOn(Schedulers.computation()) |
| 114 |
.blockingSubscribe(cons, cons, new Action() { |
| 115 |
@Override |
| 116 |
public void run() throws Exception { |
| 117 |
list.add(100); |
| 118 |
} |
| 119 |
}); |
| 120 |
|
| 121 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void blockingSubscribeObserver() { |
| 126 |
final List<Object> list = new ArrayList<>(); |
| 127 |
|
| 128 |
Observable.range(1, 5) |
| 129 |
.subscribeOn(Schedulers.computation()) |
| 130 |
.blockingSubscribe(new Observer<Object>() { |
| 131 |
|
| 132 |
@Override |
| 133 |
public void onSubscribe(Disposable d) { |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void onNext(Object value) { |
| 139 |
list.add(value); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void onError(Throwable e) { |
| 144 |
list.add(e); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void onComplete() { |
| 149 |
list.add(100); |
| 150 |
} |
| 151 |
|
| 152 |
}); |
| 153 |
|
| 154 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 100), list); |
| 155 |
} |
| 156 |
|
| 157 |
@Test |
| 158 |
public void blockingSubscribeObserverError() { |
| 159 |
final List<Object> list = new ArrayList<>(); |
| 160 |
|
| 161 |
final TestException ex = new TestException(); |
| 162 |
|
| 163 |
Observable.range(1, 5).concatWith(Observable.<Integer>error(ex)) |
| 164 |
.subscribeOn(Schedulers.computation()) |
| 165 |
.blockingSubscribe(new Observer<Object>() { |
| 166 |
|
| 167 |
@Override |
| 168 |
public void onSubscribe(Disposable d) { |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public void onNext(Object value) { |
| 174 |
list.add(value); |
| 175 |
} |
| 176 |
|
| 177 |
@Override |
| 178 |
public void onError(Throwable e) { |
| 179 |
list.add(e); |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public void onComplete() { |
| 184 |
list.add(100); |
| 185 |
} |
| 186 |
|
| 187 |
}); |
| 188 |
|
| 189 |
assertEquals(Arrays.asList(1, 2, 3, 4, 5, ex), list); |
| 190 |
} |
| 191 |
|
| 192 |
@Test(expected = TestException.class) |
| 193 |
public void blockingForEachThrows() { |
| 194 |
Observable.just(1) |
| 195 |
.blockingForEach(new Consumer<Integer>() { |
| 196 |
@Override |
| 197 |
public void accept(Integer e) throws Exception { |
| 198 |
throw new TestException(); |
| 199 |
} |
| 200 |
}); |
| 201 |
} |
| 202 |
|
| 203 |
@Test(expected = NoSuchElementException.class) |
| 204 |
public void blockingFirstEmpty() { |
| 205 |
Observable.empty().blockingFirst(); |
| 206 |
} |
| 207 |
|
| 208 |
@Test(expected = NoSuchElementException.class) |
| 209 |
public void blockingLastEmpty() { |
| 210 |
Observable.empty().blockingLast(); |
| 211 |
} |
| 212 |
|
| 213 |
@Test |
| 214 |
public void blockingFirstNormal() { |
| 215 |
assertEquals(1, Observable.just(1, 2).blockingFirst(3).intValue()); |
| 216 |
} |
| 217 |
|
| 218 |
@Test |
| 219 |
public void blockingLastNormal() { |
| 220 |
assertEquals(2, Observable.just(1, 2).blockingLast(3).intValue()); |
| 221 |
} |
| 222 |
|
| 223 |
@Test(expected = NoSuchElementException.class) |
| 224 |
public void blockingSingleEmpty() { |
| 225 |
Observable.empty().blockingSingle(); |
| 226 |
} |
| 227 |
|
| 228 |
@Test |
| 229 |
public void utilityClass() { |
| 230 |
TestHelper.checkUtilityClass(ObservableBlockingSubscribe.class); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void disposeUpFront() { |
| 235 |
TestObserver<Object> to = new TestObserver<>(); |
| 236 |
to.dispose(); |
| 237 |
Observable.just(1).blockingSubscribe(to); |
| 238 |
|
| 239 |
to.assertEmpty(); |
| 240 |
} |
| 241 |
|
| 242 |
@SuppressWarnings("rawtypes") |
| 243 |
@Test |
| 244 |
public void delayed() throws Exception { |
| 245 |
final TestObserver<Object> to = new TestObserver<>(); |
| 246 |
final Observer[] s = { null }; |
| 247 |
|
| 248 |
Schedulers.single().scheduleDirect(new Runnable() { |
| 249 |
@SuppressWarnings("unchecked") |
| 250 |
@Override |
| 251 |
public void run() { |
| 252 |
to.dispose(); |
| 253 |
s[0].onNext(1); |
| 254 |
} |
| 255 |
}, 200, TimeUnit.MILLISECONDS); |
| 256 |
|
| 257 |
new Observable<Integer>() { |
| 258 |
@Override |
| 259 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 260 |
observer.onSubscribe(Disposable.empty()); |
| 261 |
s[0] = observer; |
| 262 |
} |
| 263 |
}.blockingSubscribe(to); |
| 264 |
|
| 265 |
while (!to.isDisposed()) { |
| 266 |
Thread.sleep(100); |
| 267 |
} |
| 268 |
|
| 269 |
to.assertEmpty(); |
| 270 |
} |
| 271 |
|
| 272 |
@Test |
| 273 |
public void interrupt() { |
| 274 |
TestObserver<Object> to = new TestObserver<>(); |
| 275 |
Thread.currentThread().interrupt(); |
| 276 |
Observable.never().blockingSubscribe(to); |
| 277 |
} |
| 278 |
|
| 279 |
@Test |
| 280 |
public void onCompleteDelayed() { |
| 281 |
TestObserver<Object> to = new TestObserver<>(); |
| 282 |
|
| 283 |
Observable.empty().delay(100, TimeUnit.MILLISECONDS) |
| 284 |
.blockingSubscribe(to); |
| 285 |
|
| 286 |
to.assertResult(); |
| 287 |
} |
| 288 |
|
| 289 |
@Test |
| 290 |
public void blockingCancelUpfront() { |
| 291 |
BlockingFirstObserver<Integer> o = new BlockingFirstObserver<>(); |
| 292 |
|
| 293 |
assertFalse(o.isDisposed()); |
| 294 |
o.dispose(); |
| 295 |
assertTrue(o.isDisposed()); |
| 296 |
|
| 297 |
Disposable d = Disposable.empty(); |
| 298 |
|
| 299 |
o.onSubscribe(d); |
| 300 |
|
| 301 |
assertTrue(d.isDisposed()); |
| 302 |
|
| 303 |
Thread.currentThread().interrupt(); |
| 304 |
try { |
| 305 |
o.blockingGet(); |
| 306 |
fail("Should have thrown"); |
| 307 |
} catch (RuntimeException ex) { |
| 308 |
assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException); |
| 309 |
} |
| 310 |
|
| 311 |
Thread.interrupted(); |
| 312 |
|
| 313 |
o.onError(new TestException()); |
| 314 |
|
| 315 |
try { |
| 316 |
o.blockingGet(); |
| 317 |
fail("Should have thrown"); |
| 318 |
} catch (TestException ex) { |
| 319 |
// expected |
| 320 |
} |
| 321 |
} |
| 322 |
} |