Skip to content

Commit 39b9787

Browse files
committed
JUnit setting subscription concurrently
1 parent ec9bd2c commit 39b9787

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

rxjava-core/src/test/java/rx/subscriptions/SerialSubscriptionTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,48 @@ public void run() {
163163
t.join();
164164
}
165165
}
166+
167+
@Test
168+
public void concurrentSetSubscriptionShouldNotInterleave()
169+
throws InterruptedException {
170+
final int count = 10;
171+
final List<Subscription> subscriptions = new ArrayList<Subscription>();
172+
173+
final CountDownLatch start = new CountDownLatch(1);
174+
final CountDownLatch end = new CountDownLatch(count);
175+
176+
final List<Thread> threads = new ArrayList<Thread>();
177+
for (int i = 0 ; i < count ; i++) {
178+
final Subscription subscription = mock(Subscription.class);
179+
subscriptions.add(subscription);
180+
181+
final Thread t = new Thread() {
182+
@Override
183+
public void run() {
184+
try {
185+
start.await();
186+
serialSubscription.setSubscription(subscription);
187+
} catch (InterruptedException e) {
188+
fail(e.getMessage());
189+
} finally {
190+
end.countDown();
191+
}
192+
}
193+
};
194+
t.start();
195+
threads.add(t);
196+
}
197+
198+
start.countDown();
199+
end.await();
200+
serialSubscription.unsubscribe();
201+
202+
for(final Subscription subscription : subscriptions) {
203+
verify(subscription).unsubscribe();
204+
}
205+
206+
for (final Thread t : threads) {
207+
t.join();
208+
}
209+
}
166210
}

0 commit comments

Comments
 (0)