| 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 java.util.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.functions.Consumer; |
| 20 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 21 |
|
| 22 |
/** |
| 23 |
* Utility for retrieving a mock eventstream for testing. |
| 24 |
*/ |
| 25 |
public final class FlowableEventStream { |
| 26 |
private FlowableEventStream() { |
| 27 |
throw new IllegalStateException("No instances!"); |
| 28 |
} |
| 29 |
public static Flowable<Event> getEventStream(final String type, final int numInstances) { |
| 30 |
|
| 31 |
return Flowable.<Event>generate(new EventConsumer(type, numInstances)) |
| 32 |
.subscribeOn(Schedulers.newThread()); |
| 33 |
} |
| 34 |
|
| 35 |
public static Event randomEvent(String type, int numInstances) { |
| 36 |
Map<String, Object> values = new LinkedHashMap<>(); |
| 37 |
values.put("count200", randomIntFrom0to(4000)); |
| 38 |
values.put("count4xx", randomIntFrom0to(300)); |
| 39 |
values.put("count5xx", randomIntFrom0to(500)); |
| 40 |
return new Event(type, "instance_" + randomIntFrom0to(numInstances), values); |
| 41 |
} |
| 42 |
|
| 43 |
private static int randomIntFrom0to(int max) { |
| 44 |
// XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml |
| 45 |
long x = System.nanoTime(); |
| 46 |
x ^= (x << 21); |
| 47 |
x ^= (x >>> 35); |
| 48 |
x ^= (x << 4); |
| 49 |
return Math.abs((int) x % max); |
| 50 |
} |
| 51 |
|
| 52 |
static final class EventConsumer implements Consumer<Emitter<Event>> { |
| 53 |
private final String type; |
| 54 |
private final int numInstances; |
| 55 |
|
| 56 |
EventConsumer(String type, int numInstances) { |
| 57 |
this.type = type; |
| 58 |
this.numInstances = numInstances; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void accept(Emitter<Event> s) { |
| 63 |
s.onNext(randomEvent(type, numInstances)); |
| 64 |
try { |
| 65 |
// slow it down somewhat |
| 66 |
Thread.sleep(50); |
| 67 |
} catch (InterruptedException e) { |
| 68 |
Thread.currentThread().interrupt(); |
| 69 |
s.onError(e); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public static class Event { |
| 75 |
public final String type; |
| 76 |
public final String instanceId; |
| 77 |
public final Map<String, Object> values; |
| 78 |
|
| 79 |
/** |
| 80 |
* Construct an event with the provided parameters. |
| 81 |
* @param type the event type |
| 82 |
* @param instanceId the instance identifier |
| 83 |
* @param values |
| 84 |
* This does NOT deep-copy, so do not mutate this Map after passing it in. |
| 85 |
*/ |
| 86 |
public Event(String type, String instanceId, Map<String, Object> values) { |
| 87 |
this.type = type; |
| 88 |
this.instanceId = instanceId; |
| 89 |
this.values = Collections.unmodifiableMap(values); |
| 90 |
} |
| 91 |
} |
| 92 |
} |