forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSubject.java
More file actions
163 lines (138 loc) · 4.94 KB
/
Copy pathTestSubject.java
File metadata and controls
163 lines (138 loc) · 4.94 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* 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.subjects;
import java.util.concurrent.TimeUnit;
import rx.Observer;
import rx.Scheduler;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.internal.operators.NotificationLite;
import rx.schedulers.TestScheduler;
import rx.subjects.SubjectSubscriptionManager.SubjectObserver;
/**
* A variety of Subject that is useful for testing purposes. It operates on a {@link TestScheduler} and allows
* you to precisely time emissions and notifications to the Subject's subscribers.
*
* @param <T>
* the type of item observed by and emitted by the subject
*/
public final class TestSubject<T> extends Subject<T, T> {
/**
* Creates and returns a new {@code TestSubject}.
*
* @param <T> the value type
* @param scheduler a {@link TestScheduler} on which to operate this Subject
* @return the new {@code TestSubject}
*/
public static <T> TestSubject<T> create(TestScheduler scheduler) {
final SubjectSubscriptionManager<T> state = new SubjectSubscriptionManager<T>();
state.onAdded = new Action1<SubjectObserver<T>>() {
@Override
public void call(SubjectObserver<T> o) {
o.emitFirst(state.get(), state.nl);
}
};
state.onTerminated = state.onAdded;
return new TestSubject<T>(state, state, scheduler);
}
private final SubjectSubscriptionManager<T> state;
private final Scheduler.Worker innerScheduler;
protected TestSubject(OnSubscribe<T> onSubscribe, SubjectSubscriptionManager<T> state, TestScheduler scheduler) {
super(onSubscribe);
this.state = state;
this.innerScheduler = scheduler.createWorker();
}
@Override
public void onCompleted() {
onCompleted(innerScheduler.now());
}
private void _onCompleted() {
if (state.active) {
for (SubjectObserver<T> bo : state.terminate(NotificationLite.instance().completed())) {
bo.onCompleted();
}
}
}
/**
* Schedule a call to the {@code onCompleted} methods of all of the subscribers to this Subject to begin at
* a particular time.
*
* @param timeInMilliseconds
* the time at which to begin calling the {@code onCompleted} methods of the subscribers
*/
public void onCompleted(long timeInMilliseconds) {
innerScheduler.schedule(new Action0() {
@Override
public void call() {
_onCompleted();
}
}, timeInMilliseconds, TimeUnit.MILLISECONDS);
}
@Override
public void onError(final Throwable e) {
onError(e, innerScheduler.now());
}
private void _onError(final Throwable e) {
if (state.active) {
for (SubjectObserver<T> bo : state.terminate(NotificationLite.instance().error(e))) {
bo.onError(e);
}
}
}
/**
* Schedule a call to the {@code onError} methods of all of the subscribers to this Subject to begin at
* a particular time.
*
* @param e
* the {@code Throwable} to pass to the {@code onError} methods of the subscribers
* @param timeInMilliseconds
* the time at which to begin calling the {@code onError} methods of the subscribers
*/
public void onError(final Throwable e, long timeInMilliseconds) {
innerScheduler.schedule(new Action0() {
@Override
public void call() {
_onError(e);
}
}, timeInMilliseconds, TimeUnit.MILLISECONDS);
}
@Override
public void onNext(T v) {
onNext(v, innerScheduler.now());
}
private void _onNext(T v) {
for (Observer<? super T> o : state.observers()) {
o.onNext(v);
}
}
/**
* Emit an item to all of the subscribers to this Subject at a particular time.
*
* @param v
* the item to emit
* @param timeInMilliseconds
* the time at which to begin calling the {@code onNext} methods of the subscribers in order to emit
* the item
*/
public void onNext(final T v, long timeInMilliseconds) {
innerScheduler.schedule(new Action0() {
@Override
public void call() {
_onNext(v);
}
}, timeInMilliseconds, TimeUnit.MILLISECONDS);
}
}