Skip to content

Commit 500909d

Browse files
Combine RefCountTest and RefCountTests
1 parent abc8bec commit 500909d

File tree

2 files changed

+94
-103
lines changed

2 files changed

+94
-103
lines changed

rxjava-core/src/test/java/rx/RefCountTest.java

Lines changed: 0 additions & 103 deletions
This file was deleted.

rxjava-core/src/test/java/rx/RefCountTests.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import org.junit.Before;
44
import org.junit.Test;
55
import org.mockito.MockitoAnnotations;
6+
7+
import rx.concurrency.TestScheduler;
68
import rx.subscriptions.Subscriptions;
79
import rx.util.functions.Action0;
10+
import rx.util.functions.Action1;
811

12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.concurrent.TimeUnit;
915
import java.util.concurrent.atomic.AtomicInteger;
1016

1117
import static org.junit.Assert.assertEquals;
@@ -45,4 +51,92 @@ public void call() {
4551
second.unsubscribe();
4652
assertEquals(1, unsubscriptionCount.get());
4753
}
54+
55+
@Test
56+
public void testRefCount() {
57+
TestScheduler s = new TestScheduler();
58+
Observable<Long> interval = Observable.interval(100, TimeUnit.MILLISECONDS, s).publish().refCount();
59+
60+
// subscribe list1
61+
final List<Long> list1 = new ArrayList<Long>();
62+
Subscription s1 = interval.subscribe(new Action1<Long>() {
63+
64+
@Override
65+
public void call(Long t1) {
66+
list1.add(t1);
67+
}
68+
69+
});
70+
s.advanceTimeBy(200, TimeUnit.MILLISECONDS);
71+
72+
assertEquals(2, list1.size());
73+
assertEquals(0L, list1.get(0).longValue());
74+
assertEquals(1L, list1.get(1).longValue());
75+
76+
// subscribe list2
77+
final List<Long> list2 = new ArrayList<Long>();
78+
Subscription s2 = interval.subscribe(new Action1<Long>() {
79+
80+
@Override
81+
public void call(Long t1) {
82+
list2.add(t1);
83+
}
84+
85+
});
86+
s.advanceTimeBy(300, TimeUnit.MILLISECONDS);
87+
88+
// list 1 should have 5 items
89+
assertEquals(5, list1.size());
90+
assertEquals(2L, list1.get(2).longValue());
91+
assertEquals(3L, list1.get(3).longValue());
92+
assertEquals(4L, list1.get(4).longValue());
93+
94+
// list 2 should only have 3 items
95+
assertEquals(3, list2.size());
96+
assertEquals(2L, list2.get(0).longValue());
97+
assertEquals(3L, list2.get(1).longValue());
98+
assertEquals(4L, list2.get(2).longValue());
99+
100+
// unsubscribe list1
101+
s1.unsubscribe();
102+
103+
// advance further
104+
s.advanceTimeBy(300, TimeUnit.MILLISECONDS);
105+
106+
// list 1 should still have 5 items
107+
assertEquals(5, list1.size());
108+
109+
// list 2 should have 6 items
110+
assertEquals(6, list2.size());
111+
assertEquals(5L, list2.get(3).longValue());
112+
assertEquals(6L, list2.get(4).longValue());
113+
assertEquals(7L, list2.get(5).longValue());
114+
115+
// unsubscribe list2
116+
s2.unsubscribe();
117+
118+
// advance further
119+
s.advanceTimeBy(1000, TimeUnit.MILLISECONDS);
120+
121+
// the following is not working as it seems the PublishSubject does not allow re-subscribing. TODO fix that in subsequent pull request
122+
123+
124+
// // subscribing a new one should start over because the source should have been unsubscribed
125+
// // subscribe list1
126+
// final List<Long> list3 = new ArrayList<Long>();
127+
// Subscription s3 = interval.subscribe(new Action1<Long>() {
128+
//
129+
// @Override
130+
// public void call(Long t1) {
131+
// list3.add(t1);
132+
// }
133+
//
134+
// });
135+
// s.advanceTimeBy(200, TimeUnit.MILLISECONDS);
136+
//
137+
// assertEquals(2, list3.size());
138+
// assertEquals(0L, list3.get(0).longValue());
139+
// assertEquals(1L, list3.get(1).longValue());
140+
141+
}
48142
}

0 commit comments

Comments
 (0)