| 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.Mockito.*; |
| 18 |
|
| 19 |
import java.util.*; |
| 20 |
import java.util.concurrent.CountDownLatch; |
| 21 |
|
| 22 |
import org.junit.*; |
| 23 |
import org.junit.runner.RunWith; |
| 24 |
import org.mockito.junit.MockitoJUnitRunner; |
| 25 |
|
| 26 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 27 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 28 |
|
| 29 |
@RunWith(MockitoJUnitRunner.class) |
| 30 |
public class SerialDisposableTests extends RxJavaTest { |
| 31 |
private SerialDisposable serialDisposable; |
| 32 |
|
| 33 |
@Before |
| 34 |
public void setUp() { |
| 35 |
serialDisposable = new SerialDisposable(); |
| 36 |
} |
| 37 |
|
| 38 |
@Test |
| 39 |
public void unsubscribingWithoutUnderlyingDoesNothing() { |
| 40 |
serialDisposable.dispose(); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void getDisposableShouldReturnset() { |
| 45 |
final Disposable underlying = mock(Disposable.class); |
| 46 |
serialDisposable.set(underlying); |
| 47 |
assertSame(underlying, serialDisposable.get()); |
| 48 |
|
| 49 |
final Disposable another = mock(Disposable.class); |
| 50 |
serialDisposable.set(another); |
| 51 |
assertSame(another, serialDisposable.get()); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void notDisposedWhenReplaced() { |
| 56 |
final Disposable underlying = mock(Disposable.class); |
| 57 |
serialDisposable.set(underlying); |
| 58 |
|
| 59 |
serialDisposable.replace(Disposable.empty()); |
| 60 |
serialDisposable.dispose(); |
| 61 |
|
| 62 |
verify(underlying, never()).dispose(); |
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void unsubscribingTwiceDoesUnsubscribeOnce() { |
| 67 |
Disposable underlying = mock(Disposable.class); |
| 68 |
serialDisposable.set(underlying); |
| 69 |
|
| 70 |
serialDisposable.dispose(); |
| 71 |
verify(underlying).dispose(); |
| 72 |
|
| 73 |
serialDisposable.dispose(); |
| 74 |
verifyNoMoreInteractions(underlying); |
| 75 |
} |
| 76 |
|
| 77 |
@Test |
| 78 |
public void settingSameDisposableTwiceDoesUnsubscribeIt() { |
| 79 |
Disposable underlying = mock(Disposable.class); |
| 80 |
serialDisposable.set(underlying); |
| 81 |
verifyNoInteractions(underlying); |
| 82 |
serialDisposable.set(underlying); |
| 83 |
verify(underlying).dispose(); |
| 84 |
} |
| 85 |
|
| 86 |
@Test |
| 87 |
public void unsubscribingWithSingleUnderlyingUnsubscribes() { |
| 88 |
Disposable underlying = mock(Disposable.class); |
| 89 |
serialDisposable.set(underlying); |
| 90 |
underlying.dispose(); |
| 91 |
verify(underlying).dispose(); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void replacingFirstUnderlyingCausesUnsubscription() { |
| 96 |
Disposable first = mock(Disposable.class); |
| 97 |
serialDisposable.set(first); |
| 98 |
Disposable second = mock(Disposable.class); |
| 99 |
serialDisposable.set(second); |
| 100 |
verify(first).dispose(); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void whenUnsubscribingSecondUnderlyingUnsubscribed() { |
| 105 |
Disposable first = mock(Disposable.class); |
| 106 |
serialDisposable.set(first); |
| 107 |
Disposable second = mock(Disposable.class); |
| 108 |
serialDisposable.set(second); |
| 109 |
serialDisposable.dispose(); |
| 110 |
verify(second).dispose(); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscription() { |
| 115 |
serialDisposable.dispose(); |
| 116 |
Disposable underlying = mock(Disposable.class); |
| 117 |
serialDisposable.set(underlying); |
| 118 |
verify(underlying).dispose(); |
| 119 |
} |
| 120 |
|
| 121 |
@Test |
| 122 |
public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscriptionConcurrently() |
| 123 |
throws InterruptedException { |
| 124 |
final Disposable firstSet = mock(Disposable.class); |
| 125 |
serialDisposable.set(firstSet); |
| 126 |
|
| 127 |
final CountDownLatch start = new CountDownLatch(1); |
| 128 |
|
| 129 |
final int count = 10; |
| 130 |
final CountDownLatch end = new CountDownLatch(count); |
| 131 |
|
| 132 |
final List<Thread> threads = new ArrayList<>(); |
| 133 |
for (int i = 0; i < count; i++) { |
| 134 |
final Thread t = new Thread() { |
| 135 |
@Override |
| 136 |
public void run() { |
| 137 |
try { |
| 138 |
start.await(); |
| 139 |
serialDisposable.dispose(); |
| 140 |
} catch (InterruptedException e) { |
| 141 |
fail(e.getMessage()); |
| 142 |
} finally { |
| 143 |
end.countDown(); |
| 144 |
} |
| 145 |
} |
| 146 |
}; |
| 147 |
t.start(); |
| 148 |
threads.add(t); |
| 149 |
} |
| 150 |
|
| 151 |
final Disposable underlying = mock(Disposable.class); |
| 152 |
start.countDown(); |
| 153 |
serialDisposable.set(underlying); |
| 154 |
end.await(); |
| 155 |
verify(firstSet).dispose(); |
| 156 |
verify(underlying).dispose(); |
| 157 |
|
| 158 |
for (final Thread t : threads) { |
| 159 |
t.join(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void concurrentSetDisposableShouldNotInterleave() |
| 165 |
throws InterruptedException { |
| 166 |
final int count = 10; |
| 167 |
final List<Disposable> subscriptions = new ArrayList<>(); |
| 168 |
|
| 169 |
final CountDownLatch start = new CountDownLatch(1); |
| 170 |
final CountDownLatch end = new CountDownLatch(count); |
| 171 |
|
| 172 |
final List<Thread> threads = new ArrayList<>(); |
| 173 |
for (int i = 0; i < count; i++) { |
| 174 |
final Disposable subscription = mock(Disposable.class); |
| 175 |
subscriptions.add(subscription); |
| 176 |
|
| 177 |
final Thread t = new Thread() { |
| 178 |
@Override |
| 179 |
public void run() { |
| 180 |
try { |
| 181 |
start.await(); |
| 182 |
serialDisposable.set(subscription); |
| 183 |
} catch (InterruptedException e) { |
| 184 |
fail(e.getMessage()); |
| 185 |
} finally { |
| 186 |
end.countDown(); |
| 187 |
} |
| 188 |
} |
| 189 |
}; |
| 190 |
t.start(); |
| 191 |
threads.add(t); |
| 192 |
} |
| 193 |
|
| 194 |
start.countDown(); |
| 195 |
end.await(); |
| 196 |
serialDisposable.dispose(); |
| 197 |
|
| 198 |
for (final Disposable subscription : subscriptions) { |
| 199 |
verify(subscription).dispose(); |
| 200 |
} |
| 201 |
|
| 202 |
for (final Thread t : threads) { |
| 203 |
t.join(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
@Test |
| 208 |
public void disposeState() { |
| 209 |
Disposable empty = Disposable.empty(); |
| 210 |
SerialDisposable d = new SerialDisposable(empty); |
| 211 |
|
| 212 |
assertFalse(d.isDisposed()); |
| 213 |
|
| 214 |
assertSame(empty, d.get()); |
| 215 |
|
| 216 |
d.dispose(); |
| 217 |
|
| 218 |
assertTrue(d.isDisposed()); |
| 219 |
|
| 220 |
assertNotSame(empty, d.get()); |
| 221 |
|
| 222 |
assertNotSame(DisposableHelper.DISPOSED, d.get()); |
| 223 |
} |
| 224 |
} |