1 package com.github.davidmoten.rx.testing;
2
3 import java.util.List;
4 import java.util.concurrent.TimeUnit;
5
6 import rx.Observable;
7 import rx.Producer;
8 import rx.Subscriber;
9 import rx.functions.Action0;
10 import rx.functions.Func1;
11 import rx.observers.TestSubscriber;
12
13 public class TestSubscriber2<T> extends Subscriber<T> {
14
15 private final TestSubscriber<T> ts;
16
17 private TestSubscriber2(TestSubscriber<T> ts) {
18 this.ts = ts;
19 }
20
21 public static <T> TestSubscriber2<T> createWithRequest(long initialRequest) {
22 TestSubscriber<T> t1 = new TestSubscriber<T>(initialRequest);
23 TestSubscriber2<T> t2 = new TestSubscriber2<T>(t1);
24 t2.add(t1);
25 return t2;
26 }
27
28 static <T> Func1<Observable<T>, TestSubscriber2<T>> test() {
29 return testWithRequest(Long.MAX_VALUE);
30 }
31
32 static <T> Func1<Observable<T>, TestSubscriber2<T>> testWithRequest(final long initialRequest) {
33 return new Func1<Observable<T>, TestSubscriber2<T>>() {
34
35 @Override
36 public TestSubscriber2<T> call(Observable<T> o) {
37 TestSubscriber2<T> ts2 = createWithRequest(initialRequest);
38 o.subscribe(ts2.ts);
39 return ts2;
40 }
41
42 };
43 }
44
45 public void onStart() {
46 ts.onStart();
47 }
48
49 public void onCompleted() {
50 ts.onCompleted();
51 }
52
53 public void setProducer(Producer p) {
54 ts.setProducer(p);
55 }
56
57 public final int getCompletions() {
58 return ts.getCompletions();
59 }
60
61 public void onError(Throwable e) {
62 ts.onError(e);
63 }
64
65 public List<Throwable> getOnErrorEvents() {
66 return ts.getOnErrorEvents();
67 }
68
69 public void onNext(T t) {
70 ts.onNext(t);
71 }
72
73 public String toString() {
74 return ts.toString();
75 }
76
77 public final int getValueCount() {
78 return ts.getValueCount();
79 }
80
81 public TestSubscriber2<T> requestMore(long n) {
82 ts.requestMore(n);
83 return this;
84 }
85
86 public List<T> getOnNextEvents() {
87 return ts.getOnNextEvents();
88 }
89
90 public TestSubscriber2<T> assertReceivedOnNext(List<T> items) {
91 ts.assertReceivedOnNext(items);
92 return this;
93 }
94
95 public final boolean awaitValueCount(int expected, long timeout, TimeUnit unit) {
96 return ts.awaitValueCount(expected, timeout, unit);
97 }
98
99 public TestSubscriber2<T> assertTerminalEvent() {
100 ts.assertTerminalEvent();
101 return this;
102 }
103
104 public TestSubscriber2<T> assertUnsubscribed() {
105 ts.assertUnsubscribed();
106 return this;
107 }
108
109 public TestSubscriber2<T> assertNoErrors() {
110 ts.assertNoErrors();
111 return this;
112 }
113
114 public TestSubscriber2<T> awaitTerminalEvent() {
115 ts.awaitTerminalEvent();
116 return this;
117 }
118
119 public TestSubscriber2<T> awaitTerminalEvent(long timeout, TimeUnit unit) {
120 ts.awaitTerminalEvent(timeout, unit);
121 return this;
122 }
123
124 public TestSubscriber2<T> awaitTerminalEventAndUnsubscribeOnTimeout(long timeout,
125 TimeUnit unit) {
126 ts.awaitTerminalEventAndUnsubscribeOnTimeout(timeout, unit);
127 return this;
128 }
129
130 public Thread getLastSeenThread() {
131 return ts.getLastSeenThread();
132 }
133
134 public TestSubscriber2<T> assertCompleted() {
135 ts.assertCompleted();
136 return this;
137 }
138
139 public TestSubscriber2<T> assertNotCompleted() {
140 ts.assertNotCompleted();
141 return this;
142 }
143
144 public TestSubscriber2<T> assertError(Class<? extends Throwable> clazz) {
145 ts.assertError(clazz);
146 return this;
147 }
148
149 public TestSubscriber2<T> assertError(Throwable throwable) {
150 ts.assertError(throwable);
151 return this;
152 }
153
154 public TestSubscriber2<T> assertNoTerminalEvent() {
155 ts.assertNoTerminalEvent();
156 return this;
157 }
158
159 public TestSubscriber2<T> assertNoValues() {
160 ts.assertNoValues();
161 return this;
162 }
163
164 public TestSubscriber2<T> assertValueCount(int count) {
165 ts.assertValueCount(count);
166 return this;
167 }
168
169 public TestSubscriber2<T> assertValues(T... values) {
170 ts.assertValues(values);
171 return this;
172 }
173
174 public TestSubscriber2<T> assertValue(T value) {
175 ts.assertValue(value);
176 return this;
177 }
178
179 public final TestSubscriber2<T> assertValuesAndClear(T expectedFirstValue,
180 T... expectedRestValues) {
181 ts.assertValuesAndClear(expectedFirstValue, expectedRestValues);
182 return this;
183 }
184
185 public final TestSubscriber2<T> perform(Action0 action) {
186 action.call();
187 return this;
188 }
189
190 }