Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/BlockingObservableMostRecentTest.java

raw
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.rxjava3.internal.operators.observable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.*;
22
23 import io.reactivex.rxjava3.core.Observable;
24 import io.reactivex.rxjava3.core.RxJavaTest;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.schedulers.TestScheduler;
27 import io.reactivex.rxjava3.subjects.*;
28
29 public class BlockingObservableMostRecentTest extends RxJavaTest {
30
31 static <T> Iterable<T> mostRecent(Observable<T> source, T initialValue) {
32 return source.blockingMostRecent(initialValue);
33 }
34
35 @Test
36 public void mostRecent() {
37 Subject<String> s = PublishSubject.create();
38
39 Iterator<String> it = mostRecent(s, "default").iterator();
40
41 assertTrue(it.hasNext());
42 assertEquals("default", it.next());
43 assertEquals("default", it.next());
44
45 s.onNext("one");
46 assertTrue(it.hasNext());
47 assertEquals("one", it.next());
48 assertEquals("one", it.next());
49
50 s.onNext("two");
51 assertTrue(it.hasNext());
52 assertEquals("two", it.next());
53 assertEquals("two", it.next());
54
55 s.onComplete();
56 assertFalse(it.hasNext());
57
58 }
59
60 @Test(expected = TestException.class)
61 public void mostRecentWithException() {
62 Subject<String> s = PublishSubject.create();
63
64 Iterator<String> it = mostRecent(s, "default").iterator();
65
66 assertTrue(it.hasNext());
67 assertEquals("default", it.next());
68 assertEquals("default", it.next());
69
70 s.onError(new TestException());
71 assertTrue(it.hasNext());
72
73 it.next();
74 }
75
76 @Test
77 public void singleSourceManyIterators() {
78 TestScheduler scheduler = new TestScheduler();
79 Observable<Long> source = Observable.interval(1, TimeUnit.SECONDS, scheduler).take(10);
80
81 Iterable<Long> iter = source.blockingMostRecent(-1L);
82
83 for (int j = 0; j < 3; j++) {
84 Iterator<Long> it = iter.iterator();
85
86 Assert.assertEquals(Long.valueOf(-1), it.next());
87
88 for (int i = 0; i < 9; i++) {
89 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
90
91 Assert.assertTrue(it.hasNext());
92 Assert.assertEquals(Long.valueOf(i), it.next());
93 }
94 scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
95
96 Assert.assertFalse(it.hasNext());
97 }
98
99 }
100
101 @Test
102 public void empty() {
103 Iterator<Integer> it = Observable.<Integer>empty()
104 .blockingMostRecent(1)
105 .iterator();
106
107 try {
108 it.next();
109 fail("Should have thrown");
110 } catch (NoSuchElementException ex) {
111 // expected
112 }
113
114 try {
115 it.remove();
116 fail("Should have thrown");
117 } catch (UnsupportedOperationException ex) {
118 // expected
119 }
120 }
121 }