|
30 | 30 | import rx.Subscription;
|
31 | 31 |
|
32 | 32 | public class SerialSubscriptionTests {
|
33 |
| - private SerialSubscription serialSubscription; |
34 |
| - |
35 |
| - @Before |
36 |
| - public void setUp() { |
37 |
| - MockitoAnnotations.initMocks(this); |
38 |
| - |
39 |
| - serialSubscription = new SerialSubscription(); |
40 |
| - } |
41 |
| - |
42 |
| - @Test |
43 |
| - public void unsubscribingWithoutUnderlyingDoesNothing() { |
44 |
| - serialSubscription.unsubscribe(); |
45 |
| - } |
46 |
| - |
47 |
| - @Test |
48 |
| - public void unsubscribingWithSingleUnderlyingUnsubscribes() { |
49 |
| - Subscription underlying = mock(Subscription.class); |
50 |
| - serialSubscription.setSubscription(underlying); |
51 |
| - underlying.unsubscribe(); |
52 |
| - verify(underlying).unsubscribe(); |
53 |
| - } |
54 |
| - |
55 |
| - @Test |
56 |
| - public void replacingFirstUnderlyingCausesUnsubscription() { |
57 |
| - Subscription first = mock(Subscription.class); |
58 |
| - serialSubscription.setSubscription(first); |
59 |
| - Subscription second = mock(Subscription.class); |
60 |
| - serialSubscription.setSubscription(second); |
61 |
| - verify(first).unsubscribe(); |
62 |
| - } |
63 |
| - |
64 |
| - @Test |
65 |
| - public void whenUnsubscribingSecondUnderlyingUnsubscribed() { |
66 |
| - Subscription first = mock(Subscription.class); |
67 |
| - serialSubscription.setSubscription(first); |
68 |
| - Subscription second = mock(Subscription.class); |
69 |
| - serialSubscription.setSubscription(second); |
70 |
| - serialSubscription.unsubscribe(); |
71 |
| - verify(second).unsubscribe(); |
72 |
| - } |
73 |
| - |
74 |
| - @Test |
75 |
| - public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscription() { |
76 |
| - serialSubscription.unsubscribe(); |
77 |
| - Subscription underlying = mock(Subscription.class); |
78 |
| - serialSubscription.setSubscription(underlying); |
79 |
| - verify(underlying).unsubscribe(); |
80 |
| - } |
81 |
| - |
82 |
| - @Test(timeout = 1000) |
83 |
| - public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscriptionConcurrently() |
84 |
| - throws InterruptedException { |
85 |
| - final Subscription firstSet = mock(Subscription.class); |
86 |
| - serialSubscription.setSubscription(firstSet); |
87 |
| - |
88 |
| - final CountDownLatch start = new CountDownLatch(1); |
89 |
| - |
90 |
| - final int count = 10; |
91 |
| - final CountDownLatch end = new CountDownLatch(count); |
92 |
| - |
93 |
| - final List<Thread> threads = new ArrayList<Thread>(); |
94 |
| - for (int i = 0 ; i < count ; i++) { |
95 |
| - final Thread t = new Thread() { |
96 |
| - @Override |
97 |
| - public void run() { |
98 |
| - try { |
99 |
| - start.await(); |
100 |
| - serialSubscription.unsubscribe(); |
101 |
| - } catch (InterruptedException e) { |
102 |
| - fail(e.getMessage()); |
103 |
| - } finally { |
104 |
| - end.countDown(); |
105 |
| - } |
106 |
| - } |
107 |
| - }; |
108 |
| - t.start(); |
109 |
| - threads.add(t); |
110 |
| - } |
111 |
| - |
112 |
| - final Subscription underlying = mock(Subscription.class); |
113 |
| - start.countDown(); |
114 |
| - serialSubscription.setSubscription(underlying); |
115 |
| - end.await(); |
116 |
| - verify(firstSet).unsubscribe(); |
117 |
| - verify(underlying).unsubscribe(); |
118 |
| - |
119 |
| - for (final Thread t : threads) { |
120 |
| - t.interrupt(); |
121 |
| - } |
122 |
| - } |
| 33 | + private SerialSubscription serialSubscription; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setUp() { |
| 37 | + MockitoAnnotations.initMocks(this); |
| 38 | + |
| 39 | + serialSubscription = new SerialSubscription(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void unsubscribingWithoutUnderlyingDoesNothing() { |
| 44 | + serialSubscription.unsubscribe(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void unsubscribingWithSingleUnderlyingUnsubscribes() { |
| 49 | + Subscription underlying = mock(Subscription.class); |
| 50 | + serialSubscription.setSubscription(underlying); |
| 51 | + underlying.unsubscribe(); |
| 52 | + verify(underlying).unsubscribe(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void replacingFirstUnderlyingCausesUnsubscription() { |
| 57 | + Subscription first = mock(Subscription.class); |
| 58 | + serialSubscription.setSubscription(first); |
| 59 | + Subscription second = mock(Subscription.class); |
| 60 | + serialSubscription.setSubscription(second); |
| 61 | + verify(first).unsubscribe(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void whenUnsubscribingSecondUnderlyingUnsubscribed() { |
| 66 | + Subscription first = mock(Subscription.class); |
| 67 | + serialSubscription.setSubscription(first); |
| 68 | + Subscription second = mock(Subscription.class); |
| 69 | + serialSubscription.setSubscription(second); |
| 70 | + serialSubscription.unsubscribe(); |
| 71 | + verify(second).unsubscribe(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscription() { |
| 76 | + serialSubscription.unsubscribe(); |
| 77 | + Subscription underlying = mock(Subscription.class); |
| 78 | + serialSubscription.setSubscription(underlying); |
| 79 | + verify(underlying).unsubscribe(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test(timeout = 1000) |
| 83 | + public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscriptionConcurrently() |
| 84 | + throws InterruptedException { |
| 85 | + final Subscription firstSet = mock(Subscription.class); |
| 86 | + serialSubscription.setSubscription(firstSet); |
| 87 | + |
| 88 | + final CountDownLatch start = new CountDownLatch(1); |
| 89 | + |
| 90 | + final int count = 10; |
| 91 | + final CountDownLatch end = new CountDownLatch(count); |
| 92 | + |
| 93 | + final List<Thread> threads = new ArrayList<Thread>(); |
| 94 | + for (int i = 0 ; i < count ; i++) { |
| 95 | + final Thread t = new Thread() { |
| 96 | + @Override |
| 97 | + public void run() { |
| 98 | + try { |
| 99 | + start.await(); |
| 100 | + serialSubscription.unsubscribe(); |
| 101 | + } catch (InterruptedException e) { |
| 102 | + fail(e.getMessage()); |
| 103 | + } finally { |
| 104 | + end.countDown(); |
| 105 | + } |
| 106 | + } |
| 107 | + }; |
| 108 | + t.start(); |
| 109 | + threads.add(t); |
| 110 | + } |
| 111 | + |
| 112 | + final Subscription underlying = mock(Subscription.class); |
| 113 | + start.countDown(); |
| 114 | + serialSubscription.setSubscription(underlying); |
| 115 | + end.await(); |
| 116 | + verify(firstSet).unsubscribe(); |
| 117 | + verify(underlying).unsubscribe(); |
| 118 | + |
| 119 | + for (final Thread t : threads) { |
| 120 | + t.interrupt(); |
| 121 | + } |
| 122 | + } |
123 | 123 | }
|
0 commit comments