Debian Codegraph @

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