forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperatorTakeUntilPredicateTest.java
More file actions
153 lines (132 loc) · 4.88 KB
/
Copy pathOperatorTakeUntilPredicateTest.java
File metadata and controls
153 lines (132 loc) · 4.88 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
/**
* 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.internal.operators;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import java.util.Arrays;
import org.junit.*;
import rx.*;
import rx.exceptions.TestException;
import rx.functions.Func1;
import rx.internal.util.UtilityFunctions;
import rx.observers.TestSubscriber;
;
public class OperatorTakeUntilPredicateTest {
@Test
public void takeEmpty() {
@SuppressWarnings("unchecked")
Observer<Object> o = mock(Observer.class);
Observable.empty().takeUntil(UtilityFunctions.alwaysTrue()).subscribe(o);
verify(o, never()).onNext(any());
verify(o, never()).onError(any(Throwable.class));
verify(o).onCompleted();
}
@Test
public void takeAll() {
@SuppressWarnings("unchecked")
Observer<Object> o = mock(Observer.class);
Observable.just(1, 2).takeUntil(UtilityFunctions.alwaysFalse()).subscribe(o);
verify(o).onNext(1);
verify(o).onNext(2);
verify(o, never()).onError(any(Throwable.class));
verify(o).onCompleted();
}
@Test
public void takeFirst() {
@SuppressWarnings("unchecked")
Observer<Object> o = mock(Observer.class);
Observable.just(1, 2).takeUntil(UtilityFunctions.alwaysTrue()).subscribe(o);
verify(o).onNext(1);
verify(o, never()).onNext(2);
verify(o, never()).onError(any(Throwable.class));
verify(o).onCompleted();
}
@Test
public void takeSome() {
@SuppressWarnings("unchecked")
Observer<Object> o = mock(Observer.class);
Observable.just(1, 2, 3).takeUntil(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer t1) {
return t1 == 2;
}
}).subscribe(o);
verify(o).onNext(1);
verify(o).onNext(2);
verify(o, never()).onNext(3);
verify(o, never()).onError(any(Throwable.class));
verify(o).onCompleted();
}
@Test
public void functionThrows() {
@SuppressWarnings("unchecked")
Observer<Object> o = mock(Observer.class);
Observable.just(1, 2, 3).takeUntil(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer t1) {
throw new TestException("Forced failure");
}
}).subscribe(o);
verify(o).onNext(1);
verify(o, never()).onNext(2);
verify(o, never()).onNext(3);
verify(o).onError(any(TestException.class));
verify(o, never()).onCompleted();
}
@Test
public void sourceThrows() {
@SuppressWarnings("unchecked")
Observer<Object> o = mock(Observer.class);
Observable.just(1)
.concatWith(Observable.<Integer>error(new TestException()))
.concatWith(Observable.just(2))
.takeUntil(UtilityFunctions.alwaysFalse()).subscribe(o);
verify(o).onNext(1);
verify(o, never()).onNext(2);
verify(o).onError(any(TestException.class));
verify(o, never()).onCompleted();
}
@Test
public void backpressure() {
TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {
@Override
public void onStart() {
request(5);
}
};
Observable.range(1, 1000).takeUntil(UtilityFunctions.alwaysFalse()).subscribe(ts);
ts.assertNoErrors();
ts.assertReceivedOnNext(Arrays.asList(1, 2, 3, 4, 5));
Assert.assertEquals(0, ts.getOnCompletedEvents().size());
}
@Test
public void testErrorIncludesLastValueAsCause() {
TestSubscriber<String> ts = new TestSubscriber<String>();
final TestException e = new TestException("Forced failure");
Observable.just("abc").takeUntil(new Func1<String, Boolean>() {
@Override
public Boolean call(String t) {
throw e;
}
}).subscribe(ts);
ts.assertTerminalEvent();
ts.assertNotCompleted();
assertEquals(1, (int) ts.getOnErrorEvents().size());
assertTrue(ts.getOnErrorEvents().get(0).getCause().getMessage().contains("abc"));
}
}