| 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.assertEquals; |
| 17 |
|
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
import org.reactivestreams.*; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.functions.Consumer; |
| 25 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 26 |
|
| 27 |
public class FlowableDoOnSubscribeTest extends RxJavaTest { |
| 28 |
|
| 29 |
@Test |
| 30 |
public void doOnSubscribe() throws Exception { |
| 31 |
final AtomicInteger count = new AtomicInteger(); |
| 32 |
Flowable<Integer> f = Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() { |
| 33 |
@Override |
| 34 |
public void accept(Subscription s) { |
| 35 |
count.incrementAndGet(); |
| 36 |
} |
| 37 |
}); |
| 38 |
|
| 39 |
f.subscribe(); |
| 40 |
f.subscribe(); |
| 41 |
f.subscribe(); |
| 42 |
assertEquals(3, count.get()); |
| 43 |
} |
| 44 |
|
| 45 |
@Test |
| 46 |
public void doOnSubscribe2() throws Exception { |
| 47 |
final AtomicInteger count = new AtomicInteger(); |
| 48 |
Flowable<Integer> f = Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() { |
| 49 |
@Override |
| 50 |
public void accept(Subscription s) { |
| 51 |
count.incrementAndGet(); |
| 52 |
} |
| 53 |
}).take(1).doOnSubscribe(new Consumer<Subscription>() { |
| 54 |
@Override |
| 55 |
public void accept(Subscription s) { |
| 56 |
count.incrementAndGet(); |
| 57 |
} |
| 58 |
}); |
| 59 |
|
| 60 |
f.subscribe(); |
| 61 |
assertEquals(2, count.get()); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void doOnUnSubscribeWorksWithRefCount() throws Exception { |
| 66 |
final AtomicInteger onSubscribed = new AtomicInteger(); |
| 67 |
final AtomicInteger countBefore = new AtomicInteger(); |
| 68 |
final AtomicInteger countAfter = new AtomicInteger(); |
| 69 |
final AtomicReference<Subscriber<? super Integer>> sref = new AtomicReference<>(); |
| 70 |
Flowable<Integer> f = Flowable.unsafeCreate(new Publisher<Integer>() { |
| 71 |
|
| 72 |
@Override |
| 73 |
public void subscribe(Subscriber<? super Integer> s) { |
| 74 |
s.onSubscribe(new BooleanSubscription()); |
| 75 |
onSubscribed.incrementAndGet(); |
| 76 |
sref.set(s); |
| 77 |
} |
| 78 |
|
| 79 |
}).doOnSubscribe(new Consumer<Subscription>() { |
| 80 |
@Override |
| 81 |
public void accept(Subscription s) { |
| 82 |
countBefore.incrementAndGet(); |
| 83 |
} |
| 84 |
}).publish().refCount() |
| 85 |
.doOnSubscribe(new Consumer<Subscription>() { |
| 86 |
@Override |
| 87 |
public void accept(Subscription s) { |
| 88 |
countAfter.incrementAndGet(); |
| 89 |
} |
| 90 |
}); |
| 91 |
|
| 92 |
f.subscribe(); |
| 93 |
f.subscribe(); |
| 94 |
f.subscribe(); |
| 95 |
assertEquals(1, countBefore.get()); |
| 96 |
assertEquals(1, onSubscribed.get()); |
| 97 |
assertEquals(3, countAfter.get()); |
| 98 |
sref.get().onComplete(); |
| 99 |
f.subscribe(); |
| 100 |
f.subscribe(); |
| 101 |
f.subscribe(); |
| 102 |
assertEquals(2, countBefore.get()); |
| 103 |
assertEquals(2, onSubscribed.get()); |
| 104 |
assertEquals(6, countAfter.get()); |
| 105 |
} |
| 106 |
|
| 107 |
} |