| 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.disposables; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.anyInt; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.util.List; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.reactivestreams.Subscription; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 28 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 29 |
import io.reactivex.rxjava3.functions.Action; |
| 30 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 31 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 32 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 33 |
|
| 34 |
public class DisposableTest extends RxJavaTest { |
| 35 |
|
| 36 |
@Test |
| 37 |
public void unsubscribeOnlyOnce() { |
| 38 |
Runnable run = mock(Runnable.class); |
| 39 |
|
| 40 |
Disposable d = Disposable.fromRunnable(run); |
| 41 |
|
| 42 |
assertTrue(d.toString(), d.toString().contains("RunnableDisposable(disposed=false, ")); |
| 43 |
|
| 44 |
d.dispose(); |
| 45 |
assertTrue(d.toString(), d.toString().contains("RunnableDisposable(disposed=true, ")); |
| 46 |
|
| 47 |
d.dispose(); |
| 48 |
assertTrue(d.toString(), d.toString().contains("RunnableDisposable(disposed=true, ")); |
| 49 |
|
| 50 |
verify(run, times(1)).run(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void empty() { |
| 55 |
Disposable empty = Disposable.empty(); |
| 56 |
assertFalse(empty.isDisposed()); |
| 57 |
empty.dispose(); |
| 58 |
assertTrue(empty.isDisposed()); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void unsubscribed() { |
| 63 |
Disposable disposed = Disposable.disposed(); |
| 64 |
assertTrue(disposed.isDisposed()); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void fromAction() throws Throwable { |
| 69 |
Action action = mock(Action.class); |
| 70 |
|
| 71 |
Disposable d = Disposable.fromAction(action); |
| 72 |
|
| 73 |
assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=false, ")); |
| 74 |
|
| 75 |
d.dispose(); |
| 76 |
assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=true, ")); |
| 77 |
|
| 78 |
d.dispose(); |
| 79 |
assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=true, ")); |
| 80 |
|
| 81 |
verify(action, times(1)).run(); |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void fromActionThrows() { |
| 86 |
try { |
| 87 |
Disposable.fromAction(new Action() { |
| 88 |
@Override |
| 89 |
public void run() throws Exception { |
| 90 |
throw new IllegalArgumentException(); |
| 91 |
} |
| 92 |
}).dispose(); |
| 93 |
fail("Should have thrown!"); |
| 94 |
} catch (IllegalArgumentException ex) { |
| 95 |
// expected |
| 96 |
} |
| 97 |
|
| 98 |
try { |
| 99 |
Disposable.fromAction(new Action() { |
| 100 |
@Override |
| 101 |
public void run() throws Exception { |
| 102 |
throw new InternalError(); |
| 103 |
} |
| 104 |
}).dispose(); |
| 105 |
fail("Should have thrown!"); |
| 106 |
} catch (InternalError ex) { |
| 107 |
// expected |
| 108 |
} |
| 109 |
|
| 110 |
try { |
| 111 |
Disposable.fromAction(new Action() { |
| 112 |
@Override |
| 113 |
public void run() throws Exception { |
| 114 |
throw new IOException(); |
| 115 |
} |
| 116 |
}).dispose(); |
| 117 |
fail("Should have thrown!"); |
| 118 |
} catch (RuntimeException ex) { |
| 119 |
if (!(ex.getCause() instanceof IOException)) { |
| 120 |
fail(ex.toString() + ": Should have cause of IOException"); |
| 121 |
} |
| 122 |
// expected |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void disposeRace() { |
| 129 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 130 |
final Disposable d = Disposable.empty(); |
| 131 |
|
| 132 |
Runnable r = new Runnable() { |
| 133 |
@Override |
| 134 |
public void run() { |
| 135 |
d.dispose(); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
TestHelper.race(r, r); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
@Test(expected = NullPointerException.class) |
| 144 |
public void fromSubscriptionNull() { |
| 145 |
Disposable.fromSubscription(null); |
| 146 |
} |
| 147 |
|
| 148 |
@Test |
| 149 |
public void fromSubscription() { |
| 150 |
Subscription s = mock(Subscription.class); |
| 151 |
|
| 152 |
Disposable.fromSubscription(s).dispose(); |
| 153 |
|
| 154 |
verify(s).cancel(); |
| 155 |
verify(s, never()).request(anyInt()); |
| 156 |
} |
| 157 |
|
| 158 |
@Test |
| 159 |
public void setOnceTwice() { |
| 160 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 161 |
try { |
| 162 |
|
| 163 |
AtomicReference<Disposable> target = new AtomicReference<>(); |
| 164 |
Disposable d = Disposable.empty(); |
| 165 |
|
| 166 |
DisposableHelper.setOnce(target, d); |
| 167 |
|
| 168 |
Disposable d1 = Disposable.empty(); |
| 169 |
|
| 170 |
DisposableHelper.setOnce(target, d1); |
| 171 |
|
| 172 |
assertTrue(d1.isDisposed()); |
| 173 |
|
| 174 |
TestHelper.assertError(errors, 0, IllegalStateException.class, "Disposable already set!"); |
| 175 |
} finally { |
| 176 |
RxJavaPlugins.reset(); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
@Test |
| 181 |
public void fromAutoCloseable() { |
| 182 |
AtomicInteger counter = new AtomicInteger(); |
| 183 |
|
| 184 |
AutoCloseable ac = () -> counter.getAndIncrement(); |
| 185 |
|
| 186 |
Disposable d = Disposable.fromAutoCloseable(ac); |
| 187 |
|
| 188 |
assertFalse(d.isDisposed()); |
| 189 |
assertEquals(0, counter.get()); |
| 190 |
assertTrue(d.toString(), d.toString().contains("AutoCloseableDisposable(disposed=false, ")); |
| 191 |
|
| 192 |
d.dispose(); |
| 193 |
|
| 194 |
assertTrue(d.isDisposed()); |
| 195 |
assertEquals(1, counter.get()); |
| 196 |
assertTrue(d.toString(), d.toString().contains("AutoCloseableDisposable(disposed=true, ")); |
| 197 |
|
| 198 |
d.dispose(); |
| 199 |
|
| 200 |
assertTrue(d.isDisposed()); |
| 201 |
assertEquals(1, counter.get()); |
| 202 |
assertTrue(d.toString(), d.toString().contains("AutoCloseableDisposable(disposed=true, ")); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void fromAutoCloseableThrows() throws Throwable { |
| 207 |
TestHelper.withErrorTracking(errors -> { |
| 208 |
AutoCloseable ac = () -> { throw new TestException(); }; |
| 209 |
|
| 210 |
Disposable d = Disposable.fromAutoCloseable(ac); |
| 211 |
|
| 212 |
assertFalse(d.isDisposed()); |
| 213 |
|
| 214 |
assertTrue(errors.isEmpty()); |
| 215 |
|
| 216 |
try { |
| 217 |
d.dispose(); |
| 218 |
fail("Should have thrown!"); |
| 219 |
} catch (TestException expected) { |
| 220 |
// expected |
| 221 |
} |
| 222 |
|
| 223 |
assertTrue(d.isDisposed()); |
| 224 |
|
| 225 |
d.dispose(); |
| 226 |
|
| 227 |
assertTrue(d.isDisposed()); |
| 228 |
|
| 229 |
assertTrue(errors.isEmpty()); |
| 230 |
}); |
| 231 |
} |
| 232 |
|
| 233 |
@Test |
| 234 |
public void toAutoCloseable() throws Exception { |
| 235 |
AtomicInteger counter = new AtomicInteger(); |
| 236 |
|
| 237 |
Disposable d = Disposable.fromAction(() -> counter.getAndIncrement()); |
| 238 |
|
| 239 |
AutoCloseable ac = Disposable.toAutoCloseable(d); |
| 240 |
|
| 241 |
assertFalse(d.isDisposed()); |
| 242 |
assertEquals(0, counter.get()); |
| 243 |
|
| 244 |
ac.close(); |
| 245 |
|
| 246 |
assertTrue(d.isDisposed()); |
| 247 |
assertEquals(1, counter.get()); |
| 248 |
|
| 249 |
ac.close(); |
| 250 |
|
| 251 |
assertTrue(d.isDisposed()); |
| 252 |
assertEquals(1, counter.get()); |
| 253 |
} |
| 254 |
} |