|
15 | 15 | */
|
16 | 16 | package rx.operators;
|
17 | 17 |
|
18 |
| -import static org.mockito.Matchers.*; |
19 |
| -import static org.mockito.Mockito.*; |
20 |
| - |
21 | 18 | import java.util.concurrent.TimeUnit;
|
22 | 19 | import java.util.concurrent.atomic.AtomicReference;
|
23 | 20 |
|
|
30 | 27 | import rx.Subscription;
|
31 | 28 | import rx.concurrency.TestScheduler;
|
32 | 29 | import rx.subscriptions.Subscriptions;
|
| 30 | +import rx.util.AtomicObservableSubscription; |
33 | 31 | import rx.util.functions.Action0;
|
34 | 32 | import rx.util.functions.Func1;
|
35 | 33 |
|
| 34 | +import static org.mockito.Mockito.inOrder; |
| 35 | +import static org.mockito.Mockito.mock; |
| 36 | +import static org.mockito.Mockito.never; |
| 37 | +import static org.mockito.Mockito.times; |
| 38 | +import static org.mockito.Mockito.verify; |
| 39 | + |
| 40 | +import static org.mockito.Matchers.any; |
| 41 | +import static org.mockito.Matchers.anyString; |
| 42 | + |
36 | 43 |
|
37 | 44 | /**
|
38 | 45 | * This operation transforms an {@link Observable} sequence of {@link Observable} sequences into a single
|
@@ -67,54 +74,64 @@ public Switch(Observable<Observable<T>> sequences) {
|
67 | 74 |
|
68 | 75 | @Override
|
69 | 76 | public Subscription call(Observer<T> observer) {
|
70 |
| - return sequences.subscribe(new SwitchObserver<T>(observer)); |
| 77 | + AtomicObservableSubscription subscription = new AtomicObservableSubscription(); |
| 78 | + subscription.wrap(sequences.subscribe(new SwitchObserver<T>(observer, subscription))); |
| 79 | + return subscription; |
71 | 80 | }
|
72 | 81 | }
|
73 | 82 |
|
74 | 83 | private static class SwitchObserver<T> implements Observer<Observable<T>> {
|
75 | 84 |
|
76 |
| - private final AtomicReference<Subscription> subscription = new AtomicReference<Subscription>(); |
77 |
| - |
78 | 85 | private final Observer<T> observer;
|
| 86 | + private final AtomicObservableSubscription parent; |
| 87 | + private final AtomicReference<Subscription> subsequence = new AtomicReference<Subscription>(); |
79 | 88 |
|
80 |
| - public SwitchObserver(Observer<T> observer) { |
| 89 | + public SwitchObserver(Observer<T> observer, AtomicObservableSubscription parent) { |
81 | 90 | this.observer = observer;
|
| 91 | + this.parent = parent; |
82 | 92 | }
|
83 | 93 |
|
84 | 94 | @Override
|
85 | 95 | public void onCompleted() {
|
| 96 | + unsubscribeFromSubSequence(); |
86 | 97 | observer.onCompleted();
|
87 | 98 | }
|
88 | 99 |
|
89 | 100 | @Override
|
90 | 101 | public void onError(Exception e) {
|
| 102 | + unsubscribeFromSubSequence(); |
91 | 103 | observer.onError(e);
|
92 | 104 | }
|
93 | 105 |
|
94 | 106 | @Override
|
95 | 107 | public void onNext(Observable<T> args) {
|
96 |
| - Subscription previousSubscription = subscription.get(); |
97 |
| - if (previousSubscription != null) { |
98 |
| - previousSubscription.unsubscribe(); |
99 |
| - } |
| 108 | + unsubscribeFromSubSequence(); |
100 | 109 |
|
101 |
| - subscription.set(args.subscribe(new Observer<T>() { |
| 110 | + subsequence.set(args.subscribe(new Observer<T>() { |
102 | 111 | @Override
|
103 | 112 | public void onCompleted() {
|
104 |
| - // Do nothing. |
| 113 | + // Do nothing. |
105 | 114 | }
|
106 | 115 |
|
107 | 116 | @Override
|
108 | 117 | public void onError(Exception e) {
|
109 |
| - // Do nothing. |
| 118 | + parent.unsubscribe(); |
| 119 | + observer.onError(e); |
110 | 120 | }
|
111 | 121 |
|
112 |
| - @Override |
| 122 | + @Override |
113 | 123 | public void onNext(T args) {
|
114 | 124 | observer.onNext(args);
|
115 | 125 | }
|
116 | 126 | }));
|
117 | 127 | }
|
| 128 | + |
| 129 | + private void unsubscribeFromSubSequence() { |
| 130 | + Subscription previousSubscription = subsequence.get(); |
| 131 | + if (previousSubscription != null) { |
| 132 | + previousSubscription.unsubscribe(); |
| 133 | + } |
| 134 | + } |
118 | 135 | }
|
119 | 136 |
|
120 | 137 | public static class UnitTest {
|
@@ -299,7 +316,6 @@ public Subscription call(Observer<String> observer) {
|
299 | 316 | verify(observer, never()).onError(any(Exception.class));
|
300 | 317 |
|
301 | 318 | scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
|
302 |
| - inOrder.verify(observer, times(1)).onNext("two"); |
303 | 319 | inOrder.verify(observer, times(1)).onNext("three");
|
304 | 320 | verify(observer, never()).onCompleted();
|
305 | 321 | verify(observer, never()).onError(any(Exception.class));
|
@@ -355,10 +371,9 @@ public Subscription call(Observer<String> observer) {
|
355 | 371 | verify(observer, never()).onError(any(Exception.class));
|
356 | 372 |
|
357 | 373 | scheduler.advanceTimeTo(250, TimeUnit.MILLISECONDS);
|
358 |
| - inOrder.verify(observer, times(1)).onNext("two"); |
359 |
| - inOrder.verify(observer, times(1)).onNext("three"); |
| 374 | + inOrder.verify(observer, never()).onNext("three"); |
360 | 375 | verify(observer, never()).onCompleted();
|
361 |
| - verify(observer, never()).onError(any(Exception.class)); |
| 376 | + verify(observer, times(1)).onError(any(TestException.class)); |
362 | 377 | }
|
363 | 378 |
|
364 | 379 | private <T> void publishCompleted(final Observer<T> observer, long delay) {
|
|
0 commit comments