| 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.single; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertNull; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.lang.ref.WeakReference; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.functions.Function; |
| 27 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 28 |
import io.reactivex.rxjava3.processors.PublishProcessor; |
| 29 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 30 |
|
| 31 |
public class SingleDetachTest extends RxJavaTest { |
| 32 |
|
| 33 |
@Test |
| 34 |
public void doubleSubscribe() { |
| 35 |
|
| 36 |
TestHelper.checkDoubleOnSubscribeSingle(new Function<Single<Object>, SingleSource<Object>>() { |
| 37 |
@Override |
| 38 |
public SingleSource<Object> apply(Single<Object> m) throws Exception { |
| 39 |
return m.onTerminateDetach(); |
| 40 |
} |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void dispose() { |
| 46 |
TestHelper.checkDisposed(PublishProcessor.create().singleOrError().onTerminateDetach()); |
| 47 |
} |
| 48 |
|
| 49 |
@Test |
| 50 |
public void onError() { |
| 51 |
Single.error(new TestException()) |
| 52 |
.onTerminateDetach() |
| 53 |
.test() |
| 54 |
.assertFailure(TestException.class); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void onSuccess() { |
| 59 |
Single.just(1) |
| 60 |
.onTerminateDetach() |
| 61 |
.test() |
| 62 |
.assertResult(1); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void cancelDetaches() throws Exception { |
| 67 |
Disposable d = Disposable.empty(); |
| 68 |
final WeakReference<Disposable> wr = new WeakReference<>(d); |
| 69 |
|
| 70 |
TestObserver<Object> to = new Single<Object>() { |
| 71 |
@Override |
| 72 |
protected void subscribeActual(SingleObserver<? super Object> observer) { |
| 73 |
observer.onSubscribe(wr.get()); |
| 74 |
}; |
| 75 |
} |
| 76 |
.onTerminateDetach() |
| 77 |
.test(); |
| 78 |
|
| 79 |
d = null; |
| 80 |
|
| 81 |
to.dispose(); |
| 82 |
|
| 83 |
System.gc(); |
| 84 |
Thread.sleep(200); |
| 85 |
|
| 86 |
to.assertEmpty(); |
| 87 |
|
| 88 |
assertNull(wr.get()); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void errorDetaches() throws Exception { |
| 93 |
Disposable d = Disposable.empty(); |
| 94 |
final WeakReference<Disposable> wr = new WeakReference<>(d); |
| 95 |
|
| 96 |
TestObserver<Integer> to = new Single<Integer>() { |
| 97 |
@Override |
| 98 |
protected void subscribeActual(SingleObserver<? super Integer> observer) { |
| 99 |
observer.onSubscribe(wr.get()); |
| 100 |
observer.onError(new TestException()); |
| 101 |
observer.onError(new IOException()); |
| 102 |
}; |
| 103 |
} |
| 104 |
.onTerminateDetach() |
| 105 |
.test(); |
| 106 |
|
| 107 |
d = null; |
| 108 |
|
| 109 |
System.gc(); |
| 110 |
Thread.sleep(200); |
| 111 |
|
| 112 |
to.assertFailure(TestException.class); |
| 113 |
|
| 114 |
assertNull(wr.get()); |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void successDetaches() throws Exception { |
| 119 |
Disposable d = Disposable.empty(); |
| 120 |
final WeakReference<Disposable> wr = new WeakReference<>(d); |
| 121 |
|
| 122 |
TestObserver<Integer> to = new Single<Integer>() { |
| 123 |
@Override |
| 124 |
protected void subscribeActual(SingleObserver<? super Integer> observer) { |
| 125 |
observer.onSubscribe(wr.get()); |
| 126 |
observer.onSuccess(1); |
| 127 |
observer.onSuccess(2); |
| 128 |
}; |
| 129 |
} |
| 130 |
.onTerminateDetach() |
| 131 |
.test(); |
| 132 |
|
| 133 |
d = null; |
| 134 |
|
| 135 |
System.gc(); |
| 136 |
Thread.sleep(200); |
| 137 |
|
| 138 |
to.assertResult(1); |
| 139 |
|
| 140 |
assertNull(wr.get()); |
| 141 |
} |
| 142 |
} |