forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSubscriberTest.java
More file actions
163 lines (136 loc) · 5.44 KB
/
Copy pathTestSubscriberTest.java
File metadata and controls
163 lines (136 loc) · 5.44 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.observers;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.InOrder;
import rx.Observable;
import rx.Observer;
import rx.functions.Action0;
import rx.subjects.PublishSubject;
public class TestSubscriberTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testAssert() {
Observable<Integer> oi = Observable.from(Arrays.asList(1, 2));
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
oi.subscribe(o);
o.assertReceivedOnNext(Arrays.asList(1, 2));
assertEquals(2, o.getOnNextEvents().size());
o.assertTerminalEvent();
}
@Test
public void testAssertNotMatchCount() {
Observable<Integer> oi = Observable.from(Arrays.asList(1, 2));
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
oi.subscribe(o);
thrown.expect(AssertionError.class);
thrown.expectMessage("Number of items does not match. Provided: 1 Actual: 2");
o.assertReceivedOnNext(Arrays.asList(1));
assertEquals(2, o.getOnNextEvents().size());
o.assertTerminalEvent();
}
@Test
public void testAssertNotMatchValue() {
Observable<Integer> oi = Observable.from(Arrays.asList(1, 2));
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
oi.subscribe(o);
thrown.expect(AssertionError.class);
thrown.expectMessage("Value at index: 1 expected to be [3] (Integer) but was: [2] (Integer)");
o.assertReceivedOnNext(Arrays.asList(1, 3));
assertEquals(2, o.getOnNextEvents().size());
o.assertTerminalEvent();
}
@Test
public void testAssertTerminalEventNotReceived() {
PublishSubject<Integer> p = PublishSubject.create();
TestSubscriber<Integer> o = new TestSubscriber<Integer>();
p.subscribe(o);
p.onNext(1);
p.onNext(2);
thrown.expect(AssertionError.class);
thrown.expectMessage("No terminal events received.");
o.assertReceivedOnNext(Arrays.asList(1, 2));
assertEquals(2, o.getOnNextEvents().size());
o.assertTerminalEvent();
}
@Test
public void testWrappingMock() {
Observable<Integer> oi = Observable.from(Arrays.asList(1, 2));
@SuppressWarnings("unchecked")
Observer<Integer> mockObserver = mock(Observer.class);
oi.subscribe(new TestSubscriber<Integer>(mockObserver));
InOrder inOrder = inOrder(mockObserver);
inOrder.verify(mockObserver, times(1)).onNext(1);
inOrder.verify(mockObserver, times(1)).onNext(2);
inOrder.verify(mockObserver, times(1)).onCompleted();
inOrder.verifyNoMoreInteractions();
}
@Test
public void testWrappingMockWhenUnsubscribeInvolved() {
Observable<Integer> oi = Observable.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9)).take(2);
@SuppressWarnings("unchecked")
Observer<Integer> mockObserver = mock(Observer.class);
oi.subscribe(new TestSubscriber<Integer>(mockObserver));
InOrder inOrder = inOrder(mockObserver);
inOrder.verify(mockObserver, times(1)).onNext(1);
inOrder.verify(mockObserver, times(1)).onNext(2);
inOrder.verify(mockObserver, times(1)).onCompleted();
inOrder.verifyNoMoreInteractions();
}
@Test
public void testAssertError() {
RuntimeException e = new RuntimeException("Oops");
TestSubscriber<Object> subscriber = new TestSubscriber<Object>();
Observable.error(e).subscribe(subscriber);
subscriber.assertError(e);
}
@Test
public void testAwaitTerminalEventWithDuration() {
TestSubscriber<Object> ts = new TestSubscriber<Object>();
Observable.just(1).subscribe(ts);
ts.awaitTerminalEvent(1, TimeUnit.SECONDS);
ts.assertTerminalEvent();
}
@Test
public void testAwaitTerminalEventWithDurationAndUnsubscribeOnTimeout() {
TestSubscriber<Object> ts = new TestSubscriber<Object>();
final AtomicBoolean unsub = new AtomicBoolean(false);
Observable.just(1)
//
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
unsub.set(true);
}
})
//
.delay(1000, TimeUnit.MILLISECONDS).subscribe(ts);
ts.awaitTerminalEventAndUnsubscribeOnTimeout(100, TimeUnit.MILLISECONDS);
assertTrue(unsub.get());
}
}