Skip to content

Commit 08a70ff

Browse files
Unit Test while testing refCount submission
1 parent 7c61f9b commit 08a70ff

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package rx;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.concurrent.TimeUnit;
8+
9+
import org.junit.Test;
10+
11+
import rx.concurrency.TestScheduler;
12+
import rx.util.functions.Action1;
13+
14+
public class RefCountTest {
15+
16+
@Test
17+
public void testRefCount() {
18+
TestScheduler s = new TestScheduler();
19+
Observable<Long> interval = Observable.interval(100, TimeUnit.MILLISECONDS, s).publish().refCount();
20+
21+
// subscribe list1
22+
final List<Long> list1 = new ArrayList<Long>();
23+
Subscription s1 = interval.subscribe(new Action1<Long>() {
24+
25+
@Override
26+
public void call(Long t1) {
27+
list1.add(t1);
28+
}
29+
30+
});
31+
s.advanceTimeBy(200, TimeUnit.MILLISECONDS);
32+
33+
assertEquals(2, list1.size());
34+
assertEquals(0L, list1.get(0).longValue());
35+
assertEquals(1L, list1.get(1).longValue());
36+
37+
// subscribe list2
38+
final List<Long> list2 = new ArrayList<Long>();
39+
Subscription s2 = interval.subscribe(new Action1<Long>() {
40+
41+
@Override
42+
public void call(Long t1) {
43+
list2.add(t1);
44+
}
45+
46+
});
47+
s.advanceTimeBy(300, TimeUnit.MILLISECONDS);
48+
49+
// list 1 should have 5 items
50+
assertEquals(5, list1.size());
51+
assertEquals(2L, list1.get(2).longValue());
52+
assertEquals(3L, list1.get(3).longValue());
53+
assertEquals(4L, list1.get(4).longValue());
54+
55+
// list 2 should only have 3 items
56+
assertEquals(3, list2.size());
57+
assertEquals(2L, list2.get(0).longValue());
58+
assertEquals(3L, list2.get(1).longValue());
59+
assertEquals(4L, list2.get(2).longValue());
60+
61+
// unsubscribe list1
62+
s1.unsubscribe();
63+
64+
// advance further
65+
s.advanceTimeBy(300, TimeUnit.MILLISECONDS);
66+
67+
// list 1 should still have 5 items
68+
assertEquals(5, list1.size());
69+
70+
// list 2 should have 6 items
71+
assertEquals(6, list2.size());
72+
assertEquals(5L, list2.get(3).longValue());
73+
assertEquals(6L, list2.get(4).longValue());
74+
assertEquals(7L, list2.get(5).longValue());
75+
76+
// unsubscribe list2
77+
s2.unsubscribe();
78+
79+
// advance further
80+
s.advanceTimeBy(1000, TimeUnit.MILLISECONDS);
81+
82+
// the following is not working as it seems the PublishSubject does not allow re-subscribing. TODO fix that in subsequent pull request
83+
84+
85+
// // subscribing a new one should start over because the source should have been unsubscribed
86+
// // subscribe list1
87+
// final List<Long> list3 = new ArrayList<Long>();
88+
// Subscription s3 = interval.subscribe(new Action1<Long>() {
89+
//
90+
// @Override
91+
// public void call(Long t1) {
92+
// list3.add(t1);
93+
// }
94+
//
95+
// });
96+
// s.advanceTimeBy(200, TimeUnit.MILLISECONDS);
97+
//
98+
// assertEquals(2, list3.size());
99+
// assertEquals(0L, list3.get(0).longValue());
100+
// assertEquals(1L, list3.get(1).longValue());
101+
102+
}
103+
}

0 commit comments

Comments
 (0)