| 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 java.lang.ref.WeakReference; |
| 17 |
|
| 18 |
import org.junit.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 22 |
import io.reactivex.rxjava3.functions.Function; |
| 23 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 24 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 25 |
|
| 26 |
public class ObservableDetachTest extends RxJavaTest { |
| 27 |
|
| 28 |
Object o; |
| 29 |
|
| 30 |
@Test |
| 31 |
public void just() throws Exception { |
| 32 |
o = new Object(); |
| 33 |
|
| 34 |
WeakReference<Object> wr = new WeakReference<>(o); |
| 35 |
|
| 36 |
TestObserver<Object> to = new TestObserver<>(); |
| 37 |
|
| 38 |
Observable.just(o).count().toObservable().onTerminateDetach().subscribe(to); |
| 39 |
|
| 40 |
to.assertValue(1L); |
| 41 |
to.assertComplete(); |
| 42 |
to.assertNoErrors(); |
| 43 |
|
| 44 |
o = null; |
| 45 |
|
| 46 |
System.gc(); |
| 47 |
Thread.sleep(200); |
| 48 |
|
| 49 |
Assert.assertNull("Object retained!", wr.get()); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void error() { |
| 55 |
TestObserver<Object> to = new TestObserver<>(); |
| 56 |
|
| 57 |
Observable.error(new TestException()).onTerminateDetach().subscribe(to); |
| 58 |
|
| 59 |
to.assertNoValues(); |
| 60 |
to.assertError(TestException.class); |
| 61 |
to.assertNotComplete(); |
| 62 |
} |
| 63 |
|
| 64 |
@Test |
| 65 |
public void empty() { |
| 66 |
TestObserver<Object> to = new TestObserver<>(); |
| 67 |
|
| 68 |
Observable.empty().onTerminateDetach().subscribe(to); |
| 69 |
|
| 70 |
to.assertNoValues(); |
| 71 |
to.assertNoErrors(); |
| 72 |
to.assertComplete(); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void range() { |
| 77 |
TestObserver<Object> to = new TestObserver<>(); |
| 78 |
|
| 79 |
Observable.range(1, 1000).onTerminateDetach().subscribe(to); |
| 80 |
|
| 81 |
to.assertValueCount(1000); |
| 82 |
to.assertNoErrors(); |
| 83 |
to.assertComplete(); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void justUnsubscribed() throws Exception { |
| 88 |
o = new Object(); |
| 89 |
|
| 90 |
WeakReference<Object> wr = new WeakReference<>(o); |
| 91 |
|
| 92 |
TestObserver<Long> to = Observable.just(o).count().toObservable().onTerminateDetach().test(); |
| 93 |
|
| 94 |
o = null; |
| 95 |
to.dispose(); |
| 96 |
|
| 97 |
System.gc(); |
| 98 |
Thread.sleep(200); |
| 99 |
|
| 100 |
Assert.assertNull("Object retained!", wr.get()); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void dispose() { |
| 106 |
TestHelper.checkDisposed(Observable.never().onTerminateDetach()); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void doubleOnSubscribe() { |
| 111 |
TestHelper.checkDoubleOnSubscribeObservable(new Function<Observable<Object>, ObservableSource<Object>>() { |
| 112 |
@Override |
| 113 |
public ObservableSource<Object> apply(Observable<Object> o) throws Exception { |
| 114 |
return o.onTerminateDetach(); |
| 115 |
} |
| 116 |
}); |
| 117 |
} |
| 118 |
} |