|
20 | 20 |
|
21 | 21 | import java.util.Arrays;
|
22 | 22 | import java.util.List;
|
| 23 | +import java.util.concurrent.CopyOnWriteArrayList; |
23 | 24 | import java.util.concurrent.TimeUnit;
|
24 | 25 |
|
25 | 26 | import org.junit.Test;
|
26 | 27 |
|
27 | 28 | import rx.*;
|
| 29 | +import rx.Observable.OnSubscribe; |
| 30 | +import rx.functions.Action1; |
28 | 31 | import rx.functions.Func1;
|
29 | 32 | import rx.observers.TestSubscriber;
|
| 33 | +import rx.plugins.RxJavaHooks; |
30 | 34 |
|
31 | 35 | public class OperatorAllTest {
|
32 | 36 |
|
@@ -178,4 +182,114 @@ public Boolean call(Object object) {
|
178 | 182 | assertEquals(ex, errors.get(0));
|
179 | 183 | assertTrue(ex.getCause().getMessage().contains("Boo!"));
|
180 | 184 | }
|
| 185 | + |
| 186 | + @Test |
| 187 | + public void testDoesNotEmitMultipleTerminalEvents() { |
| 188 | + TestSubscriber<Boolean> ts = TestSubscriber.create(); |
| 189 | + Observable.create(new OnSubscribe<Integer>() { |
| 190 | + |
| 191 | + @Override |
| 192 | + public void call(final Subscriber<? super Integer> sub) { |
| 193 | + sub.setProducer(new Producer() { |
| 194 | + |
| 195 | + @Override |
| 196 | + public void request(long n) { |
| 197 | + if (n > 0) { |
| 198 | + sub.onNext(1); |
| 199 | + sub.onCompleted(); |
| 200 | + } |
| 201 | + } |
| 202 | + }); |
| 203 | + } |
| 204 | + }) |
| 205 | + .all(new Func1<Integer,Boolean>() { |
| 206 | + |
| 207 | + @Override |
| 208 | + public Boolean call(Integer t) { |
| 209 | + throw new RuntimeException("boo"); |
| 210 | + }}) |
| 211 | + .unsafeSubscribe(ts); |
| 212 | + ts.assertError(RuntimeException.class); |
| 213 | + ts.assertNotCompleted(); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void testUpstreamEmitsOnNextAfterFailureWithoutCheckingSubscription() { |
| 218 | + TestSubscriber<Boolean> ts = TestSubscriber.create(); |
| 219 | + Observable.create(new OnSubscribe<Integer>() { |
| 220 | + |
| 221 | + @Override |
| 222 | + public void call(final Subscriber<? super Integer> sub) { |
| 223 | + sub.setProducer(new Producer() { |
| 224 | + |
| 225 | + @Override |
| 226 | + public void request(long n) { |
| 227 | + if (n > 1) { |
| 228 | + sub.onNext(1); |
| 229 | + sub.onNext(2); |
| 230 | + } |
| 231 | + } |
| 232 | + }); |
| 233 | + } |
| 234 | + }) |
| 235 | + .all(new Func1<Integer,Boolean>() { |
| 236 | + boolean once = true; |
| 237 | + @Override |
| 238 | + public Boolean call(Integer t) { |
| 239 | + if (once) |
| 240 | + throw new RuntimeException("boo"); |
| 241 | + else { |
| 242 | + once = false; |
| 243 | + return true; |
| 244 | + } |
| 245 | + }}) |
| 246 | + .unsafeSubscribe(ts); |
| 247 | + ts.assertNoValues(); |
| 248 | + ts.assertError(RuntimeException.class); |
| 249 | + ts.assertNotCompleted(); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void testDoesNotEmitMultipleErrorEventsAndReportsSecondErrorToHooks() { |
| 254 | + try { |
| 255 | + final List<Throwable> list = new CopyOnWriteArrayList<Throwable>(); |
| 256 | + RxJavaHooks.setOnError(new Action1<Throwable>() { |
| 257 | + |
| 258 | + @Override |
| 259 | + public void call(Throwable t) { |
| 260 | + list.add(t); |
| 261 | + } |
| 262 | + }); |
| 263 | + TestSubscriber<Boolean> ts = TestSubscriber.create(); |
| 264 | + final RuntimeException e1 = new RuntimeException(); |
| 265 | + final Throwable e2 = new RuntimeException(); |
| 266 | + Observable.create(new OnSubscribe<Integer>() { |
| 267 | + |
| 268 | + @Override |
| 269 | + public void call(final Subscriber<? super Integer> sub) { |
| 270 | + sub.setProducer(new Producer() { |
| 271 | + |
| 272 | + @Override |
| 273 | + public void request(long n) { |
| 274 | + if (n > 0) { |
| 275 | + sub.onNext(1); |
| 276 | + sub.onError(e2); |
| 277 | + } |
| 278 | + } |
| 279 | + }); |
| 280 | + } |
| 281 | + }).all(new Func1<Integer, Boolean>() { |
| 282 | + |
| 283 | + @Override |
| 284 | + public Boolean call(Integer t) { |
| 285 | + throw e1; |
| 286 | + } |
| 287 | + }).unsafeSubscribe(ts); |
| 288 | + ts.assertNotCompleted(); |
| 289 | + assertEquals(Arrays.asList(e1), ts.getOnErrorEvents()); |
| 290 | + assertEquals(Arrays.asList(e2), list); |
| 291 | + } finally { |
| 292 | + RxJavaHooks.setOnError(null); |
| 293 | + } |
| 294 | + } |
181 | 295 | }
|
0 commit comments