Skip to content

Commit 2fb9504

Browse files
authored
2.x: Expand and fix Completable.delaySubscription tests (#6252)
1 parent f78bd95 commit 2fb9504

File tree

2 files changed

+169
-65
lines changed

2 files changed

+169
-65
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
* Copyright (c) 2016-present, RxJava Contributors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5+
* compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is
10+
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11+
* the License for the specific language governing permissions and limitations under the License.
12+
*/
13+
14+
package io.reactivex.internal.operators.completable;
15+
16+
import static org.junit.Assert.*;
17+
18+
import java.util.concurrent.TimeUnit;
19+
import java.util.concurrent.atomic.AtomicInteger;
20+
21+
import org.junit.Test;
22+
23+
import io.reactivex.Completable;
24+
import io.reactivex.exceptions.TestException;
25+
import io.reactivex.functions.Action;
26+
import io.reactivex.observers.TestObserver;
27+
import io.reactivex.schedulers.TestScheduler;
28+
import io.reactivex.subjects.CompletableSubject;
29+
30+
public class CompletableDelaySubscriptionTest {
31+
32+
@Test
33+
public void normal() {
34+
final AtomicInteger counter = new AtomicInteger();
35+
36+
Completable.fromAction(new Action() {
37+
@Override
38+
public void run() throws Exception {
39+
counter.incrementAndGet();
40+
}
41+
})
42+
.delaySubscription(100, TimeUnit.MILLISECONDS)
43+
.test()
44+
.awaitDone(5, TimeUnit.SECONDS)
45+
.assertResult();
46+
47+
assertEquals(1, counter.get());
48+
}
49+
50+
@Test
51+
public void error() {
52+
final AtomicInteger counter = new AtomicInteger();
53+
54+
Completable.fromAction(new Action() {
55+
@Override
56+
public void run() throws Exception {
57+
counter.incrementAndGet();
58+
59+
throw new TestException();
60+
}
61+
})
62+
.delaySubscription(100, TimeUnit.MILLISECONDS)
63+
.test()
64+
.awaitDone(5, TimeUnit.SECONDS)
65+
.assertFailure(TestException.class);
66+
67+
assertEquals(1, counter.get());
68+
}
69+
70+
@Test
71+
public void disposeBeforeTime() {
72+
TestScheduler scheduler = new TestScheduler();
73+
74+
final AtomicInteger counter = new AtomicInteger();
75+
76+
Completable result = Completable.fromAction(new Action() {
77+
@Override
78+
public void run() throws Exception {
79+
counter.incrementAndGet();
80+
}
81+
})
82+
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler);
83+
TestObserver<Void> to = result.test();
84+
85+
to.assertEmpty();
86+
87+
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS);
88+
89+
to.dispose();
90+
91+
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
92+
93+
to.assertEmpty();
94+
95+
assertEquals(0, counter.get());
96+
}
97+
98+
@Test
99+
public void timestep() {
100+
TestScheduler scheduler = new TestScheduler();
101+
final AtomicInteger counter = new AtomicInteger();
102+
103+
Completable result = Completable.fromAction(new Action() {
104+
@Override
105+
public void run() throws Exception {
106+
counter.incrementAndGet();
107+
}
108+
})
109+
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler);
110+
111+
TestObserver<Void> to = result.test();
112+
113+
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS);
114+
to.assertEmpty();
115+
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
116+
to.assertResult();
117+
118+
assertEquals(1, counter.get());
119+
}
120+
121+
@Test
122+
public void timestepError() {
123+
TestScheduler scheduler = new TestScheduler();
124+
final AtomicInteger counter = new AtomicInteger();
125+
126+
Completable result = Completable.fromAction(new Action() {
127+
@Override
128+
public void run() throws Exception {
129+
counter.incrementAndGet();
130+
131+
throw new TestException();
132+
}
133+
})
134+
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler);
135+
136+
TestObserver<Void> to = result.test();
137+
138+
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS);
139+
140+
to.assertEmpty();
141+
142+
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
143+
144+
to.assertFailure(TestException.class);
145+
146+
assertEquals(1, counter.get());
147+
}
148+
149+
@Test
150+
public void disposeMain() {
151+
CompletableSubject cs = CompletableSubject.create();
152+
153+
TestScheduler scheduler = new TestScheduler();
154+
155+
TestObserver<Void> to = cs
156+
.delaySubscription(1, TimeUnit.SECONDS, scheduler)
157+
.test();
158+
159+
assertFalse(cs.hasObservers());
160+
161+
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
162+
163+
assertTrue(cs.hasObservers());
164+
165+
to.dispose();
166+
167+
assertFalse(cs.hasObservers());
168+
}
169+
}

src/test/java/io/reactivex/internal/operators/completable/CompletableDelayTest.java

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -123,69 +123,4 @@ public void errorDelayed() {
123123

124124
to.assertFailure(TestException.class);
125125
}
126-
127-
@Test
128-
public void errorDelayedSubscription() {
129-
TestScheduler scheduler = new TestScheduler();
130-
131-
TestObserver<Void> to = Completable.error(new TestException())
132-
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler)
133-
.test();
134-
135-
to.assertEmpty();
136-
137-
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS);
138-
139-
to.assertEmpty();
140-
141-
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
142-
143-
to.assertFailure(TestException.class);
144-
}
145-
146-
@Test
147-
public void errorDelayedSubscriptionDisposeBeforeTime() {
148-
TestScheduler scheduler = new TestScheduler();
149-
150-
Completable result = Completable.complete()
151-
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler);
152-
TestObserver<Void> to = result.test();
153-
154-
to.assertEmpty();
155-
156-
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS);
157-
to.dispose();
158-
159-
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
160-
161-
to.assertEmpty();
162-
}
163-
164-
@Test
165-
public void testDelaySubscriptionDisposeBeforeTime() {
166-
TestScheduler scheduler = new TestScheduler();
167-
168-
Completable result = Completable.complete()
169-
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler);
170-
TestObserver<Void> to = result.test();
171-
172-
to.assertEmpty();
173-
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS);
174-
to.dispose();
175-
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
176-
to.assertEmpty();
177-
}
178-
179-
@Test
180-
public void testDelaySubscription() {
181-
TestScheduler scheduler = new TestScheduler();
182-
Completable result = Completable.complete()
183-
.delaySubscription(100, TimeUnit.MILLISECONDS, scheduler);
184-
TestObserver<Void> to = result.test();
185-
186-
scheduler.advanceTimeBy(90, TimeUnit.MILLISECONDS);
187-
to.assertEmpty();
188-
scheduler.advanceTimeBy(15, TimeUnit.MILLISECONDS);
189-
to.assertResult();
190-
}
191126
}

0 commit comments

Comments
 (0)