|
15 | 15 | */
|
16 | 16 | package rx.subscriptions;
|
17 | 17 |
|
18 |
| -import static org.mockito.Mockito.*; |
| 18 | +import static org.junit.Assert.fail; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.CountDownLatch; |
19 | 25 |
|
20 | 26 | import org.junit.Before;
|
21 | 27 | import org.junit.Test;
|
|
24 | 30 | import rx.Subscription;
|
25 | 31 |
|
26 | 32 | public class SerialSubscriptionTests {
|
27 |
| - private SerialSubscription serialSubscription; |
28 |
| - |
29 |
| - @Before |
30 |
| - public void setUp() { |
31 |
| - MockitoAnnotations.initMocks(this); |
32 |
| - |
33 |
| - serialSubscription = new SerialSubscription(); |
34 |
| - } |
35 |
| - |
36 |
| - @Test |
37 |
| - public void unsubscribingWithoutUnderlyingDoesNothing() { |
38 |
| - serialSubscription.unsubscribe(); |
39 |
| - } |
40 |
| - |
41 |
| - @Test |
42 |
| - public void unsubscribingWithSingleUnderlyingUnsubscribes() { |
43 |
| - Subscription underlying = mock(Subscription.class); |
44 |
| - serialSubscription.setSubscription(underlying); |
45 |
| - underlying.unsubscribe(); |
46 |
| - verify(underlying).unsubscribe(); |
47 |
| - } |
48 |
| - |
49 |
| - @Test |
50 |
| - public void replacingFirstUnderlyingCausesUnsubscription() { |
51 |
| - Subscription first = mock(Subscription.class); |
52 |
| - serialSubscription.setSubscription(first); |
53 |
| - Subscription second = mock(Subscription.class); |
54 |
| - serialSubscription.setSubscription(second); |
55 |
| - verify(first).unsubscribe(); |
56 |
| - } |
57 |
| - |
58 |
| - @Test |
59 |
| - public void whenUnsubscribingSecondUnderlyingUnsubscribed() { |
60 |
| - Subscription first = mock(Subscription.class); |
61 |
| - serialSubscription.setSubscription(first); |
62 |
| - Subscription second = mock(Subscription.class); |
63 |
| - serialSubscription.setSubscription(second); |
64 |
| - serialSubscription.unsubscribe(); |
65 |
| - verify(second).unsubscribe(); |
66 |
| - } |
67 |
| - |
68 |
| - @Test |
69 |
| - public void settingUnderlyingWhenUnsubscribedCausesImmediateUnsubscription() |
70 |
| - { |
71 |
| - serialSubscription.unsubscribe(); |
72 |
| - Subscription underlying = mock(Subscription.class); |
73 |
| - serialSubscription.setSubscription(underlying); |
74 |
| - verify(underlying).unsubscribe(); |
75 |
| - } |
| 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 | + } |
76 | 123 | }
|
0 commit comments