forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefCountTests.java
More file actions
82 lines (74 loc) · 3.53 KB
/
RefCountTests.java
File metadata and controls
82 lines (74 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package rx;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import rx.observables.ConnectableObservable;
import rx.subscriptions.Subscriptions;
import static org.mockito.Mockito.*;
public class RefCountTests {
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void subscriptionToUnderlyingOnFirstSubscription() {
@SuppressWarnings("unchecked")
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class);
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable);
@SuppressWarnings("unchecked")
Observer<Integer> observer = mock(Observer.class);
when(connectable.subscribe(any(Observer.class))).thenReturn(Subscriptions.empty());
when(connectable.connect()).thenReturn(Subscriptions.empty());
refCounted.subscribe(observer);
verify(connectable, times(1)).subscribe(any(Observer.class));
verify(connectable, times(1)).connect();
}
@Test
public void noSubscriptionToUnderlyingOnSecondSubscription() {
@SuppressWarnings("unchecked")
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class);
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable);
@SuppressWarnings("unchecked")
Observer<Integer> observer = mock(Observer.class);
when(connectable.subscribe(any(Observer.class))).thenReturn(Subscriptions.empty());
when(connectable.connect()).thenReturn(Subscriptions.empty());
refCounted.subscribe(observer);
refCounted.subscribe(observer);
verify(connectable, times(2)).subscribe(any(Observer.class));
verify(connectable, times(1)).connect();
}
@Test
public void unsubscriptionFromUnderlyingOnLastUnsubscription() {
@SuppressWarnings("unchecked")
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class);
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable);
@SuppressWarnings("unchecked")
Observer<Integer> observer = mock(Observer.class);
Subscription underlying = mock(Subscription.class);
when(connectable.subscribe(any(Observer.class))).thenReturn(underlying);
Subscription connection = mock(Subscription.class);
when(connectable.connect()).thenReturn(connection);
Subscription first = refCounted.subscribe(observer);
first.unsubscribe();
verify(underlying, times(1)).unsubscribe();
verify(connection, times(1)).unsubscribe();
}
@Test
public void noUnsubscriptionFromUnderlyingOnFirstUnsubscription() {
@SuppressWarnings("unchecked")
ConnectableObservable<Integer> connectable = mock(ConnectableObservable.class);
Observable<Integer> refCounted = ConnectableObservable.refCount(connectable);
@SuppressWarnings("unchecked")
Observer<Integer> observer = mock(Observer.class);
Subscription underlying = mock(Subscription.class);
when(connectable.subscribe(any(Observer.class))).thenReturn(underlying);
Subscription connection = mock(Subscription.class);
when(connectable.connect()).thenReturn(connection);
Subscription first = refCounted.subscribe(observer);
Subscription second = refCounted.subscribe(observer);
first.unsubscribe();
second.unsubscribe();
verify(underlying, times(2)).unsubscribe();
verify(connection, times(1)).unsubscribe();
}
}