Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
100 lines (87 loc) · 3.57 KB

File metadata and controls

100 lines (87 loc) · 3.57 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import rx.Observable.OnSubscribeFunc;
import rx.Scheduler.Inner;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
/**
* Utility for retrieving a mock eventstream for testing.
*/
public class EventStream {
public static Observable<Event> getEventStream(final String type, final int numInstances) {
return Observable.create(new OnSubscribeFunc<Event>() {
@Override
public Subscription onSubscribe(final Observer<? super Event> observer) {
// run on a background thread inside the OnSubscribeFunc so unsubscribe works
return Schedulers.newThread().schedule(new Action1<Inner>() {
@Override
public void call(Inner inner) {
inner.schedule(new Action1<Inner>() {
@Override
public void call(Inner inner) {
while (!(inner.isUnsubscribed() || Thread.currentThread().isInterrupted())) {
observer.onNext(randomEvent(type, numInstances));
try {
// slow it down somewhat
Thread.sleep(50);
} catch (InterruptedException e) {
observer.onError(e);
}
}
observer.onCompleted();
}
});
}
});
}
});
}
public static Event randomEvent(String type, int numInstances) {
Map<String, Object> values = new LinkedHashMap<String, Object>();
values.put("count200", randomIntFrom0to(4000));
values.put("count4xx", randomIntFrom0to(300));
values.put("count5xx", randomIntFrom0to(500));
return new Event(type, "instance_" + randomIntFrom0to(numInstances), values);
}
private static int randomIntFrom0to(int max) {
// XORShift instead of Math.random http://javamex.com/tutorials/random_numbers/xorshift.shtml
long x = System.nanoTime();
x ^= (x << 21);
x ^= (x >>> 35);
x ^= (x << 4);
return Math.abs((int) x % max);
}
public static class Event {
public final String type;
public final String instanceId;
public final Map<String, Object> values;
/**
* @param type
* @param instanceId
* @param values
* This does NOT deep-copy, so do not mutate this Map after passing it in.
*/
public Event(String type, String instanceId, Map<String, Object> values) {
this.type = type;
this.instanceId = instanceId;
this.values = Collections.unmodifiableMap(values);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.