| 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.flowable; |
| 15 |
|
| 16 |
import org.junit.Test; |
| 17 |
import org.reactivestreams.Publisher; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.flowable.FlowableEventStream.Event; |
| 21 |
import io.reactivex.rxjava3.flowables.GroupedFlowable; |
| 22 |
import io.reactivex.rxjava3.functions.*; |
| 23 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 24 |
|
| 25 |
public class FlowableGroupByTests extends RxJavaTest { |
| 26 |
|
| 27 |
@Test |
| 28 |
public void takeUnsubscribesOnGroupBy() { |
| 29 |
Flowable.merge( |
| 30 |
FlowableEventStream.getEventStream("HTTP-ClusterA", 50), |
| 31 |
FlowableEventStream.getEventStream("HTTP-ClusterB", 20) |
| 32 |
) |
| 33 |
// group by type (2 clusters) |
| 34 |
.groupBy(new Function<Event, Object>() { |
| 35 |
@Override |
| 36 |
public Object apply(Event event) { |
| 37 |
return event.type; |
| 38 |
} |
| 39 |
}) |
| 40 |
.take(1) |
| 41 |
.blockingForEach(new Consumer<GroupedFlowable<Object, Event>>() { |
| 42 |
@Override |
| 43 |
public void accept(GroupedFlowable<Object, Event> v) { |
| 44 |
System.out.println(v); |
| 45 |
v.take(1).subscribe(); // FIXME groups need consumption to a certain degree to cancel upstream |
| 46 |
} |
| 47 |
}); |
| 48 |
|
| 49 |
System.out.println("**** finished"); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void takeUnsubscribesOnFlatMapOfGroupBy() { |
| 54 |
Flowable.merge( |
| 55 |
FlowableEventStream.getEventStream("HTTP-ClusterA", 50), |
| 56 |
FlowableEventStream.getEventStream("HTTP-ClusterB", 20) |
| 57 |
) |
| 58 |
// group by type (2 clusters) |
| 59 |
.groupBy(new Function<Event, Object>() { |
| 60 |
@Override |
| 61 |
public Object apply(Event event) { |
| 62 |
return event.type; |
| 63 |
} |
| 64 |
}) |
| 65 |
.flatMap(new Function<GroupedFlowable<Object, Event>, Publisher<Object>>() { |
| 66 |
@Override |
| 67 |
public Publisher<Object> apply(GroupedFlowable<Object, 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 v) { |
| 80 |
System.out.println(v); |
| 81 |
} |
| 82 |
}); |
| 83 |
|
| 84 |
System.out.println("**** finished"); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void groupsCompleteAsSoonAsMainCompletes() { |
| 89 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 90 |
|
| 91 |
Flowable.range(0, 20) |
| 92 |
.groupBy(new Function<Integer, Integer>() { |
| 93 |
@Override |
| 94 |
public Integer apply(Integer i) { |
| 95 |
return i % 5; |
| 96 |
} |
| 97 |
}) |
| 98 |
.concatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<Integer>>() { |
| 99 |
@Override |
| 100 |
public Flowable<Integer> apply(GroupedFlowable<Integer, Integer> v) { |
| 101 |
return v; |
| 102 |
} |
| 103 |
}, 20) // need to prefetch as many groups as groupBy produces to avoid MBE |
| 104 |
.subscribe(ts); |
| 105 |
|
| 106 |
// Behavior change: this now counts as group abandonment because concatMap |
| 107 |
// doesn't subscribe to the 2nd+ emitted groups immediately |
| 108 |
ts.assertValues( |
| 109 |
0, 5, 10, 15, // First group is okay |
| 110 |
// any other group gets abandoned so we get 16 one-element group |
| 111 |
1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19 |
| 112 |
); |
| 113 |
ts.assertComplete(); |
| 114 |
ts.assertNoErrors(); |
| 115 |
} |
| 116 |
} |