Repository navigation
Expand file tree
/
Copy pathOperatorIgnoreElementsTest.java
More file actions
146 lines (126 loc) · 4.51 KB
/
Copy pathOperatorIgnoreElementsTest.java
File metadata and controls
146 lines (126 loc) · 4.51 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
/**
* Copyright 2016 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 java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.observers.TestSubscriber;
public class OperatorIgnoreElementsTest {
@Test
public void testWithEmpty() {
assertTrue(Observable.empty().ignoreElements().isEmpty().toBlocking().single());
}
@Test
public void testWithNonEmpty() {
assertTrue(Observable.just(1, 2, 3).ignoreElements().isEmpty().toBlocking().single());
}
@Test
public void testUpstreamIsProcessedButIgnored() {
final int num = 10;
final AtomicInteger upstreamCount = new AtomicInteger();
int count = Observable.range(1, num)
.doOnNext(new Action1<Integer>() {
@Override
public void call(Integer t) {
upstreamCount.incrementAndGet();
}
})
.ignoreElements()
.count().toBlocking().single();
assertEquals(num, upstreamCount.get());
assertEquals(0, count);
}
@Test
public void testCompletedOk() {
TestSubscriber<Object> ts = new TestSubscriber<Object>();
Observable.range(1, 10).ignoreElements().subscribe(ts);
ts.assertNoErrors();
ts.assertReceivedOnNext(Arrays.asList());
ts.assertTerminalEvent();
ts.assertUnsubscribed();
}
@Test
public void testErrorReceived() {
TestSubscriber<Object> ts = new TestSubscriber<Object>();
RuntimeException ex = new RuntimeException("boo");
Observable.error(ex).ignoreElements().subscribe(ts);
ts.assertReceivedOnNext(Arrays.asList());
ts.assertTerminalEvent();
ts.assertUnsubscribed();
assertEquals(1, ts.getOnErrorEvents().size());
assertEquals("boo", ts.getOnErrorEvents().get(0).getMessage());
}
@Test
public void testUnsubscribesFromUpstream() {
final AtomicBoolean unsub = new AtomicBoolean();
Observable.range(1, 10).doOnUnsubscribe(new Action0() {
@Override
public void call() {
unsub.set(true);
}})
.subscribe();
assertTrue(unsub.get());
}
@Test(timeout = 10000)
public void testDoesNotHangAndProcessesAllUsingBackpressure() {
final AtomicInteger upstreamCount = new AtomicInteger();
final AtomicInteger count = new AtomicInteger(0);
int num = 10;
Observable.range(1, num)
//
.doOnNext(new Action1<Integer>() {
@Override
public void call(Integer t) {
upstreamCount.incrementAndGet();
}
})
//
.ignoreElements()
//
.doOnNext(new Action1<Integer>() {
@Override
public void call(Integer t) {
upstreamCount.incrementAndGet();
}
})
//
.subscribe(new Subscriber<Integer>() {
@Override
public void onStart() {
request(1);
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Integer t) {
count.incrementAndGet();
}
});
assertEquals(num, upstreamCount.get());
assertEquals(0, count.get());
}
}