|
| 1 | +/** |
| 2 | + * Copyright 2015 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package rx.internal.util; |
| 18 | + |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | +import static org.junit.Assert.*; |
| 21 | + |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
| 24 | + |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +import rx.Observable; |
| 28 | +import rx.Subscriber; |
| 29 | +import rx.Subscription; |
| 30 | +import rx.schedulers.Schedulers; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test suite for {@link BlockingUtils}. |
| 34 | + */ |
| 35 | +public class BlockingUtilsTest { |
| 36 | + @Test |
| 37 | + public void awaitCompleteShouldReturnIfCountIsZero() { |
| 38 | + Subscription subscription = mock(Subscription.class); |
| 39 | + CountDownLatch latch = new CountDownLatch(0); |
| 40 | + BlockingUtils.awaitForComplete(latch, subscription); |
| 41 | + verifyZeroInteractions(subscription); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void awaitCompleteShouldReturnOnEmpty() { |
| 46 | + final CountDownLatch latch = new CountDownLatch(1); |
| 47 | + Subscriber<Object> subscription = createSubscription(latch); |
| 48 | + Observable<Object> observable = Observable.empty().subscribeOn(Schedulers.newThread()); |
| 49 | + observable.subscribe(subscription); |
| 50 | + BlockingUtils.awaitForComplete(latch, subscription); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void awaitCompleteShouldReturnOnError() { |
| 55 | + final CountDownLatch latch = new CountDownLatch(1); |
| 56 | + Subscriber<Object> subscription = createSubscription(latch); |
| 57 | + Observable<Object> observable = Observable.error(new RuntimeException()).subscribeOn(Schedulers.newThread()); |
| 58 | + observable.subscribe(subscription); |
| 59 | + BlockingUtils.awaitForComplete(latch, subscription); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void shouldThrowRuntimeExceptionOnThreadInterrupted() throws Exception { |
| 64 | + final CountDownLatch latch = new CountDownLatch(1); |
| 65 | + final Subscription subscription = mock(Subscription.class); |
| 66 | + final AtomicReference<Exception> caught = new AtomicReference<Exception>(); |
| 67 | + Thread thread = new Thread(new Runnable() { |
| 68 | + @Override |
| 69 | + public void run() { |
| 70 | + Thread.currentThread().interrupt(); |
| 71 | + try { |
| 72 | + BlockingUtils.awaitForComplete(latch, subscription); |
| 73 | + } catch (RuntimeException e) { |
| 74 | + caught.set(e); |
| 75 | + } |
| 76 | + } |
| 77 | + }); |
| 78 | + thread.run(); |
| 79 | + verify(subscription).unsubscribe(); |
| 80 | + Exception actual = caught.get(); |
| 81 | + assertNotNull(actual); |
| 82 | + assertNotNull(actual.getCause()); |
| 83 | + assertTrue(actual.getCause() instanceof InterruptedException); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + private static <T> Subscriber<T> createSubscription(final CountDownLatch latch) { |
| 88 | + return new Subscriber<T>() { |
| 89 | + @Override |
| 90 | + public void onNext(T t) { |
| 91 | + //no-oop |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onError(Throwable e) { |
| 96 | + latch.countDown(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void onCompleted() { |
| 101 | + latch.countDown(); |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | +} |
0 commit comments