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