| 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.operators.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.ArgumentMatchers.*; |
| 18 |
import static org.mockito.Mockito.*; |
| 19 |
|
| 20 |
import java.util.*; |
| 21 |
import java.util.concurrent.*; |
| 22 |
import java.util.concurrent.atomic.*; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.mockito.Mockito; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.core.Observable; |
| 29 |
import io.reactivex.rxjava3.core.Observer; |
| 30 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 31 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 32 |
import io.reactivex.rxjava3.functions.*; |
| 33 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 34 |
import io.reactivex.rxjava3.observables.GroupedObservable; |
| 35 |
import io.reactivex.rxjava3.observers.*; |
| 36 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 37 |
import io.reactivex.rxjava3.subjects.*; |
| 38 |
import io.reactivex.rxjava3.testsupport.*; |
| 39 |
|
| 40 |
public class ObservableGroupByTest extends RxJavaTest { |
| 41 |
|
| 42 |
final Function<String, Integer> length = new Function<String, Integer>() { |
| 43 |
@Override |
| 44 |
public Integer apply(String s) { |
| 45 |
return s.length(); |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
@Test |
| 50 |
public void groupBy() { |
| 51 |
Observable<String> source = Observable.just("one", "two", "three", "four", "five", "six"); |
| 52 |
Observable<GroupedObservable<Integer, String>> grouped = source.groupBy(length); |
| 53 |
|
| 54 |
Map<Integer, Collection<String>> map = toMap(grouped); |
| 55 |
|
| 56 |
assertEquals(3, map.size()); |
| 57 |
assertArrayEquals(Arrays.asList("one", "two", "six").toArray(), map.get(3).toArray()); |
| 58 |
assertArrayEquals(Arrays.asList("four", "five").toArray(), map.get(4).toArray()); |
| 59 |
assertArrayEquals(Arrays.asList("three").toArray(), map.get(5).toArray()); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void groupByWithElementSelector() { |
| 64 |
Observable<String> source = Observable.just("one", "two", "three", "four", "five", "six"); |
| 65 |
Observable<GroupedObservable<Integer, Integer>> grouped = source.groupBy(length, length); |
| 66 |
|
| 67 |
Map<Integer, Collection<Integer>> map = toMap(grouped); |
| 68 |
|
| 69 |
assertEquals(3, map.size()); |
| 70 |
assertArrayEquals(Arrays.asList(3, 3, 3).toArray(), map.get(3).toArray()); |
| 71 |
assertArrayEquals(Arrays.asList(4, 4).toArray(), map.get(4).toArray()); |
| 72 |
assertArrayEquals(Arrays.asList(5).toArray(), map.get(5).toArray()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void groupByWithElementSelector2() { |
| 77 |
Observable<String> source = Observable.just("one", "two", "three", "four", "five", "six"); |
| 78 |
Observable<GroupedObservable<Integer, Integer>> grouped = source.groupBy(length, length); |
| 79 |
|
| 80 |
Map<Integer, Collection<Integer>> map = toMap(grouped); |
| 81 |
|
| 82 |
assertEquals(3, map.size()); |
| 83 |
assertArrayEquals(Arrays.asList(3, 3, 3).toArray(), map.get(3).toArray()); |
| 84 |
assertArrayEquals(Arrays.asList(4, 4).toArray(), map.get(4).toArray()); |
| 85 |
assertArrayEquals(Arrays.asList(5).toArray(), map.get(5).toArray()); |
| 86 |
} |
| 87 |
|
| 88 |
@Test |
| 89 |
public void empty() { |
| 90 |
Observable<String> source = Observable.empty(); |
| 91 |
Observable<GroupedObservable<Integer, String>> grouped = source.groupBy(length); |
| 92 |
|
| 93 |
Map<Integer, Collection<String>> map = toMap(grouped); |
| 94 |
|
| 95 |
assertTrue(map.isEmpty()); |
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
@SuppressUndeliverable |
| 100 |
public void error() { |
| 101 |
Observable<String> sourceStrings = Observable.just("one", "two", "three", "four", "five", "six"); |
| 102 |
Observable<String> errorSource = Observable.error(new RuntimeException("forced failure")); |
| 103 |
Observable<String> source = Observable.concat(sourceStrings, errorSource); |
| 104 |
|
| 105 |
Observable<GroupedObservable<Integer, String>> grouped = source.groupBy(length); |
| 106 |
|
| 107 |
final AtomicInteger groupCounter = new AtomicInteger(); |
| 108 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 109 |
final AtomicReference<Throwable> error = new AtomicReference<>(); |
| 110 |
|
| 111 |
grouped.flatMap(new Function<GroupedObservable<Integer, String>, Observable<String>>() { |
| 112 |
|
| 113 |
@Override |
| 114 |
public Observable<String> apply(final GroupedObservable<Integer, String> o) { |
| 115 |
groupCounter.incrementAndGet(); |
| 116 |
return o.map(new Function<String, String>() { |
| 117 |
|
| 118 |
@Override |
| 119 |
public String apply(String v) { |
| 120 |
return "Event => key: " + o.getKey() + " value: " + v; |
| 121 |
} |
| 122 |
}); |
| 123 |
} |
| 124 |
}).subscribe(new DefaultObserver<String>() { |
| 125 |
|
| 126 |
@Override |
| 127 |
public void onComplete() { |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void onError(Throwable e) { |
| 133 |
e.printStackTrace(); |
| 134 |
error.set(e); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void onNext(String v) { |
| 139 |
eventCounter.incrementAndGet(); |
| 140 |
System.out.println(v); |
| 141 |
|
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
assertEquals(3, groupCounter.get()); |
| 146 |
assertEquals(6, eventCounter.get()); |
| 147 |
assertNotNull(error.get()); |
| 148 |
} |
| 149 |
|
| 150 |
private static <K, V> Map<K, Collection<V>> toMap(Observable<GroupedObservable<K, V>> observable) { |
| 151 |
|
| 152 |
final ConcurrentHashMap<K, Collection<V>> result = new ConcurrentHashMap<>(); |
| 153 |
|
| 154 |
observable.doOnNext(new Consumer<GroupedObservable<K, V>>() { |
| 155 |
|
| 156 |
@Override |
| 157 |
public void accept(final GroupedObservable<K, V> o) { |
| 158 |
result.put(o.getKey(), new ConcurrentLinkedQueue<>()); |
| 159 |
o.subscribe(new Consumer<V>() { |
| 160 |
|
| 161 |
@Override |
| 162 |
public void accept(V v) { |
| 163 |
result.get(o.getKey()).add(v); |
| 164 |
} |
| 165 |
|
| 166 |
}); |
| 167 |
} |
| 168 |
}).blockingSubscribe(); |
| 169 |
|
| 170 |
return result; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Assert that only a single subscription to a stream occurs and that all events are received. |
| 175 |
* |
| 176 |
* @throws Throwable some method may throw |
| 177 |
*/ |
| 178 |
@Test |
| 179 |
public void groupedEventStream() throws Throwable { |
| 180 |
|
| 181 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 182 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 183 |
final AtomicInteger groupCounter = new AtomicInteger(); |
| 184 |
final CountDownLatch latch = new CountDownLatch(1); |
| 185 |
final int count = 100; |
| 186 |
final int groupCount = 2; |
| 187 |
|
| 188 |
Observable<Event> es = Observable.unsafeCreate(new ObservableSource<Event>() { |
| 189 |
|
| 190 |
@Override |
| 191 |
public void subscribe(final Observer<? super Event> observer) { |
| 192 |
observer.onSubscribe(Disposable.empty()); |
| 193 |
System.out.println("*** Subscribing to EventStream ***"); |
| 194 |
subscribeCounter.incrementAndGet(); |
| 195 |
new Thread(new Runnable() { |
| 196 |
|
| 197 |
@Override |
| 198 |
public void run() { |
| 199 |
for (int i = 0; i < count; i++) { |
| 200 |
Event e = new Event(); |
| 201 |
e.source = i % groupCount; |
| 202 |
e.message = "Event-" + i; |
| 203 |
observer.onNext(e); |
| 204 |
} |
| 205 |
observer.onComplete(); |
| 206 |
} |
| 207 |
|
| 208 |
}).start(); |
| 209 |
} |
| 210 |
|
| 211 |
}); |
| 212 |
|
| 213 |
es.groupBy(new Function<Event, Integer>() { |
| 214 |
|
| 215 |
@Override |
| 216 |
public Integer apply(Event e) { |
| 217 |
return e.source; |
| 218 |
} |
| 219 |
}).flatMap(new Function<GroupedObservable<Integer, Event>, Observable<String>>() { |
| 220 |
|
| 221 |
@Override |
| 222 |
public Observable<String> apply(GroupedObservable<Integer, Event> eventGroupedObservable) { |
| 223 |
System.out.println("GroupedObservable Key: " + eventGroupedObservable.getKey()); |
| 224 |
groupCounter.incrementAndGet(); |
| 225 |
|
| 226 |
return eventGroupedObservable.map(new Function<Event, String>() { |
| 227 |
|
| 228 |
@Override |
| 229 |
public String apply(Event event) { |
| 230 |
return "Source: " + event.source + " Message: " + event.message; |
| 231 |
} |
| 232 |
}); |
| 233 |
|
| 234 |
} |
| 235 |
}).subscribe(new DefaultObserver<String>() { |
| 236 |
|
| 237 |
@Override |
| 238 |
public void onComplete() { |
| 239 |
latch.countDown(); |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public void onError(Throwable e) { |
| 244 |
e.printStackTrace(); |
| 245 |
latch.countDown(); |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
public void onNext(String outputMessage) { |
| 250 |
System.out.println(outputMessage); |
| 251 |
eventCounter.incrementAndGet(); |
| 252 |
} |
| 253 |
}); |
| 254 |
|
| 255 |
latch.await(5000, TimeUnit.MILLISECONDS); |
| 256 |
assertEquals(1, subscribeCounter.get()); |
| 257 |
assertEquals(groupCount, groupCounter.get()); |
| 258 |
assertEquals(count, eventCounter.get()); |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
/* |
| 263 |
* We will only take 1 group with 20 events from it and then unsubscribe. |
| 264 |
*/ |
| 265 |
@Test |
| 266 |
public void unsubscribeOnNestedTakeAndSyncInfiniteStream() throws InterruptedException { |
| 267 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 268 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 269 |
doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(SYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); |
| 270 |
Thread.sleep(500); |
| 271 |
assertEquals(39, sentEventCounter.get()); |
| 272 |
} |
| 273 |
|
| 274 |
/* |
| 275 |
* We will only take 1 group with 20 events from it and then unsubscribe. |
| 276 |
*/ |
| 277 |
@Test |
| 278 |
public void unsubscribeOnNestedTakeAndAsyncInfiniteStream() throws InterruptedException { |
| 279 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 280 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 281 |
doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(ASYNC_INFINITE_OBSERVABLE_OF_EVENT(2, subscribeCounter, sentEventCounter), subscribeCounter); |
| 282 |
Thread.sleep(500); |
| 283 |
assertEquals(39, sentEventCounter.get()); |
| 284 |
} |
| 285 |
|
| 286 |
private void doTestUnsubscribeOnNestedTakeAndAsyncInfiniteStream(Observable<Event> es, AtomicInteger subscribeCounter) throws InterruptedException { |
| 287 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 288 |
final AtomicInteger groupCounter = new AtomicInteger(); |
| 289 |
final CountDownLatch latch = new CountDownLatch(1); |
| 290 |
|
| 291 |
es.groupBy(new Function<Event, Integer>() { |
| 292 |
|
| 293 |
@Override |
| 294 |
public Integer apply(Event e) { |
| 295 |
return e.source; |
| 296 |
} |
| 297 |
}) |
| 298 |
.take(1) // we want only the first group |
| 299 |
.flatMap(new Function<GroupedObservable<Integer, Event>, Observable<String>>() { |
| 300 |
|
| 301 |
@Override |
| 302 |
public Observable<String> apply(GroupedObservable<Integer, Event> eventGroupedObservable) { |
| 303 |
System.out.println("testUnsubscribe => GroupedObservable Key: " + eventGroupedObservable.getKey()); |
| 304 |
groupCounter.incrementAndGet(); |
| 305 |
|
| 306 |
return eventGroupedObservable |
| 307 |
.take(20) // limit to only 20 events on this group |
| 308 |
.map(new Function<Event, String>() { |
| 309 |
|
| 310 |
@Override |
| 311 |
public String apply(Event event) { |
| 312 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 313 |
} |
| 314 |
}); |
| 315 |
|
| 316 |
} |
| 317 |
}).subscribe(new DefaultObserver<String>() { |
| 318 |
|
| 319 |
@Override |
| 320 |
public void onComplete() { |
| 321 |
latch.countDown(); |
| 322 |
} |
| 323 |
|
| 324 |
@Override |
| 325 |
public void onError(Throwable e) { |
| 326 |
e.printStackTrace(); |
| 327 |
latch.countDown(); |
| 328 |
} |
| 329 |
|
| 330 |
@Override |
| 331 |
public void onNext(String outputMessage) { |
| 332 |
System.out.println(outputMessage); |
| 333 |
eventCounter.incrementAndGet(); |
| 334 |
} |
| 335 |
}); |
| 336 |
|
| 337 |
if (!latch.await(2000, TimeUnit.MILLISECONDS)) { |
| 338 |
fail("timed out so likely did not unsubscribe correctly"); |
| 339 |
} |
| 340 |
assertEquals(1, subscribeCounter.get()); |
| 341 |
assertEquals(1, groupCounter.get()); |
| 342 |
assertEquals(20, eventCounter.get()); |
| 343 |
// sentEvents will go until 'eventCounter' hits 20 and then unsubscribes |
| 344 |
// which means it will also send (but ignore) the 19/20 events for the other group |
| 345 |
// It will not however send all 100 events. |
| 346 |
} |
| 347 |
|
| 348 |
@Test |
| 349 |
public void unsubscribeViaTakeOnGroupThenMergeAndTake() { |
| 350 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 351 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 352 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 353 |
|
| 354 |
SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) |
| 355 |
.groupBy(new Function<Event, Integer>() { |
| 356 |
|
| 357 |
@Override |
| 358 |
public Integer apply(Event e) { |
| 359 |
return e.source; |
| 360 |
} |
| 361 |
}) |
| 362 |
// take 2 of the 4 groups |
| 363 |
.take(2) |
| 364 |
.flatMap(new Function<GroupedObservable<Integer, Event>, Observable<String>>() { |
| 365 |
|
| 366 |
@Override |
| 367 |
public Observable<String> apply(GroupedObservable<Integer, Event> eventGroupedObservable) { |
| 368 |
return eventGroupedObservable |
| 369 |
.map(new Function<Event, String>() { |
| 370 |
|
| 371 |
@Override |
| 372 |
public String apply(Event event) { |
| 373 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 374 |
} |
| 375 |
}); |
| 376 |
|
| 377 |
} |
| 378 |
}) |
| 379 |
.take(30).subscribe(new Consumer<String>() { |
| 380 |
|
| 381 |
@Override |
| 382 |
public void accept(String s) { |
| 383 |
eventCounter.incrementAndGet(); |
| 384 |
System.out.println("=> " + s); |
| 385 |
} |
| 386 |
|
| 387 |
}); |
| 388 |
|
| 389 |
assertEquals(30, eventCounter.get()); |
| 390 |
// we should send 28 additional events that are filtered out as they are in the groups we skip |
| 391 |
assertEquals(58, sentEventCounter.get()); |
| 392 |
} |
| 393 |
|
| 394 |
@Test |
| 395 |
public void unsubscribeViaTakeOnGroupThenTakeOnInner() { |
| 396 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 397 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 398 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 399 |
|
| 400 |
SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) |
| 401 |
.groupBy(new Function<Event, Integer>() { |
| 402 |
|
| 403 |
@Override |
| 404 |
public Integer apply(Event e) { |
| 405 |
return e.source; |
| 406 |
} |
| 407 |
}) |
| 408 |
// take 2 of the 4 groups |
| 409 |
.take(2) |
| 410 |
.flatMap(new Function<GroupedObservable<Integer, Event>, Observable<String>>() { |
| 411 |
|
| 412 |
@Override |
| 413 |
public Observable<String> apply(GroupedObservable<Integer, Event> eventGroupedObservable) { |
| 414 |
int numToTake = 0; |
| 415 |
if (eventGroupedObservable.getKey() == 1) { |
| 416 |
numToTake = 10; |
| 417 |
} else if (eventGroupedObservable.getKey() == 2) { |
| 418 |
numToTake = 5; |
| 419 |
} |
| 420 |
return eventGroupedObservable |
| 421 |
.take(numToTake) |
| 422 |
.map(new Function<Event, String>() { |
| 423 |
|
| 424 |
@Override |
| 425 |
public String apply(Event event) { |
| 426 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 427 |
} |
| 428 |
}); |
| 429 |
|
| 430 |
} |
| 431 |
}) |
| 432 |
.subscribe(new Consumer<String>() { |
| 433 |
|
| 434 |
@Override |
| 435 |
public void accept(String s) { |
| 436 |
eventCounter.incrementAndGet(); |
| 437 |
System.out.println("=> " + s); |
| 438 |
} |
| 439 |
|
| 440 |
}); |
| 441 |
|
| 442 |
assertEquals(15, eventCounter.get()); |
| 443 |
// we should send 22 additional events that are filtered out as they are skipped while taking the 15 we want |
| 444 |
assertEquals(37, sentEventCounter.get()); |
| 445 |
} |
| 446 |
|
| 447 |
@Test |
| 448 |
public void staggeredCompletion() throws InterruptedException { |
| 449 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 450 |
final CountDownLatch latch = new CountDownLatch(1); |
| 451 |
Observable.range(0, 100) |
| 452 |
.groupBy(new Function<Integer, Integer>() { |
| 453 |
|
| 454 |
@Override |
| 455 |
public Integer apply(Integer i) { |
| 456 |
return i % 2; |
| 457 |
} |
| 458 |
}) |
| 459 |
.flatMap(new Function<GroupedObservable<Integer, Integer>, Observable<Integer>>() { |
| 460 |
|
| 461 |
@Override |
| 462 |
public Observable<Integer> apply(GroupedObservable<Integer, Integer> group) { |
| 463 |
if (group.getKey() == 0) { |
| 464 |
return group.delay(100, TimeUnit.MILLISECONDS).map(new Function<Integer, Integer>() { |
| 465 |
@Override |
| 466 |
public Integer apply(Integer t) { |
| 467 |
return t * 10; |
| 468 |
} |
| 469 |
|
| 470 |
}); |
| 471 |
} else { |
| 472 |
return group; |
| 473 |
} |
| 474 |
} |
| 475 |
}) |
| 476 |
.subscribe(new DefaultObserver<Integer>() { |
| 477 |
|
| 478 |
@Override |
| 479 |
public void onComplete() { |
| 480 |
System.out.println("=> onComplete"); |
| 481 |
latch.countDown(); |
| 482 |
} |
| 483 |
|
| 484 |
@Override |
| 485 |
public void onError(Throwable e) { |
| 486 |
e.printStackTrace(); |
| 487 |
latch.countDown(); |
| 488 |
} |
| 489 |
|
| 490 |
@Override |
| 491 |
public void onNext(Integer s) { |
| 492 |
eventCounter.incrementAndGet(); |
| 493 |
System.out.println("=> " + s); |
| 494 |
} |
| 495 |
}); |
| 496 |
|
| 497 |
if (!latch.await(3000, TimeUnit.MILLISECONDS)) { |
| 498 |
fail("timed out"); |
| 499 |
} |
| 500 |
|
| 501 |
assertEquals(100, eventCounter.get()); |
| 502 |
} |
| 503 |
|
| 504 |
@Test |
| 505 |
public void completionIfInnerNotSubscribed() throws InterruptedException { |
| 506 |
final CountDownLatch latch = new CountDownLatch(1); |
| 507 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 508 |
Observable.range(0, 100) |
| 509 |
.groupBy(new Function<Integer, Integer>() { |
| 510 |
|
| 511 |
@Override |
| 512 |
public Integer apply(Integer i) { |
| 513 |
return i % 2; |
| 514 |
} |
| 515 |
}) |
| 516 |
.subscribe(new DefaultObserver<GroupedObservable<Integer, Integer>>() { |
| 517 |
|
| 518 |
@Override |
| 519 |
public void onComplete() { |
| 520 |
latch.countDown(); |
| 521 |
} |
| 522 |
|
| 523 |
@Override |
| 524 |
public void onError(Throwable e) { |
| 525 |
e.printStackTrace(); |
| 526 |
latch.countDown(); |
| 527 |
} |
| 528 |
|
| 529 |
@Override |
| 530 |
public void onNext(GroupedObservable<Integer, Integer> s) { |
| 531 |
eventCounter.incrementAndGet(); |
| 532 |
System.out.println("=> " + s); |
| 533 |
} |
| 534 |
}); |
| 535 |
if (!latch.await(500, TimeUnit.MILLISECONDS)) { |
| 536 |
fail("timed out - never got completion"); |
| 537 |
} |
| 538 |
// Behavior change: groups not subscribed immediately will be automatically abandoned |
| 539 |
// so this leads to group recreation |
| 540 |
assertEquals(100, eventCounter.get()); |
| 541 |
} |
| 542 |
|
| 543 |
@Test |
| 544 |
public void ignoringGroups() { |
| 545 |
final AtomicInteger subscribeCounter = new AtomicInteger(); |
| 546 |
final AtomicInteger sentEventCounter = new AtomicInteger(); |
| 547 |
final AtomicInteger eventCounter = new AtomicInteger(); |
| 548 |
|
| 549 |
SYNC_INFINITE_OBSERVABLE_OF_EVENT(4, subscribeCounter, sentEventCounter) |
| 550 |
.groupBy(new Function<Event, Integer>() { |
| 551 |
|
| 552 |
@Override |
| 553 |
public Integer apply(Event e) { |
| 554 |
return e.source; |
| 555 |
} |
| 556 |
}) |
| 557 |
.flatMap(new Function<GroupedObservable<Integer, Event>, Observable<String>>() { |
| 558 |
|
| 559 |
@Override |
| 560 |
public Observable<String> apply(GroupedObservable<Integer, Event> eventGroupedObservable) { |
| 561 |
Observable<Event> eventStream = eventGroupedObservable; |
| 562 |
if (eventGroupedObservable.getKey() >= 2) { |
| 563 |
// filter these |
| 564 |
eventStream = eventGroupedObservable.filter(new Predicate<Event>() { |
| 565 |
@Override |
| 566 |
public boolean test(Event t1) { |
| 567 |
return false; |
| 568 |
} |
| 569 |
}); |
| 570 |
} |
| 571 |
|
| 572 |
return eventStream |
| 573 |
.map(new Function<Event, String>() { |
| 574 |
|
| 575 |
@Override |
| 576 |
public String apply(Event event) { |
| 577 |
return "testUnsubscribe => Source: " + event.source + " Message: " + event.message; |
| 578 |
} |
| 579 |
}); |
| 580 |
|
| 581 |
} |
| 582 |
}) |
| 583 |
.take(30).subscribe(new Consumer<String>() { |
| 584 |
|
| 585 |
@Override |
| 586 |
public void accept(String s) { |
| 587 |
eventCounter.incrementAndGet(); |
| 588 |
System.out.println("=> " + s); |
| 589 |
} |
| 590 |
|
| 591 |
}); |
| 592 |
|
| 593 |
assertEquals(30, eventCounter.get()); |
| 594 |
// we should send 30 additional events that are filtered out as they are in the groups we skip |
| 595 |
assertEquals(60, sentEventCounter.get()); |
| 596 |
} |
| 597 |
|
| 598 |
@Test |
| 599 |
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException { |
| 600 |
final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete |
| 601 |
final ArrayList<String> results = new ArrayList<>(); |
| 602 |
Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 603 |
|
| 604 |
@Override |
| 605 |
public void subscribe(Observer<? super Integer> sub) { |
| 606 |
sub.onSubscribe(Disposable.empty()); |
| 607 |
sub.onNext(1); |
| 608 |
sub.onNext(2); |
| 609 |
sub.onNext(1); |
| 610 |
sub.onNext(2); |
| 611 |
try { |
| 612 |
first.await(); |
| 613 |
} catch (InterruptedException e) { |
| 614 |
sub.onError(e); |
| 615 |
return; |
| 616 |
} |
| 617 |
sub.onNext(3); |
| 618 |
sub.onNext(3); |
| 619 |
sub.onComplete(); |
| 620 |
} |
| 621 |
|
| 622 |
}).groupBy(new Function<Integer, Integer>() { |
| 623 |
|
| 624 |
@Override |
| 625 |
public Integer apply(Integer t) { |
| 626 |
return t; |
| 627 |
} |
| 628 |
|
| 629 |
}).flatMap(new Function<GroupedObservable<Integer, Integer>, Observable<String>>() { |
| 630 |
|
| 631 |
@Override |
| 632 |
public Observable<String> apply(final GroupedObservable<Integer, Integer> group) { |
| 633 |
if (group.getKey() < 3) { |
| 634 |
return group.map(new Function<Integer, String>() { |
| 635 |
|
| 636 |
@Override |
| 637 |
public String apply(Integer t1) { |
| 638 |
return "first groups: " + t1; |
| 639 |
} |
| 640 |
|
| 641 |
}) |
| 642 |
// must take(2) so an onComplete + unsubscribe happens on these first 2 groups |
| 643 |
.take(2).doOnComplete(new Action() { |
| 644 |
|
| 645 |
@Override |
| 646 |
public void run() { |
| 647 |
first.countDown(); |
| 648 |
} |
| 649 |
|
| 650 |
}); |
| 651 |
} else { |
| 652 |
return group.map(new Function<Integer, String>() { |
| 653 |
|
| 654 |
@Override |
| 655 |
public String apply(Integer t1) { |
| 656 |
return "last group: " + t1; |
| 657 |
} |
| 658 |
|
| 659 |
}); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
}).blockingForEach(new Consumer<String>() { |
| 664 |
|
| 665 |
@Override |
| 666 |
public void accept(String s) { |
| 667 |
results.add(s); |
| 668 |
} |
| 669 |
|
| 670 |
}); |
| 671 |
|
| 672 |
System.out.println("Results: " + results); |
| 673 |
assertEquals(6, results.size()); |
| 674 |
} |
| 675 |
|
| 676 |
@Test |
| 677 |
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenSubscribesOnAndDelaysAndThenCompletes() throws InterruptedException { |
| 678 |
System.err.println("----------------------------------------------------------------------------------------------"); |
| 679 |
final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete |
| 680 |
final ArrayList<String> results = new ArrayList<>(); |
| 681 |
Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 682 |
|
| 683 |
@Override |
| 684 |
public void subscribe(Observer<? super Integer> sub) { |
| 685 |
sub.onSubscribe(Disposable.empty()); |
| 686 |
sub.onNext(1); |
| 687 |
sub.onNext(2); |
| 688 |
sub.onNext(1); |
| 689 |
sub.onNext(2); |
| 690 |
try { |
| 691 |
first.await(); |
| 692 |
} catch (InterruptedException e) { |
| 693 |
sub.onError(e); |
| 694 |
return; |
| 695 |
} |
| 696 |
sub.onNext(3); |
| 697 |
sub.onNext(3); |
| 698 |
sub.onComplete(); |
| 699 |
} |
| 700 |
|
| 701 |
}).groupBy(new Function<Integer, Integer>() { |
| 702 |
|
| 703 |
@Override |
| 704 |
public Integer apply(Integer t) { |
| 705 |
return t; |
| 706 |
} |
| 707 |
|
| 708 |
}).flatMap(new Function<GroupedObservable<Integer, Integer>, Observable<String>>() { |
| 709 |
|
| 710 |
@Override |
| 711 |
public Observable<String> apply(final GroupedObservable<Integer, Integer> group) { |
| 712 |
if (group.getKey() < 3) { |
| 713 |
return group.map(new Function<Integer, String>() { |
| 714 |
|
| 715 |
@Override |
| 716 |
public String apply(Integer t1) { |
| 717 |
return "first groups: " + t1; |
| 718 |
} |
| 719 |
|
| 720 |
}) |
| 721 |
// must take(2) so an onComplete + unsubscribe happens on these first 2 groups |
| 722 |
.take(2).doOnComplete(new Action() { |
| 723 |
|
| 724 |
@Override |
| 725 |
public void run() { |
| 726 |
first.countDown(); |
| 727 |
} |
| 728 |
|
| 729 |
}); |
| 730 |
} else { |
| 731 |
return group.subscribeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() { |
| 732 |
|
| 733 |
@Override |
| 734 |
public String apply(Integer t1) { |
| 735 |
return "last group: " + t1; |
| 736 |
} |
| 737 |
|
| 738 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 739 |
|
| 740 |
@Override |
| 741 |
public void accept(Notification<String> t1) { |
| 742 |
System.err.println("subscribeOn notification => " + t1); |
| 743 |
} |
| 744 |
|
| 745 |
}); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 750 |
|
| 751 |
@Override |
| 752 |
public void accept(Notification<String> t1) { |
| 753 |
System.err.println("outer notification => " + t1); |
| 754 |
} |
| 755 |
|
| 756 |
}).blockingForEach(new Consumer<String>() { |
| 757 |
|
| 758 |
@Override |
| 759 |
public void accept(String s) { |
| 760 |
results.add(s); |
| 761 |
} |
| 762 |
|
| 763 |
}); |
| 764 |
|
| 765 |
System.out.println("Results: " + results); |
| 766 |
assertEquals(6, results.size()); |
| 767 |
} |
| 768 |
|
| 769 |
@Test |
| 770 |
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenObservesOnAndDelaysAndThenCompletes() throws InterruptedException { |
| 771 |
final CountDownLatch first = new CountDownLatch(2); // there are two groups to first complete |
| 772 |
final ArrayList<String> results = new ArrayList<>(); |
| 773 |
Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 774 |
|
| 775 |
@Override |
| 776 |
public void subscribe(Observer<? super Integer> sub) { |
| 777 |
sub.onSubscribe(Disposable.empty()); |
| 778 |
sub.onNext(1); |
| 779 |
sub.onNext(2); |
| 780 |
sub.onNext(1); |
| 781 |
sub.onNext(2); |
| 782 |
try { |
| 783 |
first.await(); |
| 784 |
} catch (InterruptedException e) { |
| 785 |
sub.onError(e); |
| 786 |
return; |
| 787 |
} |
| 788 |
sub.onNext(3); |
| 789 |
sub.onNext(3); |
| 790 |
sub.onComplete(); |
| 791 |
} |
| 792 |
|
| 793 |
}).groupBy(new Function<Integer, Integer>() { |
| 794 |
|
| 795 |
@Override |
| 796 |
public Integer apply(Integer t) { |
| 797 |
return t; |
| 798 |
} |
| 799 |
|
| 800 |
}).flatMap(new Function<GroupedObservable<Integer, Integer>, Observable<String>>() { |
| 801 |
|
| 802 |
@Override |
| 803 |
public Observable<String> apply(final GroupedObservable<Integer, Integer> group) { |
| 804 |
if (group.getKey() < 3) { |
| 805 |
return group.map(new Function<Integer, String>() { |
| 806 |
|
| 807 |
@Override |
| 808 |
public String apply(Integer t1) { |
| 809 |
return "first groups: " + t1; |
| 810 |
} |
| 811 |
|
| 812 |
}) |
| 813 |
// must take(2) so an onComplete + unsubscribe happens on these first 2 groups |
| 814 |
.take(2).doOnComplete(new Action() { |
| 815 |
|
| 816 |
@Override |
| 817 |
public void run() { |
| 818 |
first.countDown(); |
| 819 |
} |
| 820 |
|
| 821 |
}); |
| 822 |
} else { |
| 823 |
return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() { |
| 824 |
|
| 825 |
@Override |
| 826 |
public String apply(Integer t1) { |
| 827 |
return "last group: " + t1; |
| 828 |
} |
| 829 |
|
| 830 |
}); |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
}).blockingForEach(new Consumer<String>() { |
| 835 |
|
| 836 |
@Override |
| 837 |
public void accept(String s) { |
| 838 |
results.add(s); |
| 839 |
} |
| 840 |
|
| 841 |
}); |
| 842 |
|
| 843 |
System.out.println("Results: " + results); |
| 844 |
assertEquals(6, results.size()); |
| 845 |
} |
| 846 |
|
| 847 |
@Test |
| 848 |
public void groupsWithNestedSubscribeOn() throws InterruptedException { |
| 849 |
final ArrayList<String> results = new ArrayList<>(); |
| 850 |
Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 851 |
|
| 852 |
@Override |
| 853 |
public void subscribe(Observer<? super Integer> sub) { |
| 854 |
sub.onSubscribe(Disposable.empty()); |
| 855 |
sub.onNext(1); |
| 856 |
sub.onNext(2); |
| 857 |
sub.onNext(1); |
| 858 |
sub.onNext(2); |
| 859 |
sub.onComplete(); |
| 860 |
} |
| 861 |
|
| 862 |
}).groupBy(new Function<Integer, Integer>() { |
| 863 |
|
| 864 |
@Override |
| 865 |
public Integer apply(Integer t) { |
| 866 |
return t; |
| 867 |
} |
| 868 |
|
| 869 |
}).flatMap(new Function<GroupedObservable<Integer, Integer>, Observable<String>>() { |
| 870 |
|
| 871 |
@Override |
| 872 |
public Observable<String> apply(final GroupedObservable<Integer, Integer> group) { |
| 873 |
return group.subscribeOn(Schedulers.newThread()).map(new Function<Integer, String>() { |
| 874 |
|
| 875 |
@Override |
| 876 |
public String apply(Integer t1) { |
| 877 |
System.out.println("Received: " + t1 + " on group : " + group.getKey()); |
| 878 |
return "first groups: " + t1; |
| 879 |
} |
| 880 |
|
| 881 |
}); |
| 882 |
} |
| 883 |
|
| 884 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 885 |
|
| 886 |
@Override |
| 887 |
public void accept(Notification<String> t1) { |
| 888 |
System.out.println("notification => " + t1); |
| 889 |
} |
| 890 |
|
| 891 |
}).blockingForEach(new Consumer<String>() { |
| 892 |
|
| 893 |
@Override |
| 894 |
public void accept(String s) { |
| 895 |
results.add(s); |
| 896 |
} |
| 897 |
|
| 898 |
}); |
| 899 |
|
| 900 |
System.out.println("Results: " + results); |
| 901 |
assertEquals(4, results.size()); |
| 902 |
} |
| 903 |
|
| 904 |
@Test |
| 905 |
public void groupsWithNestedObserveOn() throws InterruptedException { |
| 906 |
final ArrayList<String> results = new ArrayList<>(); |
| 907 |
Observable.unsafeCreate(new ObservableSource<Integer>() { |
| 908 |
|
| 909 |
@Override |
| 910 |
public void subscribe(Observer<? super Integer> sub) { |
| 911 |
sub.onSubscribe(Disposable.empty()); |
| 912 |
sub.onNext(1); |
| 913 |
sub.onNext(2); |
| 914 |
sub.onNext(1); |
| 915 |
sub.onNext(2); |
| 916 |
sub.onComplete(); |
| 917 |
} |
| 918 |
|
| 919 |
}).groupBy(new Function<Integer, Integer>() { |
| 920 |
|
| 921 |
@Override |
| 922 |
public Integer apply(Integer t) { |
| 923 |
return t; |
| 924 |
} |
| 925 |
|
| 926 |
}).flatMap(new Function<GroupedObservable<Integer, Integer>, Observable<String>>() { |
| 927 |
|
| 928 |
@Override |
| 929 |
public Observable<String> apply(final GroupedObservable<Integer, Integer> group) { |
| 930 |
return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() { |
| 931 |
|
| 932 |
@Override |
| 933 |
public String apply(Integer t1) { |
| 934 |
return "first groups: " + t1; |
| 935 |
} |
| 936 |
|
| 937 |
}); |
| 938 |
} |
| 939 |
|
| 940 |
}).blockingForEach(new Consumer<String>() { |
| 941 |
|
| 942 |
@Override |
| 943 |
public void accept(String s) { |
| 944 |
results.add(s); |
| 945 |
} |
| 946 |
|
| 947 |
}); |
| 948 |
|
| 949 |
System.out.println("Results: " + results); |
| 950 |
assertEquals(4, results.size()); |
| 951 |
} |
| 952 |
|
| 953 |
private static class Event { |
| 954 |
int source; |
| 955 |
String message; |
| 956 |
|
| 957 |
@Override |
| 958 |
public String toString() { |
| 959 |
return "Event => source: " + source + " message: " + message; |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
Observable<Event> ASYNC_INFINITE_OBSERVABLE_OF_EVENT(final int numGroups, final AtomicInteger subscribeCounter, final AtomicInteger sentEventCounter) { |
| 964 |
return SYNC_INFINITE_OBSERVABLE_OF_EVENT(numGroups, subscribeCounter, sentEventCounter).subscribeOn(Schedulers.newThread()); |
| 965 |
}; |
| 966 |
|
| 967 |
Observable<Event> SYNC_INFINITE_OBSERVABLE_OF_EVENT(final int numGroups, final AtomicInteger subscribeCounter, final AtomicInteger sentEventCounter) { |
| 968 |
return Observable.unsafeCreate(new ObservableSource<Event>() { |
| 969 |
|
| 970 |
@Override |
| 971 |
public void subscribe(final Observer<? super Event> op) { |
| 972 |
Disposable d = Disposable.empty(); |
| 973 |
op.onSubscribe(d); |
| 974 |
subscribeCounter.incrementAndGet(); |
| 975 |
int i = 0; |
| 976 |
while (!d.isDisposed()) { |
| 977 |
i++; |
| 978 |
Event e = new Event(); |
| 979 |
e.source = i % numGroups; |
| 980 |
e.message = "Event-" + i; |
| 981 |
op.onNext(e); |
| 982 |
sentEventCounter.incrementAndGet(); |
| 983 |
} |
| 984 |
op.onComplete(); |
| 985 |
} |
| 986 |
|
| 987 |
}); |
| 988 |
}; |
| 989 |
|
| 990 |
@Test |
| 991 |
public void groupByOnAsynchronousSourceAcceptsMultipleSubscriptions() throws InterruptedException { |
| 992 |
|
| 993 |
// choose an asynchronous source |
| 994 |
Observable<Long> source = Observable.interval(10, TimeUnit.MILLISECONDS).take(1); |
| 995 |
|
| 996 |
// apply groupBy to the source |
| 997 |
Observable<GroupedObservable<Boolean, Long>> stream = source.groupBy(IS_EVEN); |
| 998 |
|
| 999 |
// create two observers |
| 1000 |
Observer<GroupedObservable<Boolean, Long>> o1 = TestHelper.mockObserver(); |
| 1001 |
Observer<GroupedObservable<Boolean, Long>> o2 = TestHelper.mockObserver(); |
| 1002 |
|
| 1003 |
// subscribe with the observers |
| 1004 |
stream.subscribe(o1); |
| 1005 |
stream.subscribe(o2); |
| 1006 |
|
| 1007 |
// check that subscriptions were successful |
| 1008 |
verify(o1, never()).onError(Mockito.<Throwable> any()); |
| 1009 |
verify(o2, never()).onError(Mockito.<Throwable> any()); |
| 1010 |
} |
| 1011 |
|
| 1012 |
private static Function<Long, Boolean> IS_EVEN = new Function<Long, Boolean>() { |
| 1013 |
|
| 1014 |
@Override |
| 1015 |
public Boolean apply(Long n) { |
| 1016 |
return n % 2 == 0; |
| 1017 |
} |
| 1018 |
}; |
| 1019 |
|
| 1020 |
private static Function<Integer, Boolean> IS_EVEN2 = new Function<Integer, Boolean>() { |
| 1021 |
|
| 1022 |
@Override |
| 1023 |
public Boolean apply(Integer n) { |
| 1024 |
return n % 2 == 0; |
| 1025 |
} |
| 1026 |
}; |
| 1027 |
|
| 1028 |
@Test |
| 1029 |
public void groupByBackpressure() throws InterruptedException { |
| 1030 |
|
| 1031 |
TestObserver<String> to = new TestObserver<>(); |
| 1032 |
|
| 1033 |
Observable.range(1, 4000) |
| 1034 |
.groupBy(IS_EVEN2) |
| 1035 |
.flatMap(new Function<GroupedObservable<Boolean, Integer>, Observable<String>>() { |
| 1036 |
|
| 1037 |
@Override |
| 1038 |
public Observable<String> apply(final GroupedObservable<Boolean, Integer> g) { |
| 1039 |
return g.observeOn(Schedulers.computation()).map(new Function<Integer, String>() { |
| 1040 |
|
| 1041 |
@Override |
| 1042 |
public String apply(Integer l) { |
| 1043 |
if (g.getKey()) { |
| 1044 |
try { |
| 1045 |
Thread.sleep(1); |
| 1046 |
} catch (InterruptedException e) { |
| 1047 |
} |
| 1048 |
return l + " is even."; |
| 1049 |
} else { |
| 1050 |
return l + " is odd."; |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
}); |
| 1055 |
} |
| 1056 |
|
| 1057 |
}).subscribe(to); |
| 1058 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1059 |
to.assertNoErrors(); |
| 1060 |
} |
| 1061 |
|
| 1062 |
<T, R> Function<T, R> just(final R value) { |
| 1063 |
return new Function<T, R>() { |
| 1064 |
@Override |
| 1065 |
public R apply(T t1) { |
| 1066 |
return value; |
| 1067 |
} |
| 1068 |
}; |
| 1069 |
} |
| 1070 |
|
| 1071 |
<T> Function<Integer, T> fail(T dummy) { |
| 1072 |
return new Function<Integer, T>() { |
| 1073 |
@Override |
| 1074 |
public T apply(Integer t1) { |
| 1075 |
throw new RuntimeException("Forced failure"); |
| 1076 |
} |
| 1077 |
}; |
| 1078 |
} |
| 1079 |
|
| 1080 |
<T, R> Function<T, R> fail2(R dummy2) { |
| 1081 |
return new Function<T, R>() { |
| 1082 |
@Override |
| 1083 |
public R apply(T t1) { |
| 1084 |
throw new RuntimeException("Forced failure"); |
| 1085 |
} |
| 1086 |
}; |
| 1087 |
} |
| 1088 |
|
| 1089 |
Function<Integer, Integer> dbl = new Function<Integer, Integer>() { |
| 1090 |
@Override |
| 1091 |
public Integer apply(Integer t1) { |
| 1092 |
return t1 * 2; |
| 1093 |
} |
| 1094 |
}; |
| 1095 |
Function<Integer, Integer> identity = new Function<Integer, Integer>() { |
| 1096 |
@Override |
| 1097 |
public Integer apply(Integer v) { |
| 1098 |
return v; |
| 1099 |
} |
| 1100 |
}; |
| 1101 |
|
| 1102 |
@Test |
| 1103 |
public void normalBehavior() { |
| 1104 |
Observable<String> source = Observable.fromIterable(Arrays.asList( |
| 1105 |
" foo", |
| 1106 |
" FoO ", |
| 1107 |
"baR ", |
| 1108 |
"foO ", |
| 1109 |
" Baz ", |
| 1110 |
" qux ", |
| 1111 |
" bar", |
| 1112 |
" BAR ", |
| 1113 |
"FOO ", |
| 1114 |
"baz ", |
| 1115 |
" bAZ ", |
| 1116 |
" fOo " |
| 1117 |
)); |
| 1118 |
|
| 1119 |
/* |
| 1120 |
* foo FoO foO FOO fOo |
| 1121 |
* baR bar BAR |
| 1122 |
* Baz baz bAZ |
| 1123 |
* qux |
| 1124 |
* |
| 1125 |
*/ |
| 1126 |
Function<String, String> keysel = new Function<String, String>() { |
| 1127 |
@Override |
| 1128 |
public String apply(String t1) { |
| 1129 |
return t1.trim().toLowerCase(); |
| 1130 |
} |
| 1131 |
}; |
| 1132 |
Function<String, String> valuesel = new Function<String, String>() { |
| 1133 |
@Override |
| 1134 |
public String apply(String t1) { |
| 1135 |
return t1 + t1; |
| 1136 |
} |
| 1137 |
}; |
| 1138 |
|
| 1139 |
Observable<String> m = source.groupBy(keysel, valuesel) |
| 1140 |
.flatMap(new Function<GroupedObservable<String, String>, Observable<String>>() { |
| 1141 |
@Override |
| 1142 |
public Observable<String> apply(final GroupedObservable<String, String> g) { |
| 1143 |
System.out.println("-----------> NEXT: " + g.getKey()); |
| 1144 |
return g.take(2).map(new Function<String, String>() { |
| 1145 |
|
| 1146 |
int count; |
| 1147 |
|
| 1148 |
@Override |
| 1149 |
public String apply(String v) { |
| 1150 |
System.out.println(v); |
| 1151 |
return g.getKey() + "-" + count++; |
| 1152 |
} |
| 1153 |
|
| 1154 |
}); |
| 1155 |
} |
| 1156 |
}); |
| 1157 |
|
| 1158 |
TestObserver<String> to = new TestObserver<>(); |
| 1159 |
m.subscribe(to); |
| 1160 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1161 |
System.out.println("ts .get " + to.values()); |
| 1162 |
to.assertNoErrors(); |
| 1163 |
assertEquals(to.values(), |
| 1164 |
Arrays.asList("foo-0", "foo-1", "bar-0", "foo-0", "baz-0", "qux-0", "bar-1", "bar-0", "foo-1", "baz-1", "baz-0", "foo-0")); |
| 1165 |
|
| 1166 |
} |
| 1167 |
|
| 1168 |
@Test |
| 1169 |
public void keySelectorThrows() { |
| 1170 |
Observable<Integer> source = Observable.just(0, 1, 2, 3, 4, 5, 6); |
| 1171 |
|
| 1172 |
Observable<Integer> m = source.groupBy(fail(0), dbl).flatMap(FLATTEN_INTEGER); |
| 1173 |
|
| 1174 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 1175 |
m.subscribe(to); |
| 1176 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1177 |
assertEquals(1, to.errors().size()); |
| 1178 |
to.assertNoValues(); |
| 1179 |
} |
| 1180 |
|
| 1181 |
@Test |
| 1182 |
@SuppressUndeliverable |
| 1183 |
public void valueSelectorThrows() { |
| 1184 |
Observable<Integer> source = Observable.just(0, 1, 2, 3, 4, 5, 6); |
| 1185 |
|
| 1186 |
Observable<Integer> m = source.groupBy(identity, fail(0)).flatMap(FLATTEN_INTEGER); |
| 1187 |
TestObserverEx<Integer> to = new TestObserverEx<>(); |
| 1188 |
m.subscribe(to); |
| 1189 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1190 |
assertEquals(1, to.errors().size()); |
| 1191 |
to.assertNoValues(); |
| 1192 |
|
| 1193 |
} |
| 1194 |
|
| 1195 |
@Test |
| 1196 |
public void innerEscapeCompleted() { |
| 1197 |
Observable<Integer> source = Observable.just(0); |
| 1198 |
|
| 1199 |
Observable<Integer> m = source.groupBy(identity, dbl).flatMap(FLATTEN_INTEGER); |
| 1200 |
|
| 1201 |
TestObserver<Object> to = new TestObserver<>(); |
| 1202 |
m.subscribe(to); |
| 1203 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1204 |
to.assertNoErrors(); |
| 1205 |
System.out.println(to.values()); |
| 1206 |
} |
| 1207 |
|
| 1208 |
/** |
| 1209 |
* Assert we get an IllegalStateException if trying to subscribe to an inner GroupedObservable more than once. |
| 1210 |
*/ |
| 1211 |
@Test |
| 1212 |
public void exceptionIfSubscribeToChildMoreThanOnce() { |
| 1213 |
Observable<Integer> source = Observable.just(0); |
| 1214 |
|
| 1215 |
final AtomicReference<GroupedObservable<Integer, Integer>> inner = new AtomicReference<>(); |
| 1216 |
|
| 1217 |
Observable<GroupedObservable<Integer, Integer>> m = source.groupBy(identity, dbl); |
| 1218 |
|
| 1219 |
m.subscribe(new Consumer<GroupedObservable<Integer, Integer>>() { |
| 1220 |
@Override |
| 1221 |
public void accept(GroupedObservable<Integer, Integer> t1) { |
| 1222 |
inner.set(t1); |
| 1223 |
} |
| 1224 |
}); |
| 1225 |
|
| 1226 |
inner.get().subscribe(); |
| 1227 |
|
| 1228 |
Observer<Integer> o2 = TestHelper.mockObserver(); |
| 1229 |
|
| 1230 |
inner.get().subscribe(o2); |
| 1231 |
|
| 1232 |
verify(o2, never()).onComplete(); |
| 1233 |
verify(o2, never()).onNext(anyInt()); |
| 1234 |
verify(o2).onError(any(IllegalStateException.class)); |
| 1235 |
} |
| 1236 |
|
| 1237 |
@Test |
| 1238 |
@SuppressUndeliverable |
| 1239 |
public void error2() { |
| 1240 |
Observable<Integer> source = Observable.concat(Observable.just(0), |
| 1241 |
Observable.<Integer> error(new TestException("Forced failure"))); |
| 1242 |
|
| 1243 |
Observable<Integer> m = source.groupBy(identity, dbl).flatMap(FLATTEN_INTEGER); |
| 1244 |
|
| 1245 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 1246 |
m.subscribe(to); |
| 1247 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1248 |
assertEquals(1, to.errors().size()); |
| 1249 |
to.assertValueCount(1); |
| 1250 |
} |
| 1251 |
|
| 1252 |
@Test |
| 1253 |
public void groupByBackpressure3() throws InterruptedException { |
| 1254 |
TestObserver<String> to = new TestObserver<>(); |
| 1255 |
|
| 1256 |
Observable.range(1, 4000).groupBy(IS_EVEN2).flatMap(new Function<GroupedObservable<Boolean, Integer>, Observable<String>>() { |
| 1257 |
|
| 1258 |
@Override |
| 1259 |
public Observable<String> apply(final GroupedObservable<Boolean, Integer> g) { |
| 1260 |
return g.doOnComplete(new Action() { |
| 1261 |
|
| 1262 |
@Override |
| 1263 |
public void run() { |
| 1264 |
System.out.println("//////////////////// COMPLETED-A"); |
| 1265 |
} |
| 1266 |
|
| 1267 |
}).observeOn(Schedulers.computation()).map(new Function<Integer, String>() { |
| 1268 |
|
| 1269 |
int c; |
| 1270 |
|
| 1271 |
@Override |
| 1272 |
public String apply(Integer l) { |
| 1273 |
if (g.getKey()) { |
| 1274 |
if (c++ < 400) { |
| 1275 |
try { |
| 1276 |
Thread.sleep(1); |
| 1277 |
} catch (InterruptedException e) { |
| 1278 |
} |
| 1279 |
} |
| 1280 |
return l + " is even."; |
| 1281 |
} else { |
| 1282 |
return l + " is odd."; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
}).doOnComplete(new Action() { |
| 1287 |
|
| 1288 |
@Override |
| 1289 |
public void run() { |
| 1290 |
System.out.println("//////////////////// COMPLETED-B"); |
| 1291 |
} |
| 1292 |
|
| 1293 |
}); |
| 1294 |
} |
| 1295 |
|
| 1296 |
}).doOnEach(new Consumer<Notification<String>>() { |
| 1297 |
|
| 1298 |
@Override |
| 1299 |
public void accept(Notification<String> t1) { |
| 1300 |
System.out.println("NEXT: " + t1); |
| 1301 |
} |
| 1302 |
|
| 1303 |
}).subscribe(to); |
| 1304 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1305 |
to.assertNoErrors(); |
| 1306 |
} |
| 1307 |
|
| 1308 |
@Test |
| 1309 |
public void groupByBackpressure2() throws InterruptedException { |
| 1310 |
|
| 1311 |
TestObserver<String> to = new TestObserver<>(); |
| 1312 |
|
| 1313 |
Observable.range(1, 4000).groupBy(IS_EVEN2).flatMap(new Function<GroupedObservable<Boolean, Integer>, Observable<String>>() { |
| 1314 |
|
| 1315 |
@Override |
| 1316 |
public Observable<String> apply(final GroupedObservable<Boolean, Integer> g) { |
| 1317 |
return g.take(2).observeOn(Schedulers.computation()).map(new Function<Integer, String>() { |
| 1318 |
|
| 1319 |
@Override |
| 1320 |
public String apply(Integer l) { |
| 1321 |
if (g.getKey()) { |
| 1322 |
try { |
| 1323 |
Thread.sleep(1); |
| 1324 |
} catch (InterruptedException e) { |
| 1325 |
} |
| 1326 |
return l + " is even."; |
| 1327 |
} else { |
| 1328 |
return l + " is odd."; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
}); |
| 1333 |
} |
| 1334 |
|
| 1335 |
}).subscribe(to); |
| 1336 |
to.awaitDone(5, TimeUnit.SECONDS); |
| 1337 |
to.assertNoErrors(); |
| 1338 |
} |
| 1339 |
|
| 1340 |
static Function<GroupedObservable<Integer, Integer>, Observable<Integer>> FLATTEN_INTEGER = new Function<GroupedObservable<Integer, Integer>, Observable<Integer>>() { |
| 1341 |
|
| 1342 |
@Override |
| 1343 |
public Observable<Integer> apply(GroupedObservable<Integer, Integer> t) { |
| 1344 |
return t; |
| 1345 |
} |
| 1346 |
|
| 1347 |
}; |
| 1348 |
|
| 1349 |
@Test |
| 1350 |
public void groupByWithNullKey() { |
| 1351 |
final String[] key = new String[]{"uninitialized"}; |
| 1352 |
final List<String> values = new ArrayList<>(); |
| 1353 |
Observable.just("a", "b", "c").groupBy(new Function<String, String>() { |
| 1354 |
|
| 1355 |
@Override |
| 1356 |
public String apply(String value) { |
| 1357 |
return null; |
| 1358 |
} |
| 1359 |
}).subscribe(new Consumer<GroupedObservable<String, String>>() { |
| 1360 |
|
| 1361 |
@Override |
| 1362 |
public void accept(GroupedObservable<String, String> groupedObservable) { |
| 1363 |
key[0] = groupedObservable.getKey(); |
| 1364 |
groupedObservable.subscribe(new Consumer<String>() { |
| 1365 |
|
| 1366 |
@Override |
| 1367 |
public void accept(String s) { |
| 1368 |
values.add(s); |
| 1369 |
} |
| 1370 |
}); |
| 1371 |
} |
| 1372 |
}); |
| 1373 |
assertNull(key[0]); |
| 1374 |
assertEquals(Arrays.asList("a", "b", "c"), values); |
| 1375 |
} |
| 1376 |
|
| 1377 |
@Test |
| 1378 |
public void groupByUnsubscribe() { |
| 1379 |
final Disposable upstream = mock(Disposable.class); |
| 1380 |
Observable<Integer> o = Observable.unsafeCreate( |
| 1381 |
new ObservableSource<Integer>() { |
| 1382 |
@Override |
| 1383 |
public void subscribe(Observer<? super Integer> observer) { |
| 1384 |
observer.onSubscribe(upstream); |
| 1385 |
} |
| 1386 |
} |
| 1387 |
); |
| 1388 |
TestObserver<Object> to = new TestObserver<>(); |
| 1389 |
|
| 1390 |
o.groupBy(new Function<Integer, Integer>() { |
| 1391 |
|
| 1392 |
@Override |
| 1393 |
public Integer apply(Integer integer) { |
| 1394 |
return null; |
| 1395 |
} |
| 1396 |
}).subscribe(to); |
| 1397 |
|
| 1398 |
to.dispose(); |
| 1399 |
|
| 1400 |
verify(upstream).dispose(); |
| 1401 |
} |
| 1402 |
|
| 1403 |
@Test |
| 1404 |
public void groupByShouldPropagateError() { |
| 1405 |
final Throwable e = new RuntimeException("Oops"); |
| 1406 |
final TestObserverEx<Integer> inner1 = new TestObserverEx<>(); |
| 1407 |
final TestObserverEx<Integer> inner2 = new TestObserverEx<>(); |
| 1408 |
|
| 1409 |
final TestObserverEx<GroupedObservable<Integer, Integer>> outer |
| 1410 |
= new TestObserverEx<>(new DefaultObserver<GroupedObservable<Integer, Integer>>() { |
| 1411 |
|
| 1412 |
@Override |
| 1413 |
public void onComplete() { |
| 1414 |
} |
| 1415 |
|
| 1416 |
@Override |
| 1417 |
public void onError(Throwable e) { |
| 1418 |
} |
| 1419 |
|
| 1420 |
@Override |
| 1421 |
public void onNext(GroupedObservable<Integer, Integer> o) { |
| 1422 |
if (o.getKey() == 0) { |
| 1423 |
o.subscribe(inner1); |
| 1424 |
} else { |
| 1425 |
o.subscribe(inner2); |
| 1426 |
} |
| 1427 |
} |
| 1428 |
}); |
| 1429 |
Observable.unsafeCreate( |
| 1430 |
new ObservableSource<Integer>() { |
| 1431 |
@Override |
| 1432 |
public void subscribe(Observer<? super Integer> observer) { |
| 1433 |
observer.onSubscribe(Disposable.empty()); |
| 1434 |
observer.onNext(0); |
| 1435 |
observer.onNext(1); |
| 1436 |
observer.onError(e); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
).groupBy(new Function<Integer, Integer>() { |
| 1440 |
|
| 1441 |
@Override |
| 1442 |
public Integer apply(Integer i) { |
| 1443 |
return i % 2; |
| 1444 |
} |
| 1445 |
}).subscribe(outer); |
| 1446 |
assertEquals(Arrays.asList(e), outer.errors()); |
| 1447 |
assertEquals(Arrays.asList(e), inner1.errors()); |
| 1448 |
assertEquals(Arrays.asList(e), inner2.errors()); |
| 1449 |
} |
| 1450 |
|
| 1451 |
@Test |
| 1452 |
@SuppressUndeliverable |
| 1453 |
public void keySelectorAndDelayError() { |
| 1454 |
Observable.just(1).concatWith(Observable.<Integer>error(new TestException())) |
| 1455 |
.groupBy(Functions.<Integer>identity(), true) |
| 1456 |
.flatMap(new Function<GroupedObservable<Integer, Integer>, ObservableSource<Integer>>() { |
| 1457 |
@Override |
| 1458 |
public ObservableSource<Integer> apply(GroupedObservable<Integer, Integer> g) throws Exception { |
| 1459 |
return g; |
| 1460 |
} |
| 1461 |
}) |
| 1462 |
.test() |
| 1463 |
.assertFailure(TestException.class, 1); |
| 1464 |
} |
| 1465 |
|
| 1466 |
@Test |
| 1467 |
@SuppressUndeliverable |
| 1468 |
public void keyAndValueSelectorAndDelayError() { |
| 1469 |
Observable.just(1).concatWith(Observable.<Integer>error(new TestException())) |
| 1470 |
.groupBy(Functions.<Integer>identity(), Functions.<Integer>identity(), true) |
| 1471 |
.flatMap(new Function<GroupedObservable<Integer, Integer>, ObservableSource<Integer>>() { |
| 1472 |
@Override |
| 1473 |
public ObservableSource<Integer> apply(GroupedObservable<Integer, Integer> g) throws Exception { |
| 1474 |
return g; |
| 1475 |
} |
| 1476 |
}) |
| 1477 |
.test() |
| 1478 |
.assertFailure(TestException.class, 1); |
| 1479 |
} |
| 1480 |
|
| 1481 |
@Test |
| 1482 |
public void dispose() { |
| 1483 |
TestHelper.checkDisposed(Observable.just(1).groupBy(Functions.justFunction(1))); |
| 1484 |
|
| 1485 |
Observable.just(1) |
| 1486 |
.groupBy(Functions.justFunction(1)) |
| 1487 |
.doOnNext(new Consumer<GroupedObservable<Integer, Integer>>() { |
| 1488 |
@Override |
| 1489 |
public void accept(GroupedObservable<Integer, Integer> g) throws Exception { |
| 1490 |
TestHelper.checkDisposed(g); |
| 1491 |
} |
| 1492 |
}) |
| 1493 |
.test(); |
| 1494 |
} |
| 1495 |
|
| 1496 |
@Test |
| 1497 |
public void reentrantComplete() { |
| 1498 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 1499 |
|
| 1500 |
TestObserver<Integer> to = new TestObserver<Integer>() { |
| 1501 |
@Override |
| 1502 |
public void onNext(Integer t) { |
| 1503 |
super.onNext(t); |
| 1504 |
if (t == 1) { |
| 1505 |
ps.onComplete(); |
| 1506 |
} |
| 1507 |
} |
| 1508 |
}; |
| 1509 |
|
| 1510 |
Observable.merge(ps.groupBy(Functions.justFunction(1))) |
| 1511 |
.subscribe(to); |
| 1512 |
|
| 1513 |
ps.onNext(1); |
| 1514 |
|
| 1515 |
to.assertResult(1); |
| 1516 |
} |
| 1517 |
|
| 1518 |
@Test |
| 1519 |
public void reentrantCompleteCancel() { |
| 1520 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 1521 |
|
| 1522 |
TestObserverEx<Integer> to = new TestObserverEx<Integer>() { |
| 1523 |
@Override |
| 1524 |
public void onNext(Integer t) { |
| 1525 |
super.onNext(t); |
| 1526 |
if (t == 1) { |
| 1527 |
ps.onComplete(); |
| 1528 |
dispose(); |
| 1529 |
} |
| 1530 |
} |
| 1531 |
}; |
| 1532 |
|
| 1533 |
Observable.merge(ps.groupBy(Functions.justFunction(1))) |
| 1534 |
.subscribe(to); |
| 1535 |
|
| 1536 |
ps.onNext(1); |
| 1537 |
|
| 1538 |
to.assertSubscribed().assertValue(1).assertNoErrors().assertNotComplete(); |
| 1539 |
} |
| 1540 |
|
| 1541 |
@Test |
| 1542 |
public void delayErrorSimpleComplete() { |
| 1543 |
Observable.just(1) |
| 1544 |
.groupBy(Functions.justFunction(1), true) |
| 1545 |
.flatMap(Functions.<Observable<Integer>>identity()) |
| 1546 |
.test() |
| 1547 |
.assertResult(1); |
| 1548 |
} |
| 1549 |
|
| 1550 |
@Test |
| 1551 |
public void cancelOverFlatmapRace() { |
| 1552 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1553 |
|
| 1554 |
final TestObserver<Integer> to = new TestObserver<>(); |
| 1555 |
|
| 1556 |
final PublishSubject<Integer> ps = PublishSubject.create(); |
| 1557 |
|
| 1558 |
ps.groupBy(new Function<Integer, Integer>() { |
| 1559 |
@Override |
| 1560 |
public Integer apply(Integer v) throws Throwable { |
| 1561 |
return v % 10; |
| 1562 |
} |
| 1563 |
}) |
| 1564 |
.flatMap(new Function<GroupedObservable<Integer, Integer>, ObservableSource<Integer>>() { |
| 1565 |
@Override |
| 1566 |
public ObservableSource<Integer> apply(GroupedObservable<Integer, Integer> v) |
| 1567 |
throws Throwable { |
| 1568 |
return v; |
| 1569 |
} |
| 1570 |
}) |
| 1571 |
.subscribe(to); |
| 1572 |
|
| 1573 |
Runnable r1 = new Runnable() { |
| 1574 |
@Override |
| 1575 |
public void run() { |
| 1576 |
for (int j = 0; j < 1000; j++) { |
| 1577 |
ps.onNext(j); |
| 1578 |
} |
| 1579 |
} |
| 1580 |
}; |
| 1581 |
|
| 1582 |
Runnable r2 = new Runnable() { |
| 1583 |
@Override |
| 1584 |
public void run() { |
| 1585 |
to.dispose(); |
| 1586 |
} |
| 1587 |
}; |
| 1588 |
|
| 1589 |
TestHelper.race(r1, r2); |
| 1590 |
|
| 1591 |
assertFalse("Round " + i, ps.hasObservers()); |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
@Test |
| 1596 |
public void abandonedGroupsNoDataloss() { |
| 1597 |
final List<GroupedObservable<Integer, Integer>> groups = new ArrayList<>(); |
| 1598 |
|
| 1599 |
Observable.range(1, 1000) |
| 1600 |
.groupBy(new Function<Integer, Integer>() { |
| 1601 |
@Override |
| 1602 |
public Integer apply(Integer v) throws Throwable { |
| 1603 |
return v % 10; |
| 1604 |
} |
| 1605 |
}) |
| 1606 |
.doOnNext(new Consumer<GroupedObservable<Integer, Integer>>() { |
| 1607 |
@Override |
| 1608 |
public void accept(GroupedObservable<Integer, Integer> v) throws Throwable { |
| 1609 |
groups.add(v); |
| 1610 |
} |
| 1611 |
}) |
| 1612 |
.test() |
| 1613 |
.assertValueCount(1000) |
| 1614 |
.assertComplete() |
| 1615 |
.assertNoErrors(); |
| 1616 |
|
| 1617 |
Observable.concat(groups) |
| 1618 |
.test() |
| 1619 |
.assertValueCount(1000) |
| 1620 |
.assertNoErrors() |
| 1621 |
.assertComplete(); |
| 1622 |
} |
| 1623 |
|
| 1624 |
@Test |
| 1625 |
public void newGroupValueSelectorFails() { |
| 1626 |
TestObserver<Object> to1 = new TestObserver<>(); |
| 1627 |
final TestObserver<Object> to2 = new TestObserver<>(); |
| 1628 |
|
| 1629 |
Observable.just(1) |
| 1630 |
.groupBy(Functions.<Integer>identity(), new Function<Integer, Object>() { |
| 1631 |
@Override |
| 1632 |
public Object apply(Integer v) throws Throwable { |
| 1633 |
throw new TestException(); |
| 1634 |
} |
| 1635 |
}) |
| 1636 |
.doOnNext(new Consumer<GroupedObservable<Integer, Object>>() { |
| 1637 |
@Override |
| 1638 |
public void accept(GroupedObservable<Integer, Object> g) throws Throwable { |
| 1639 |
g.subscribe(to2); |
| 1640 |
} |
| 1641 |
}) |
| 1642 |
.subscribe(to1); |
| 1643 |
|
| 1644 |
to1.assertValueCount(1) |
| 1645 |
.assertError(TestException.class) |
| 1646 |
.assertNotComplete(); |
| 1647 |
|
| 1648 |
to2.assertFailure(TestException.class); |
| 1649 |
} |
| 1650 |
|
| 1651 |
@Test |
| 1652 |
public void existingGroupValueSelectorFails() { |
| 1653 |
TestObserver<Object> to1 = new TestObserver<>(); |
| 1654 |
final TestObserver<Object> to2 = new TestObserver<>(); |
| 1655 |
|
| 1656 |
Observable.just(1, 2) |
| 1657 |
.groupBy(Functions.justFunction(1), new Function<Integer, Object>() { |
| 1658 |
@Override |
| 1659 |
public Object apply(Integer v) throws Throwable { |
| 1660 |
if (v == 2) { |
| 1661 |
throw new TestException(); |
| 1662 |
} |
| 1663 |
return v; |
| 1664 |
} |
| 1665 |
}) |
| 1666 |
.doOnNext(new Consumer<GroupedObservable<Integer, Object>>() { |
| 1667 |
@Override |
| 1668 |
public void accept(GroupedObservable<Integer, Object> g) throws Throwable { |
| 1669 |
g.subscribe(to2); |
| 1670 |
} |
| 1671 |
}) |
| 1672 |
.subscribe(to1); |
| 1673 |
|
| 1674 |
to1.assertValueCount(1) |
| 1675 |
.assertError(TestException.class) |
| 1676 |
.assertNotComplete(); |
| 1677 |
|
| 1678 |
to2.assertFailure(TestException.class, 1); |
| 1679 |
} |
| 1680 |
|
| 1681 |
@Test |
| 1682 |
public void doubleOnSubscribe() { |
| 1683 |
TestHelper.checkDoubleOnSubscribeObservable(o -> o.groupBy(v -> v)); |
| 1684 |
} |
| 1685 |
|
| 1686 |
@Test |
| 1687 |
public void nullKeyDisposeGroup() { |
| 1688 |
Observable.just(1) |
| 1689 |
.groupBy(v -> null) |
| 1690 |
.flatMap(v -> v.take(1)) |
| 1691 |
.test() |
| 1692 |
.assertResult(1); |
| 1693 |
} |
| 1694 |
|
| 1695 |
@Test |
| 1696 |
public void groupSubscribeOnNextRace() throws Throwable { |
| 1697 |
for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { |
| 1698 |
BehaviorSubject<Integer> bs = BehaviorSubject.createDefault(1); |
| 1699 |
CountDownLatch cdl = new CountDownLatch(1); |
| 1700 |
|
| 1701 |
bs.groupBy(v -> 1) |
| 1702 |
.doOnNext(g -> { |
| 1703 |
TestHelper.raceOther(() -> { |
| 1704 |
g.test(); |
| 1705 |
}, cdl); |
| 1706 |
}) |
| 1707 |
.test(); |
| 1708 |
|
| 1709 |
cdl.await(); |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
@Test |
| 1714 |
public void abandonedGroupDispose() { |
| 1715 |
AtomicReference<Observable<Integer>> ref = new AtomicReference<>(); |
| 1716 |
|
| 1717 |
Observable.just(1) |
| 1718 |
.groupBy(v -> 1) |
| 1719 |
.doOnNext(ref::set) |
| 1720 |
.test(); |
| 1721 |
|
| 1722 |
ref.get().take(1).test().assertResult(1); |
| 1723 |
} |
| 1724 |
|
| 1725 |
@Test |
| 1726 |
public void delayErrorCompleteMoreWorkInGroup() { |
| 1727 |
PublishSubject<Integer> ps = PublishSubject.create(); |
| 1728 |
|
| 1729 |
TestObserver<Integer> to = ps.groupBy(v -> 1, true) |
| 1730 |
.flatMap(g -> g.doOnNext(v -> { |
| 1731 |
if (v == 1) { |
| 1732 |
ps.onNext(2); |
| 1733 |
ps.onComplete(); |
| 1734 |
} |
| 1735 |
}) |
| 1736 |
) |
| 1737 |
.test() |
| 1738 |
; |
| 1739 |
|
| 1740 |
ps.onNext(1); |
| 1741 |
|
| 1742 |
to.assertResult(1, 2); |
| 1743 |
} |
| 1744 |
} |