| 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 java.lang.ref.WeakReference; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import org.junit.*; |
| 20 |
import org.reactivestreams.*; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.functions.Function; |
| 25 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 26 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 27 |
|
| 28 |
public class FlowableDetachTest extends RxJavaTest { |
| 29 |
|
| 30 |
Object o; |
| 31 |
|
| 32 |
@Test |
| 33 |
public void just() throws Exception { |
| 34 |
o = new Object(); |
| 35 |
|
| 36 |
WeakReference<Object> wr = new WeakReference<>(o); |
| 37 |
|
| 38 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 39 |
|
| 40 |
Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts); |
| 41 |
|
| 42 |
ts.assertValue(1L); |
| 43 |
ts.assertComplete(); |
| 44 |
ts.assertNoErrors(); |
| 45 |
|
| 46 |
o = null; |
| 47 |
|
| 48 |
System.gc(); |
| 49 |
Thread.sleep(200); |
| 50 |
|
| 51 |
Assert.assertNull("Object retained!", wr.get()); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
public void error() { |
| 57 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 58 |
|
| 59 |
Flowable.error(new TestException()).onTerminateDetach().subscribe(ts); |
| 60 |
|
| 61 |
ts.assertNoValues(); |
| 62 |
ts.assertError(TestException.class); |
| 63 |
ts.assertNotComplete(); |
| 64 |
} |
| 65 |
|
| 66 |
@Test |
| 67 |
public void empty() { |
| 68 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 69 |
|
| 70 |
Flowable.empty().onTerminateDetach().subscribe(ts); |
| 71 |
|
| 72 |
ts.assertNoValues(); |
| 73 |
ts.assertNoErrors(); |
| 74 |
ts.assertComplete(); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void range() { |
| 79 |
TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 80 |
|
| 81 |
Flowable.range(1, 1000).onTerminateDetach().subscribe(ts); |
| 82 |
|
| 83 |
ts.assertValueCount(1000); |
| 84 |
ts.assertNoErrors(); |
| 85 |
ts.assertComplete(); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void backpressured() throws Exception { |
| 90 |
o = new Object(); |
| 91 |
|
| 92 |
WeakReference<Object> wr = new WeakReference<>(o); |
| 93 |
|
| 94 |
TestSubscriber<Object> ts = new TestSubscriber<>(0L); |
| 95 |
|
| 96 |
Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts); |
| 97 |
|
| 98 |
ts.assertNoValues(); |
| 99 |
|
| 100 |
ts.request(1); |
| 101 |
|
| 102 |
ts.assertValue(1L); |
| 103 |
ts.assertComplete(); |
| 104 |
ts.assertNoErrors(); |
| 105 |
|
| 106 |
o = null; |
| 107 |
|
| 108 |
System.gc(); |
| 109 |
Thread.sleep(200); |
| 110 |
|
| 111 |
Assert.assertNull("Object retained!", wr.get()); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void justUnsubscribed() throws Exception { |
| 116 |
o = new Object(); |
| 117 |
|
| 118 |
WeakReference<Object> wr = new WeakReference<>(o); |
| 119 |
|
| 120 |
TestSubscriber<Object> ts = new TestSubscriber<>(0); |
| 121 |
|
| 122 |
Flowable.just(o).count().toFlowable().onTerminateDetach().subscribe(ts); |
| 123 |
|
| 124 |
ts.cancel(); |
| 125 |
o = null; |
| 126 |
|
| 127 |
System.gc(); |
| 128 |
Thread.sleep(200); |
| 129 |
|
| 130 |
Assert.assertNull("Object retained!", wr.get()); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
@Test |
| 135 |
public void deferredUpstreamProducer() { |
| 136 |
final AtomicReference<Subscriber<? super Object>> subscriber = new AtomicReference<>(); |
| 137 |
|
| 138 |
TestSubscriber<Object> ts = new TestSubscriber<>(0); |
| 139 |
|
| 140 |
Flowable.unsafeCreate(new Publisher<Object>() { |
| 141 |
@Override |
| 142 |
public void subscribe(Subscriber<? super Object> t) { |
| 143 |
subscriber.set(t); |
| 144 |
} |
| 145 |
}).onTerminateDetach().subscribe(ts); |
| 146 |
|
| 147 |
ts.request(2); |
| 148 |
|
| 149 |
new FlowableRange(1, 3).subscribe(subscriber.get()); |
| 150 |
|
| 151 |
ts.assertValues(1, 2); |
| 152 |
|
| 153 |
ts.request(1); |
| 154 |
|
| 155 |
ts.assertValues(1, 2, 3); |
| 156 |
ts.assertComplete(); |
| 157 |
ts.assertNoErrors(); |
| 158 |
} |
| 159 |
|
| 160 |
@Test |
| 161 |
public void dispose() { |
| 162 |
TestHelper.checkDisposed(Flowable.never().onTerminateDetach()); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void doubleOnSubscribe() { |
| 167 |
TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() { |
| 168 |
@Override |
| 169 |
public Flowable<Object> apply(Flowable<Object> f) throws Exception { |
| 170 |
return f.onTerminateDetach(); |
| 171 |
} |
| 172 |
}); |
| 173 |
} |
| 174 |
} |