| 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.subscriptions; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.anyLong; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
import org.reactivestreams.Subscription; |
| 22 |
|
| 23 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 24 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 25 |
|
| 26 |
public class AsyncSubscriptionTest extends RxJavaTest { |
| 27 |
@Test |
| 28 |
public void noResource() { |
| 29 |
AsyncSubscription as = new AsyncSubscription(); |
| 30 |
|
| 31 |
Subscription s = mock(Subscription.class); |
| 32 |
|
| 33 |
as.setSubscription(s); |
| 34 |
|
| 35 |
as.request(1); |
| 36 |
|
| 37 |
as.cancel(); |
| 38 |
|
| 39 |
verify(s).request(1); |
| 40 |
verify(s).cancel(); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void requestBeforeSet() { |
| 45 |
AsyncSubscription as = new AsyncSubscription(); |
| 46 |
|
| 47 |
Subscription s = mock(Subscription.class); |
| 48 |
|
| 49 |
as.request(1); |
| 50 |
|
| 51 |
as.setSubscription(s); |
| 52 |
|
| 53 |
as.cancel(); |
| 54 |
|
| 55 |
verify(s).request(1); |
| 56 |
verify(s).cancel(); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void cancelBeforeSet() { |
| 61 |
AsyncSubscription as = new AsyncSubscription(); |
| 62 |
|
| 63 |
Subscription s = mock(Subscription.class); |
| 64 |
|
| 65 |
as.request(1); |
| 66 |
as.cancel(); |
| 67 |
|
| 68 |
as.setSubscription(s); |
| 69 |
|
| 70 |
verify(s, never()).request(1); |
| 71 |
verify(s).cancel(); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void singleSet() { |
| 76 |
AsyncSubscription as = new AsyncSubscription(); |
| 77 |
|
| 78 |
Subscription s = mock(Subscription.class); |
| 79 |
|
| 80 |
as.setSubscription(s); |
| 81 |
|
| 82 |
Subscription s1 = mock(Subscription.class); |
| 83 |
|
| 84 |
as.setSubscription(s1); |
| 85 |
|
| 86 |
assertSame(as.actual.get(), s); |
| 87 |
|
| 88 |
verify(s1).cancel(); |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void initialResource() { |
| 93 |
Disposable r = mock(Disposable.class); |
| 94 |
AsyncSubscription as = new AsyncSubscription(r); |
| 95 |
|
| 96 |
as.cancel(); |
| 97 |
|
| 98 |
verify(r).dispose(); |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
public void setResource() { |
| 103 |
AsyncSubscription as = new AsyncSubscription(); |
| 104 |
|
| 105 |
Disposable r = mock(Disposable.class); |
| 106 |
|
| 107 |
assertTrue(as.setResource(r)); |
| 108 |
|
| 109 |
as.cancel(); |
| 110 |
|
| 111 |
verify(r).dispose(); |
| 112 |
} |
| 113 |
|
| 114 |
@Test |
| 115 |
public void replaceResource() { |
| 116 |
AsyncSubscription as = new AsyncSubscription(); |
| 117 |
|
| 118 |
Disposable r = mock(Disposable.class); |
| 119 |
|
| 120 |
assertTrue(as.replaceResource(r)); |
| 121 |
|
| 122 |
as.cancel(); |
| 123 |
|
| 124 |
verify(r).dispose(); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void setResource2() { |
| 129 |
AsyncSubscription as = new AsyncSubscription(); |
| 130 |
|
| 131 |
Disposable r = mock(Disposable.class); |
| 132 |
|
| 133 |
assertTrue(as.setResource(r)); |
| 134 |
|
| 135 |
Disposable r2 = mock(Disposable.class); |
| 136 |
|
| 137 |
assertTrue(as.setResource(r2)); |
| 138 |
|
| 139 |
as.cancel(); |
| 140 |
|
| 141 |
verify(r).dispose(); |
| 142 |
verify(r2).dispose(); |
| 143 |
} |
| 144 |
|
| 145 |
@Test |
| 146 |
public void replaceResource2() { |
| 147 |
AsyncSubscription as = new AsyncSubscription(); |
| 148 |
|
| 149 |
Disposable r = mock(Disposable.class); |
| 150 |
|
| 151 |
assertTrue(as.replaceResource(r)); |
| 152 |
|
| 153 |
Disposable r2 = mock(Disposable.class); |
| 154 |
|
| 155 |
as.replaceResource(r2); |
| 156 |
|
| 157 |
as.cancel(); |
| 158 |
|
| 159 |
verify(r, never()).dispose(); |
| 160 |
verify(r2).dispose(); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void setResourceAfterCancel() { |
| 165 |
AsyncSubscription as = new AsyncSubscription(); |
| 166 |
|
| 167 |
as.cancel(); |
| 168 |
|
| 169 |
Disposable r = mock(Disposable.class); |
| 170 |
|
| 171 |
as.setResource(r); |
| 172 |
|
| 173 |
verify(r).dispose(); |
| 174 |
} |
| 175 |
|
| 176 |
@Test |
| 177 |
public void replaceResourceAfterCancel() { |
| 178 |
AsyncSubscription as = new AsyncSubscription(); |
| 179 |
as.cancel(); |
| 180 |
|
| 181 |
Disposable r = mock(Disposable.class); |
| 182 |
|
| 183 |
as.replaceResource(r); |
| 184 |
|
| 185 |
verify(r).dispose(); |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void cancelOnce() { |
| 190 |
Disposable r = mock(Disposable.class); |
| 191 |
AsyncSubscription as = new AsyncSubscription(r); |
| 192 |
Subscription s = mock(Subscription.class); |
| 193 |
|
| 194 |
as.setSubscription(s); |
| 195 |
|
| 196 |
as.cancel(); |
| 197 |
as.cancel(); |
| 198 |
as.cancel(); |
| 199 |
|
| 200 |
verify(s, never()).request(anyLong()); |
| 201 |
verify(s).cancel(); |
| 202 |
verify(r).dispose(); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void disposed() { |
| 207 |
AsyncSubscription as = new AsyncSubscription(); |
| 208 |
|
| 209 |
assertFalse(as.isDisposed()); |
| 210 |
|
| 211 |
as.dispose(); |
| 212 |
|
| 213 |
assertTrue(as.isDisposed()); |
| 214 |
} |
| 215 |
} |