|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package utils; |
| 17 | + |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.Mockito; |
| 23 | +import org.mockito.runners.MockitoJUnitRunner; |
| 24 | +import org.reactivestreams.Subscriber; |
| 25 | +import software.amazon.awssdk.core.pagination.async.AsyncPageFetcher; |
| 26 | +import software.amazon.awssdk.core.pagination.async.PaginatedItemsPublisher; |
| 27 | +import software.amazon.awssdk.utils.async.LimitingSubscriber; |
| 28 | +import software.amazon.awssdk.utils.internal.async.EmptySubscription; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.Iterator; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.ExecutionException; |
| 36 | +import java.util.concurrent.ExecutorService; |
| 37 | +import java.util.concurrent.Executors; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.TimeoutException; |
| 40 | +import java.util.function.Function; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | +import static org.mockito.Matchers.anyObject; |
| 44 | +import static org.mockito.Mockito.*; |
| 45 | + |
| 46 | +@RunWith(MockitoJUnitRunner.class) |
| 47 | +public class SdkSubscriberTest { |
| 48 | + |
| 49 | + public static final Function<Integer, Iterator<Integer>> SAMPLE_ITERATOR = response -> Arrays.asList(1, 2, 3, 4, 5, 6).listIterator(); |
| 50 | + public static final Function<Integer, Iterator<Integer>> EMPTY_ITERATOR = response -> new ArrayList<Integer>().listIterator(); |
| 51 | + @Mock |
| 52 | + AsyncPageFetcher asyncPageFetcher; |
| 53 | + PaginatedItemsPublisher<Integer, Integer> itemsPublisher; |
| 54 | + |
| 55 | + @Mock |
| 56 | + Subscriber<Integer> mockSubscriber; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setUp() { |
| 60 | + doReturn(CompletableFuture.completedFuture(1)) |
| 61 | + .when(asyncPageFetcher).nextPage(null); |
| 62 | + doReturn(false) |
| 63 | + .when(asyncPageFetcher).hasNextPage(anyObject()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void limitingSubscriber_with_different_limits() throws InterruptedException, ExecutionException, TimeoutException { |
| 68 | + itemsPublisher = PaginatedItemsPublisher.builder().nextPageFetcher(asyncPageFetcher) |
| 69 | + .iteratorFunction(SAMPLE_ITERATOR).isLastPage(false).build(); |
| 70 | + |
| 71 | + final List<Integer> belowLimit = new ArrayList<>(); |
| 72 | + itemsPublisher.limit(3).subscribe(e -> belowLimit.add(e)).get(5, TimeUnit.SECONDS); |
| 73 | + assertThat(belowLimit).isEqualTo(Arrays.asList(1, 2, 3)); |
| 74 | + |
| 75 | + final List<Integer> beyondLimit = new ArrayList<>(); |
| 76 | + itemsPublisher.limit(33).subscribe(e -> beyondLimit.add(e)).get(5, TimeUnit.SECONDS); |
| 77 | + assertThat(beyondLimit).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6)); |
| 78 | + |
| 79 | + final List<Integer> zeroLimit = new ArrayList<>(); |
| 80 | + itemsPublisher.limit(0).subscribe(e -> zeroLimit.add(e)).get(5, TimeUnit.SECONDS); |
| 81 | + assertThat(zeroLimit).isEqualTo(Arrays.asList()); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void filteringSubscriber_with_different_filters() throws InterruptedException, ExecutionException, TimeoutException { |
| 86 | + itemsPublisher = PaginatedItemsPublisher.builder().nextPageFetcher(asyncPageFetcher) |
| 87 | + .iteratorFunction(SAMPLE_ITERATOR).isLastPage(false).build(); |
| 88 | + |
| 89 | + final List<Integer> filteredSomeList = new ArrayList<>(); |
| 90 | + itemsPublisher.filter(i -> i % 2 == 0).subscribe(e -> filteredSomeList.add(e)).get(5, TimeUnit.SECONDS); |
| 91 | + assertThat(filteredSomeList).isEqualTo(Arrays.asList(2, 4, 6)); |
| 92 | + |
| 93 | + final List<Integer> filteredAllList = new ArrayList<>(); |
| 94 | + itemsPublisher.filter(i -> i % 10 == 0).subscribe(e -> filteredAllList.add(e)).get(5, TimeUnit.SECONDS); |
| 95 | + assertThat(filteredAllList).isEqualTo(Arrays.asList()); |
| 96 | + |
| 97 | + final List<Integer> filteredNone = new ArrayList<>(); |
| 98 | + itemsPublisher.filter(i -> i % 1 == 0).subscribe(e -> filteredNone.add(e)).get(5, TimeUnit.SECONDS); |
| 99 | + assertThat(filteredNone).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6)); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void limit_and_filter_subscriber_chained_with_different_conditions() throws InterruptedException, ExecutionException, TimeoutException { |
| 105 | + itemsPublisher = PaginatedItemsPublisher.builder().nextPageFetcher(asyncPageFetcher) |
| 106 | + .iteratorFunction(SAMPLE_ITERATOR).isLastPage(false).build(); |
| 107 | + |
| 108 | + final List<Integer> belowLimitWithFiltering = new ArrayList<>(); |
| 109 | + itemsPublisher.limit(4).filter(i -> i % 2 == 0).subscribe(e -> belowLimitWithFiltering.add(e)).get(5, TimeUnit.SECONDS); |
| 110 | + assertThat(belowLimitWithFiltering).isEqualTo(Arrays.asList(2, 4)); |
| 111 | + |
| 112 | + final List<Integer> beyondLimitWithAllFiltering = new ArrayList<>(); |
| 113 | + itemsPublisher.limit(33).filter(i -> i % 10 == 0).subscribe(e -> beyondLimitWithAllFiltering.add(e)).get(5, TimeUnit.SECONDS); |
| 114 | + assertThat(beyondLimitWithAllFiltering).isEqualTo(Arrays.asList()); |
| 115 | + |
| 116 | + final List<Integer> zeroLimitAndNoFilter = new ArrayList<>(); |
| 117 | + itemsPublisher.limit(0).filter(i -> i % 1 == 0).subscribe(e -> zeroLimitAndNoFilter.add(e)).get(5, TimeUnit.SECONDS); |
| 118 | + assertThat(zeroLimitAndNoFilter).isEqualTo(Arrays.asList()); |
| 119 | + |
| 120 | + final List<Integer> filteringbelowLimitWith = new ArrayList<>(); |
| 121 | + itemsPublisher.filter(i -> i % 2 == 0).limit(2).subscribe(e -> filteringbelowLimitWith.add(e)).get(5, TimeUnit.SECONDS); |
| 122 | + assertThat(filteringbelowLimitWith).isEqualTo(Arrays.asList(2, 4)); |
| 123 | + |
| 124 | + final List<Integer> filteringAndOutsideLimit = new ArrayList<>(); |
| 125 | + itemsPublisher.filter(i -> i % 10 == 0).limit(33).subscribe(e -> filteringAndOutsideLimit.add(e)).get(5, TimeUnit.SECONDS); |
| 126 | + assertThat(filteringAndOutsideLimit).isEqualTo(Arrays.asList()); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void limit__subscriber_with_empty_input_and_zero_limit() throws InterruptedException, ExecutionException, TimeoutException { |
| 131 | + itemsPublisher = PaginatedItemsPublisher.builder().nextPageFetcher(asyncPageFetcher) |
| 132 | + .iteratorFunction(EMPTY_ITERATOR).isLastPage(false).build(); |
| 133 | + |
| 134 | + final List<Integer> zeroLimit = new ArrayList<>(); |
| 135 | + itemsPublisher.limit(0).subscribe(e -> zeroLimit.add(e)).get(5, TimeUnit.SECONDS); |
| 136 | + assertThat(zeroLimit).isEqualTo(Arrays.asList()); |
| 137 | + |
| 138 | + final List<Integer> nonZeroLimit = new ArrayList<>(); |
| 139 | + itemsPublisher.limit(10).subscribe(e -> nonZeroLimit.add(e)).get(5, TimeUnit.SECONDS); |
| 140 | + assertThat(zeroLimit).isEqualTo(Arrays.asList()); |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + @Test |
| 145 | + public void limiting_subscriber_with_multiple_thread_publishers() throws InterruptedException { |
| 146 | + final int limitFactor = 5; |
| 147 | + LimitingSubscriber<Integer> limitingSubscriber = new LimitingSubscriber<>(mockSubscriber, limitFactor); |
| 148 | + limitingSubscriber.onSubscribe(new EmptySubscription(mockSubscriber)); |
| 149 | + final ExecutorService executorService = Executors.newFixedThreadPool(10); |
| 150 | + for (int i = 0; i < 10; i++) { |
| 151 | + final Integer integer = Integer.valueOf(i); |
| 152 | + executorService.submit(() -> limitingSubscriber.onNext(new Integer(integer))); |
| 153 | + } |
| 154 | + executorService.awaitTermination(300, TimeUnit.MILLISECONDS); |
| 155 | + Mockito.verify(mockSubscriber, times(limitFactor)).onNext(anyInt()); |
| 156 | + Mockito.verify(mockSubscriber).onComplete(); |
| 157 | + Mockito.verify(mockSubscriber).onSubscribe(anyObject()); |
| 158 | + Mockito.verify(mockSubscriber, never()).onError(anyObject()); |
| 159 | + } |
| 160 | +} |
0 commit comments