### Question:
Subscriptions { public static void cancel(AtomicReference<Subscription> reference) { Subscription actual = reference.getAndSet(CANCELLED); if (actual != null && actual != CANCELLED) { actual.cancel(); } } private  Subscriptions(); static IllegalArgumentException getInvalidRequestException(); static Subscription empty(); static void complete(Subscriber<?> subscriber); static void fail(Subscriber<?> subscriber, Throwable failure); static void fail(Subscriber<?> subscriber, Throwable failure, Publisher<?> upstream); static long add(long a, long b); static long add(AtomicLong requested, long requests); static long subtract(AtomicLong requested, long emitted); static int unboundedOrLimit(int prefetch); static long unboundedOrRequests(int concurrency); static boolean addFailure(AtomicReference<Throwable> failures, Throwable failure); static void cancel(AtomicReference<Subscription> reference); static Throwable markFailureAsTerminated(AtomicReference<Throwable> failures); static void terminateAndPropagate(AtomicReference<Throwable> failures, Subscriber<?> subscriber); static long multiply(long n, long times); static void requestIfNotNullOrAccumulate(AtomicReference<Subscription> field, AtomicLong requested, long requests); static boolean setIfEmptyAndRequest(AtomicReference<Subscription> container, AtomicLong requested,
            Subscription subscription); static boolean setIfEmpty(AtomicReference<Subscription> container, Subscription subscription); static Throwable terminate(AtomicReference<Throwable> failure); static long produced(AtomicLong requested, long amount); static long subOrZero(long a, long b); static SingleItemSubscription<T> single(Subscriber<T> downstream, T item); static long producedAndHandleAlreadyCancelled(AtomicLong requested, long n); static long addAndHandledAlreadyCancelled(AtomicLong requested, long n); static final Throwable TERMINATED; static final EmptySubscription CANCELLED; }### Answer:
@Test public void deferredSubscriptionEmptyCancellationTest() { Subscriptions.DeferredSubscription subscription = new Subscriptions.DeferredSubscription(); assertThat(subscription.isCancelled()).isFalse(); subscription.cancel(); assertThat(subscription.isCancelled()).isTrue(); }
@Test public void deferredSubscriptionSetAfterCancellation() { Subscriptions.DeferredSubscription subscription = new Subscriptions.DeferredSubscription(); subscription.request(20); subscription.cancel(); Subscription newSub = mock(Subscription.class); assertThat(subscription.set(newSub)).isFalse(); verify(newSub, never()).request(anyLong()); verify(newSub).cancel(); }
@Test public void deferredSubscriptionSetTwiceCancelTheSecondOne() { Subscriptions.DeferredSubscription subscription = new Subscriptions.DeferredSubscription(); Subscription s1 = mock(Subscription.class); assertThat(subscription.set(s1)).isTrue(); subscription.request(20); verify(s1).request(20); Subscription newSub = mock(Subscription.class); assertThat(subscription.set(newSub)).isFalse(); verify(newSub, never()).request(anyLong()); verify(newSub).cancel(); }
@Test public void testThatTheCancelledSubscriptionCanBeCancelled() { Subscriptions.CANCELLED.cancel(); Subscriptions.CANCELLED.cancel(); }
@Test public void testCancelledSubscriber() { Subscriptions.CancelledSubscriber<Integer> subscriber = new Subscriptions.CancelledSubscriber<>(); Subscription subscription = mock(Subscription.class); subscriber.onSubscribe(subscription); verify(subscription).cancel(); verify(subscription, never()).request(anyLong()); subscriber.onNext(1); subscriber.onComplete(); subscriber.onError(new Exception("boom")); assertThatThrownBy(() -> subscriber.onSubscribe(null)) .isInstanceOf(NullPointerException.class); }