Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDoOnUnsubscribeTest.java

raw
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.*;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.AtomicInteger;
21
22 import org.junit.Test;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.disposables.*;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.processors.BehaviorProcessor;
28 import io.reactivex.rxjava3.subscribers.TestSubscriber;
29
30 public class FlowableDoOnUnsubscribeTest extends RxJavaTest {
31
32 @Test
33 public void doOnUnsubscribe() throws Exception {
34 int subCount = 3;
35 final CountDownLatch upperLatch = new CountDownLatch(subCount);
36 final CountDownLatch lowerLatch = new CountDownLatch(subCount);
37 final CountDownLatch onNextLatch = new CountDownLatch(subCount);
38
39 final AtomicInteger upperCount = new AtomicInteger();
40 final AtomicInteger lowerCount = new AtomicInteger();
41 Flowable<Long> longs = Flowable
42 // The stream needs to be infinite to ensure the stream does not terminate
43 // before it is unsubscribed
44 .interval(50, TimeUnit.MILLISECONDS)
45 .doOnCancel(new Action() {
46 @Override
47 public void run() {
48 // Test that upper stream will be notified for un-subscription
49 // from a child subscriber
50 upperLatch.countDown();
51 upperCount.incrementAndGet();
52 }
53 })
54 .doOnNext(new Consumer<Long>() {
55 @Override
56 public void accept(Long aLong) {
57 // Ensure there is at least some onNext events before un-subscription happens
58 onNextLatch.countDown();
59 }
60 })
61 .doOnCancel(new Action() {
62 @Override
63 public void run() {
64 // Test that lower stream will be notified for a direct un-subscription
65 lowerLatch.countDown();
66 lowerCount.incrementAndGet();
67 }
68 });
69
70 List<Disposable> subscriptions = new ArrayList<>();
71 List<TestSubscriber<Long>> subscribers = new ArrayList<>();
72
73 for (int i = 0; i < subCount; ++i) {
74 TestSubscriber<Long> subscriber = new TestSubscriber<>();
75 subscriptions.add(Disposable.fromSubscription(subscriber));
76 longs.subscribe(subscriber);
77 subscribers.add(subscriber);
78 }
79
80 onNextLatch.await();
81 for (int i = 0; i < subCount; ++i) {
82 subscriptions.get(i).dispose();
83 // Test that unsubscribe() method is not affected in any way
84 }
85
86 upperLatch.await();
87 lowerLatch.await();
88 assertEquals(String.format("There should exactly %d un-subscription events for upper stream", subCount), subCount, upperCount.get());
89 assertEquals(String.format("There should exactly %d un-subscription events for lower stream", subCount), subCount, lowerCount.get());
90 }
91
92 @Test
93 public void doOnUnSubscribeWorksWithRefCount() throws Exception {
94 int subCount = 3;
95 final CountDownLatch upperLatch = new CountDownLatch(1);
96 final CountDownLatch lowerLatch = new CountDownLatch(1);
97 final CountDownLatch onNextLatch = new CountDownLatch(subCount);
98
99 final AtomicInteger upperCount = new AtomicInteger();
100 final AtomicInteger lowerCount = new AtomicInteger();
101 Flowable<Long> longs = Flowable
102 // The stream needs to be infinite to ensure the stream does not terminate
103 // before it is unsubscribed
104 .interval(50, TimeUnit.MILLISECONDS)
105 .doOnCancel(new Action() {
106 @Override
107 public void run() {
108 // Test that upper stream will be notified for un-subscription
109 upperLatch.countDown();
110 upperCount.incrementAndGet();
111 }
112 })
113 .doOnNext(new Consumer<Long>() {
114 @Override
115 public void accept(Long aLong) {
116 // Ensure there is at least some onNext events before un-subscription happens
117 onNextLatch.countDown();
118 }
119 })
120 .doOnCancel(new Action() {
121 @Override
122 public void run() {
123 // Test that lower stream will be notified for un-subscription
124 lowerLatch.countDown();
125 lowerCount.incrementAndGet();
126 }
127 })
128 .publish()
129 .refCount();
130
131 List<Disposable> subscriptions = new ArrayList<>();
132 List<TestSubscriber<Long>> subscribers = new ArrayList<>();
133
134 for (int i = 0; i < subCount; ++i) {
135 TestSubscriber<Long> subscriber = new TestSubscriber<>();
136 longs.subscribe(subscriber);
137 subscriptions.add(Disposable.fromSubscription(subscriber));
138 subscribers.add(subscriber);
139 }
140
141 onNextLatch.await();
142 for (int i = 0; i < subCount; ++i) {
143 subscriptions.get(i).dispose();
144 // Test that unsubscribe() method is not affected in any way
145 }
146
147 upperLatch.await();
148 lowerLatch.await();
149 assertEquals("There should exactly 1 un-subscription events for upper stream", 1, upperCount.get());
150 assertEquals("There should exactly 1 un-subscription events for lower stream", 1, lowerCount.get());
151 }
152
153 @Test
154 public void noReentrantDispose() {
155
156 final AtomicInteger cancelCalled = new AtomicInteger();
157
158 final BehaviorProcessor<Integer> p = BehaviorProcessor.create();
159 p.doOnCancel(new Action() {
160 @Override
161 public void run() throws Exception {
162 cancelCalled.incrementAndGet();
163 p.onNext(2);
164 }
165 })
166 .firstOrError()
167 .subscribe()
168 .dispose();
169
170 assertEquals(1, cancelCalled.get());
171 }
172 }