CompositeSubscription implements Subscription { public void clear() { State oldState; State newState; do { oldState = state.get(); if (oldState.isUnsubscribed) { return; } else { newState = oldState.clear(); } } while (!state.compareAndSet(oldState, newState)); unsubscribeFromAll(oldState.subscriptions); } CompositeSubscription();  CompositeSubscription(final Subscription... subscriptions); @Override boolean isUnsubscribed(); void add(final Subscription s); void remove(final Subscription s); void clear(); @Override void unsubscribe();  }
@Test public void testClear() { BooleanSubscription s1 = new BooleanSubscription(); BooleanSubscription s2 = new BooleanSubscription(); CompositeSubscription s = new CompositeSubscription(); s.add(s1); s.add(s2); assertFalse(s1.isUnsubscribed()); assertFalse(s2.isUnsubscribed()); s.clear(); assertTrue(s1.isUnsubscribed()); assertTrue(s1.isUnsubscribed()); assertFalse(s.isUnsubscribed()); BooleanSubscription s3 = new BooleanSubscription(); s.add(s3); s.unsubscribe(); assertTrue(s3.isUnsubscribed()); assertTrue(s.isUnsubscribed()); }