| 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.List; |
| 19 |
import java.util.concurrent.atomic.*; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.Consumer; |
| 27 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class ObservableDoOnSubscribeTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void doOnSubscribe() throws Exception { |
| 34 |
final AtomicInteger count = new AtomicInteger(); |
| 35 |
Observable<Integer> o = Observable.just(1).doOnSubscribe(new Consumer<Disposable>() { |
| 36 |
@Override |
| 37 |
public void accept(Disposable d) { |
| 38 |
count.incrementAndGet(); |
| 39 |
} |
| 40 |
}); |
| 41 |
|
| 42 |
o.subscribe(); |
| 43 |
o.subscribe(); |
| 44 |
o.subscribe(); |
| 45 |
assertEquals(3, count.get()); |
| 46 |
} |
| 47 |
|
| 48 |
@Test |
| 49 |
public void doOnSubscribe2() throws Exception { |
| 50 |
final AtomicInteger count = new AtomicInteger(); |
| 51 |
Observable<Integer> o = Observable.just(1).doOnSubscribe(new Consumer<Disposable>() { |
| 52 |
@Override |
| 53 |
public void accept(Disposable d) { |
| 54 |
count.incrementAndGet(); |
| 55 |
} |
| 56 |
}).take(1).doOnSubscribe(new Consumer<Disposable>() { |
| 57 |
@Override |
| 58 |
public void accept(Disposable d) { |
| 59 |
count.incrementAndGet(); |
| 60 |
} |
| 61 |
}); |
| 62 |
|
| 63 |
o.subscribe(); |
| 64 |
assertEquals(2, count.get()); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void doOnUnSubscribeWorksWithRefCount() throws Exception { |
| 69 |
final AtomicInteger onSubscribed = new AtomicInteger(); |
| 70 |
final AtomicInteger countBefore = new AtomicInteger(); |
| 71 |
final AtomicInteger countAfter = new AtomicInteger(); |
| 72 |
final AtomicReference<Observer<? super Integer>> sref = new AtomicReference<>(); |
| 73 |
Observable<Integer> o = Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 74 |
|
| 75 |
@Override |
| 76 |
public void subscribe(Observer<? super Integer> observer) { |
| 77 |
observer.onSubscribe(Disposable.empty()); |
| 78 |
onSubscribed.incrementAndGet(); |
| 79 |
sref.set(observer); |
| 80 |
} |
| 81 |
|
| 82 |
}).doOnSubscribe(new Consumer<Disposable>() { |
| 83 |
@Override |
| 84 |
public void accept(Disposable d) { |
| 85 |
countBefore.incrementAndGet(); |
| 86 |
} |
| 87 |
}).publish().refCount() |
| 88 |
.doOnSubscribe(new Consumer<Disposable>() { |
| 89 |
@Override |
| 90 |
public void accept(Disposable d) { |
| 91 |
countAfter.incrementAndGet(); |
| 92 |
} |
| 93 |
}); |
| 94 |
|
| 95 |
o.subscribe(); |
| 96 |
o.subscribe(); |
| 97 |
o.subscribe(); |
| 98 |
assertEquals(1, countBefore.get()); |
| 99 |
assertEquals(1, onSubscribed.get()); |
| 100 |
assertEquals(3, countAfter.get()); |
| 101 |
sref.get().onComplete(); |
| 102 |
o.subscribe(); |
| 103 |
o.subscribe(); |
| 104 |
o.subscribe(); |
| 105 |
assertEquals(2, countBefore.get()); |
| 106 |
assertEquals(2, onSubscribed.get()); |
| 107 |
assertEquals(6, countAfter.get()); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void onSubscribeCrash() { |
| 112 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 113 |
try { |
| 114 |
final Disposable bs = Disposable.empty(); |
| 115 |
|
| 116 |
new Observable<Integer>() { |
| 117 |
@Override |
| 118 |
protected void subscribeActual(Observer<? super Integer> observer) { |
| 119 |
observer.onSubscribe(bs); |
| 120 |
observer.onError(new TestException("Second")); |
| 121 |
observer.onComplete(); |
| 122 |
} |
| 123 |
} |
| 124 |
.doOnSubscribe(new Consumer<Disposable>() { |
| 125 |
@Override |
| 126 |
public void accept(Disposable d) throws Exception { |
| 127 |
throw new TestException("First"); |
| 128 |
} |
| 129 |
}) |
| 130 |
.to(TestHelper.<Integer>testConsumer()) |
| 131 |
.assertFailureAndMessage(TestException.class, "First"); |
| 132 |
|
| 133 |
assertTrue(bs.isDisposed()); |
| 134 |
|
| 135 |
TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second"); |
| 136 |
} finally { |
| 137 |
RxJavaPlugins.reset(); |
| 138 |
} |
| 139 |
} |
| 140 |
} |