| 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.observable; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.functions.*; |
| 20 |
import io.reactivex.rxjava3.observable.ObservableEventStream.Event; |
| 21 |
import io.reactivex.rxjava3.observables.GroupedObservable; |
| 22 |
|
| 23 |
public class ObservableGroupByTests extends RxJavaTest { |
| 24 |
|
| 25 |
@Test |
| 26 |
public void takeUnsubscribesOnGroupBy() throws Exception { |
| 27 |
Observable.merge( |
| 28 |
ObservableEventStream.getEventStream("HTTP-ClusterA", 50), |
| 29 |
ObservableEventStream.getEventStream("HTTP-ClusterB", 20) |
| 30 |
) |
| 31 |
// group by type (2 clusters) |
| 32 |
.groupBy(new Function<Event, String>() { |
| 33 |
@Override |
| 34 |
public String apply(Event event) { |
| 35 |
return event.type; |
| 36 |
} |
| 37 |
}) |
| 38 |
.take(1) |
| 39 |
.blockingForEach(new Consumer<GroupedObservable<String, Event>>() { |
| 40 |
@Override |
| 41 |
public void accept(GroupedObservable<String, Event> v) { |
| 42 |
System.out.println(v); |
| 43 |
v.take(1).subscribe(); // FIXME groups need consumption to a certain degree to cancel upstream |
| 44 |
} |
| 45 |
}); |
| 46 |
|
| 47 |
System.out.println("**** finished"); |
| 48 |
|
| 49 |
Thread.sleep(200); // make sure the event streams receive their interrupt |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void takeUnsubscribesOnFlatMapOfGroupBy() throws Exception { |
| 54 |
Observable.merge( |
| 55 |
ObservableEventStream.getEventStream("HTTP-ClusterA", 50), |
| 56 |
ObservableEventStream.getEventStream("HTTP-ClusterB", 20) |
| 57 |
) |
| 58 |
// group by type (2 clusters) |
| 59 |
.groupBy(new Function<Event, String>() { |
| 60 |
@Override |
| 61 |
public String apply(Event event) { |
| 62 |
return event.type; |
| 63 |
} |
| 64 |
}) |
| 65 |
.flatMap(new Function<GroupedObservable<String, Event>, Observable<Object>>() { |
| 66 |
@Override |
| 67 |
public Observable<Object> apply(GroupedObservable<String, Event> g) { |
| 68 |
return g.map(new Function<Event, Object>() { |
| 69 |
@Override |
| 70 |
public Object apply(Event event) { |
| 71 |
return event.instanceId + " - " + event.values.get("count200"); |
| 72 |
} |
| 73 |
}); |
| 74 |
} |
| 75 |
}) |
| 76 |
.take(20) |
| 77 |
.blockingForEach(new Consumer<Object>() { |
| 78 |
@Override |
| 79 |
public void accept(Object pv) { |
| 80 |
System.out.println(pv); |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
System.out.println("**** finished"); |
| 85 |
|
| 86 |
Thread.sleep(200); // make sure the event streams receive their interrupt |
| 87 |
} |
| 88 |
} |