| 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.util; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
import java.io.IOException; |
| 19 |
import java.util.concurrent.atomic.AtomicInteger; |
| 20 |
|
| 21 |
import org.junit.Test; |
| 22 |
import org.reactivestreams.*; |
| 23 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription; |
| 27 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 28 |
import io.reactivex.rxjava3.testsupport.*; |
| 29 |
|
| 30 |
public class HalfSerializerSubscriberTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void utilityClass() { |
| 34 |
TestHelper.checkUtilityClass(HalfSerializer.class); |
| 35 |
} |
| 36 |
|
| 37 |
@Test |
| 38 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 39 |
public void reentrantOnNextOnNext() { |
| 40 |
final AtomicInteger wip = new AtomicInteger(); |
| 41 |
final AtomicThrowable error = new AtomicThrowable(); |
| 42 |
|
| 43 |
final Subscriber[] a = { null }; |
| 44 |
|
| 45 |
final TestSubscriber ts = new TestSubscriber(); |
| 46 |
|
| 47 |
FlowableSubscriber s = new FlowableSubscriber() { |
| 48 |
@Override |
| 49 |
public void onSubscribe(Subscription s) { |
| 50 |
ts.onSubscribe(s); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void onNext(Object t) { |
| 55 |
if (t.equals(1)) { |
| 56 |
HalfSerializer.onNext(a[0], 2, wip, error); |
| 57 |
} |
| 58 |
ts.onNext(t); |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void onError(Throwable t) { |
| 63 |
ts.onError(t); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void onComplete() { |
| 68 |
ts.onComplete(); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
a[0] = s; |
| 73 |
|
| 74 |
s.onSubscribe(new BooleanSubscription()); |
| 75 |
|
| 76 |
HalfSerializer.onNext(s, 1, wip, error); |
| 77 |
|
| 78 |
ts.assertValue(1).assertNoErrors().assertNotComplete(); |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 83 |
public void reentrantOnNextOnError() { |
| 84 |
final AtomicInteger wip = new AtomicInteger(); |
| 85 |
final AtomicThrowable error = new AtomicThrowable(); |
| 86 |
|
| 87 |
final Subscriber[] a = { null }; |
| 88 |
|
| 89 |
final TestSubscriber ts = new TestSubscriber(); |
| 90 |
|
| 91 |
FlowableSubscriber s = new FlowableSubscriber() { |
| 92 |
@Override |
| 93 |
public void onSubscribe(Subscription s) { |
| 94 |
ts.onSubscribe(s); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onNext(Object t) { |
| 99 |
if (t.equals(1)) { |
| 100 |
HalfSerializer.onError(a[0], new TestException(), wip, error); |
| 101 |
} |
| 102 |
ts.onNext(t); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onError(Throwable t) { |
| 107 |
ts.onError(t); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void onComplete() { |
| 112 |
ts.onComplete(); |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
a[0] = s; |
| 117 |
|
| 118 |
s.onSubscribe(new BooleanSubscription()); |
| 119 |
|
| 120 |
HalfSerializer.onNext(s, 1, wip, error); |
| 121 |
|
| 122 |
ts.assertFailure(TestException.class, 1); |
| 123 |
} |
| 124 |
|
| 125 |
@Test |
| 126 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 127 |
public void reentrantOnNextOnComplete() { |
| 128 |
final AtomicInteger wip = new AtomicInteger(); |
| 129 |
final AtomicThrowable error = new AtomicThrowable(); |
| 130 |
|
| 131 |
final Subscriber[] a = { null }; |
| 132 |
|
| 133 |
final TestSubscriber ts = new TestSubscriber(); |
| 134 |
|
| 135 |
FlowableSubscriber s = new FlowableSubscriber() { |
| 136 |
@Override |
| 137 |
public void onSubscribe(Subscription s) { |
| 138 |
ts.onSubscribe(s); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void onNext(Object t) { |
| 143 |
if (t.equals(1)) { |
| 144 |
HalfSerializer.onComplete(a[0], wip, error); |
| 145 |
} |
| 146 |
ts.onNext(t); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onError(Throwable t) { |
| 151 |
ts.onError(t); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void onComplete() { |
| 156 |
ts.onComplete(); |
| 157 |
} |
| 158 |
}; |
| 159 |
|
| 160 |
a[0] = s; |
| 161 |
|
| 162 |
s.onSubscribe(new BooleanSubscription()); |
| 163 |
|
| 164 |
HalfSerializer.onNext(s, 1, wip, error); |
| 165 |
|
| 166 |
ts.assertResult(1); |
| 167 |
} |
| 168 |
|
| 169 |
@Test |
| 170 |
@SuppressUndeliverable |
| 171 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 172 |
public void reentrantErrorOnError() { |
| 173 |
final AtomicInteger wip = new AtomicInteger(); |
| 174 |
final AtomicThrowable error = new AtomicThrowable(); |
| 175 |
|
| 176 |
final Subscriber[] a = { null }; |
| 177 |
|
| 178 |
final TestSubscriber ts = new TestSubscriber(); |
| 179 |
|
| 180 |
FlowableSubscriber s = new FlowableSubscriber() { |
| 181 |
@Override |
| 182 |
public void onSubscribe(Subscription s) { |
| 183 |
ts.onSubscribe(s); |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public void onNext(Object t) { |
| 188 |
ts.onNext(t); |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public void onError(Throwable t) { |
| 193 |
ts.onError(t); |
| 194 |
HalfSerializer.onError(a[0], new IOException(), wip, error); |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public void onComplete() { |
| 199 |
ts.onComplete(); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
a[0] = s; |
| 204 |
|
| 205 |
s.onSubscribe(new BooleanSubscription()); |
| 206 |
|
| 207 |
HalfSerializer.onError(s, new TestException(), wip, error); |
| 208 |
|
| 209 |
ts.assertFailure(TestException.class); |
| 210 |
} |
| 211 |
|
| 212 |
@Test |
| 213 |
public void onNextOnCompleteRace() { |
| 214 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 215 |
|
| 216 |
final AtomicInteger wip = new AtomicInteger(); |
| 217 |
final AtomicThrowable error = new AtomicThrowable(); |
| 218 |
|
| 219 |
final TestSubscriber<Integer> ts = new TestSubscriber<>(); |
| 220 |
ts.onSubscribe(new BooleanSubscription()); |
| 221 |
|
| 222 |
Runnable r1 = new Runnable() { |
| 223 |
@Override |
| 224 |
public void run() { |
| 225 |
HalfSerializer.onNext(ts, 1, wip, error); |
| 226 |
} |
| 227 |
}; |
| 228 |
|
| 229 |
Runnable r2 = new Runnable() { |
| 230 |
@Override |
| 231 |
public void run() { |
| 232 |
HalfSerializer.onComplete(ts, wip, error); |
| 233 |
} |
| 234 |
}; |
| 235 |
|
| 236 |
TestHelper.race(r1, r2); |
| 237 |
|
| 238 |
ts.assertComplete().assertNoErrors(); |
| 239 |
|
| 240 |
assertTrue(ts.values().size() <= 1); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
@Test |
| 245 |
@SuppressUndeliverable |
| 246 |
public void onErrorOnCompleteRace() { |
| 247 |
for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { |
| 248 |
|
| 249 |
final AtomicInteger wip = new AtomicInteger(); |
| 250 |
final AtomicThrowable error = new AtomicThrowable(); |
| 251 |
|
| 252 |
final TestSubscriberEx<Integer> ts = new TestSubscriberEx<>(); |
| 253 |
|
| 254 |
ts.onSubscribe(new BooleanSubscription()); |
| 255 |
|
| 256 |
final TestException ex = new TestException(); |
| 257 |
|
| 258 |
Runnable r1 = new Runnable() { |
| 259 |
@Override |
| 260 |
public void run() { |
| 261 |
HalfSerializer.onError(ts, ex, wip, error); |
| 262 |
} |
| 263 |
}; |
| 264 |
|
| 265 |
Runnable r2 = new Runnable() { |
| 266 |
@Override |
| 267 |
public void run() { |
| 268 |
HalfSerializer.onComplete(ts, wip, error); |
| 269 |
} |
| 270 |
}; |
| 271 |
|
| 272 |
TestHelper.race(r1, r2); |
| 273 |
|
| 274 |
if (ts.completions() != 0) { |
| 275 |
ts.assertResult(); |
| 276 |
} else { |
| 277 |
ts.assertFailure(TestException.class); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
} |