|
1 | 1 | /**
|
2 | 2 | * Copyright 2013 Netflix, Inc.
|
3 |
| - * |
| 3 | + * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 |
| - * |
| 7 | + * |
8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 |
| - * |
| 9 | + * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15 | 15 | */
|
16 | 16 | package rx.subjects;
|
17 | 17 |
|
| 18 | +import static org.mockito.Matchers.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
18 | 21 | import java.util.ArrayList;
|
19 | 22 | import java.util.List;
|
20 | 23 | import java.util.concurrent.ConcurrentHashMap;
|
|
24 | 27 | import junit.framework.Assert;
|
25 | 28 |
|
26 | 29 | import org.junit.Test;
|
| 30 | +import org.mockito.Mockito; |
27 | 31 |
|
28 | 32 | import rx.Notification;
|
29 | 33 | import rx.Observable;
|
|
32 | 36 | import rx.util.AtomicObservableSubscription;
|
33 | 37 | import rx.util.SynchronizedObserver;
|
34 | 38 | import rx.util.functions.Action1;
|
| 39 | +import rx.util.functions.Func0; |
35 | 40 | import rx.util.functions.Func1;
|
36 | 41 |
|
37 | 42 | public class DefaultSubject<T> extends Subject<T, T> {
|
@@ -137,5 +142,183 @@ public void unsubscribe() {
|
137 | 142 |
|
138 | 143 | sub.unsubscribe();
|
139 | 144 | }
|
| 145 | + |
| 146 | + private final Exception testException = new Exception(); |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testCompleted() { |
| 150 | + DefaultSubject<Object> subject = DefaultSubject.create(); |
| 151 | + |
| 152 | + @SuppressWarnings("unchecked") |
| 153 | + Observer<String> aObserver = mock(Observer.class); |
| 154 | + subject.subscribe(aObserver); |
| 155 | + |
| 156 | + subject.onNext("one"); |
| 157 | + subject.onNext("two"); |
| 158 | + subject.onNext("three"); |
| 159 | + subject.onCompleted(); |
| 160 | + |
| 161 | + @SuppressWarnings("unchecked") |
| 162 | + Observer<String> anotherObserver = mock(Observer.class); |
| 163 | + subject.subscribe(anotherObserver); |
| 164 | + |
| 165 | + subject.onNext("four"); |
| 166 | + subject.onCompleted(); |
| 167 | + subject.onError(new Exception()); |
| 168 | + |
| 169 | + assertCompletedObserver(aObserver); |
| 170 | + // todo bug? assertNeverObserver(anotherObserver); |
| 171 | + } |
| 172 | + |
| 173 | + private void assertCompletedObserver(Observer<String> aObserver) |
| 174 | + { |
| 175 | + verify(aObserver, times(1)).onNext("one"); |
| 176 | + verify(aObserver, times(1)).onNext("two"); |
| 177 | + verify(aObserver, times(1)).onNext("three"); |
| 178 | + verify(aObserver, Mockito.never()).onError(any(Exception.class)); |
| 179 | + verify(aObserver, times(1)).onCompleted(); |
| 180 | + } |
| 181 | + |
| 182 | + private void assertNeverObserver(Observer<String> aObserver) |
| 183 | + { |
| 184 | + verify(aObserver, Mockito.never()).onNext(any(String.class)); |
| 185 | + verify(aObserver, Mockito.never()).onError(any(Exception.class)); |
| 186 | + verify(aObserver, Mockito.never()).onCompleted(); |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testError() { |
| 191 | + DefaultSubject<Object> subject = DefaultSubject.create(); |
| 192 | + |
| 193 | + @SuppressWarnings("unchecked") |
| 194 | + Observer<String> aObserver = mock(Observer.class); |
| 195 | + subject.subscribe(aObserver); |
| 196 | + |
| 197 | + subject.onNext("one"); |
| 198 | + subject.onNext("two"); |
| 199 | + subject.onNext("three"); |
| 200 | + subject.onError(testException); |
| 201 | + |
| 202 | + @SuppressWarnings("unchecked") |
| 203 | + Observer<String> anotherObserver = mock(Observer.class); |
| 204 | + subject.subscribe(anotherObserver); |
| 205 | + |
| 206 | + subject.onNext("four"); |
| 207 | + subject.onError(new Exception()); |
| 208 | + subject.onCompleted(); |
| 209 | + |
| 210 | + assertErrorObserver(aObserver); |
| 211 | + // todo bug? assertNeverObserver(anotherObserver); |
| 212 | + } |
| 213 | + |
| 214 | + private void assertErrorObserver(Observer<String> aObserver) |
| 215 | + { |
| 216 | + verify(aObserver, times(1)).onNext("one"); |
| 217 | + verify(aObserver, times(1)).onNext("two"); |
| 218 | + verify(aObserver, times(1)).onNext("three"); |
| 219 | + verify(aObserver, times(1)).onError(testException); |
| 220 | + verify(aObserver, Mockito.never()).onCompleted(); |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + public void testSubscribeMidSequence() { |
| 225 | + DefaultSubject<Object> subject = DefaultSubject.create(); |
| 226 | + |
| 227 | + @SuppressWarnings("unchecked") |
| 228 | + Observer<String> aObserver = mock(Observer.class); |
| 229 | + subject.subscribe(aObserver); |
| 230 | + |
| 231 | + subject.onNext("one"); |
| 232 | + subject.onNext("two"); |
| 233 | + |
| 234 | + assertObservedUntilTwo(aObserver); |
| 235 | + |
| 236 | + @SuppressWarnings("unchecked") |
| 237 | + Observer<String> anotherObserver = mock(Observer.class); |
| 238 | + subject.subscribe(anotherObserver); |
| 239 | + |
| 240 | + subject.onNext("three"); |
| 241 | + subject.onCompleted(); |
| 242 | + |
| 243 | + assertCompletedObserver(aObserver); |
| 244 | + assertCompletedStartingWithThreeObserver(anotherObserver); |
| 245 | + } |
| 246 | + |
| 247 | + private void assertCompletedStartingWithThreeObserver(Observer<String> aObserver) |
| 248 | + { |
| 249 | + verify(aObserver, Mockito.never()).onNext("one"); |
| 250 | + verify(aObserver, Mockito.never()).onNext("two"); |
| 251 | + verify(aObserver, times(1)).onNext("three"); |
| 252 | + verify(aObserver, Mockito.never()).onError(any(Exception.class)); |
| 253 | + verify(aObserver, times(1)).onCompleted(); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void testUnsubscribeFirstObserver() { |
| 258 | + DefaultSubject<Object> subject = DefaultSubject.create(); |
| 259 | + |
| 260 | + @SuppressWarnings("unchecked") |
| 261 | + Observer<String> aObserver = mock(Observer.class); |
| 262 | + Subscription subscription = subject.subscribe(aObserver); |
| 263 | + |
| 264 | + subject.onNext("one"); |
| 265 | + subject.onNext("two"); |
| 266 | + |
| 267 | + subscription.unsubscribe(); |
| 268 | + assertObservedUntilTwo(aObserver); |
| 269 | + |
| 270 | + @SuppressWarnings("unchecked") |
| 271 | + Observer<String> anotherObserver = mock(Observer.class); |
| 272 | + subject.subscribe(anotherObserver); |
| 273 | + |
| 274 | + subject.onNext("three"); |
| 275 | + subject.onCompleted(); |
| 276 | + |
| 277 | + assertObservedUntilTwo(aObserver); |
| 278 | + assertCompletedStartingWithThreeObserver(anotherObserver); |
| 279 | + } |
| 280 | + |
| 281 | + private void assertObservedUntilTwo(Observer<String> aObserver) |
| 282 | + { |
| 283 | + verify(aObserver, times(1)).onNext("one"); |
| 284 | + verify(aObserver, times(1)).onNext("two"); |
| 285 | + verify(aObserver, Mockito.never()).onNext("three"); |
| 286 | + verify(aObserver, Mockito.never()).onError(any(Exception.class)); |
| 287 | + verify(aObserver, Mockito.never()).onCompleted(); |
| 288 | + } |
| 289 | + |
| 290 | + @Test |
| 291 | + public void testUnsubscribe() |
| 292 | + { |
| 293 | + UnsubscribeTester.test(new Func0<DefaultSubject<Object>>() |
| 294 | + { |
| 295 | + @Override |
| 296 | + public DefaultSubject<Object> call() |
| 297 | + { |
| 298 | + return DefaultSubject.create(); |
| 299 | + } |
| 300 | + }, new Action1<DefaultSubject<Object>>() |
| 301 | + { |
| 302 | + @Override |
| 303 | + public void call(DefaultSubject<Object> DefaultSubject) |
| 304 | + { |
| 305 | + DefaultSubject.onCompleted(); |
| 306 | + } |
| 307 | + }, new Action1<DefaultSubject<Object>>() |
| 308 | + { |
| 309 | + @Override |
| 310 | + public void call(DefaultSubject<Object> DefaultSubject) |
| 311 | + { |
| 312 | + DefaultSubject.onError(new Exception()); |
| 313 | + } |
| 314 | + }, new Action1<DefaultSubject<Object>>() |
| 315 | + { |
| 316 | + @Override |
| 317 | + public void call(DefaultSubject<Object> DefaultSubject) |
| 318 | + { |
| 319 | + DefaultSubject.onNext("one"); |
| 320 | + } |
| 321 | + }); |
| 322 | + } |
140 | 323 | }
|
141 | 324 | }
|
0 commit comments