Skip to content

Commit f800dc8

Browse files
committed
Fixed defer and create mix-up
1 parent 0194a94 commit f800dc8

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

tests/java/itrx/chapter2/creating/ObservableFactoriesExample.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void exampleError() {
6060
// Error: java.lang.Exception: Oops
6161
}
6262

63-
public void exampleDefer() throws InterruptedException {
63+
public void exampleShouldDefer() throws InterruptedException {
6464
Observable<Long> now = Observable.just(System.currentTimeMillis());
6565

6666
now.subscribe(System.out::println);
@@ -71,7 +71,7 @@ public void exampleDefer() throws InterruptedException {
7171
// 1431443908375
7272
}
7373

74-
public void exampleCreate() throws InterruptedException {
74+
public void exampleDefer() throws InterruptedException {
7575
Observable<Long> now = Observable.defer(() ->
7676
Observable.just(System.currentTimeMillis()));
7777

@@ -83,6 +83,22 @@ public void exampleCreate() throws InterruptedException {
8383
// 1431444108858
8484
}
8585

86+
public void exampleCreate() {
87+
Observable<String> values = Observable.create(o -> {
88+
o.onNext("Hello");
89+
o.onCompleted();
90+
});
91+
92+
values.subscribe(
93+
v -> System.out.println("Received: " + v),
94+
e -> System.out.println("Error: " + e),
95+
() -> System.out.println("Completed")
96+
);
97+
98+
// Received: Hello
99+
// Completed
100+
}
101+
86102

87103
//
88104
// Tests
@@ -137,6 +153,22 @@ public void testError() {
137153
assertEquals(tester.getOnCompletedEvents().size(), 0);
138154
}
139155

156+
@Test
157+
public void testShouldDefer() throws InterruptedException {
158+
TestScheduler scheduler = Schedulers.test();
159+
TestSubscriber<Long> tester1 = new TestSubscriber<>();
160+
TestSubscriber<Long> tester2 = new TestSubscriber<>();
161+
162+
Observable<Long> now = Observable.just(scheduler.now());
163+
164+
now.subscribe(tester1);
165+
scheduler.advanceTimeBy(1000, TimeUnit.MILLISECONDS);
166+
now.subscribe(tester2);
167+
168+
assertEquals(tester1.getOnNextEvents().get(0),
169+
tester2.getOnNextEvents().get(0));
170+
}
171+
140172
@Test
141173
public void testDefer() {
142174
TestScheduler scheduler = Schedulers.test();

0 commit comments

Comments
 (0)