|
| 1 | +/** |
| 2 | + * Copyright 2013 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 | +package rx.operators; |
| 17 | + |
| 18 | +import static org.mockito.Matchers.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.mockito.InOrder; |
| 26 | + |
| 27 | +import rx.Observable; |
| 28 | +import rx.Observable.OnSubscribeFunc; |
| 29 | +import rx.Observer; |
| 30 | +import rx.Scheduler; |
| 31 | +import rx.Subscription; |
| 32 | +import rx.concurrency.Schedulers; |
| 33 | +import rx.concurrency.TestScheduler; |
| 34 | +import rx.subscriptions.Subscriptions; |
| 35 | +import rx.util.functions.Action0; |
| 36 | +import rx.util.functions.Func1; |
| 37 | + |
| 38 | +/** |
| 39 | + * Throttle by windowing a stream and returning the first value in each window. |
| 40 | + */ |
| 41 | +public final class OperationThrottleFirst { |
| 42 | + |
| 43 | + /** |
| 44 | + * Throttles to first value in each window. |
| 45 | + * |
| 46 | + * @param items |
| 47 | + * The {@link Observable} which is publishing events. |
| 48 | + * @param windowDuration |
| 49 | + * Duration of windows within with the first value will be chosen. |
| 50 | + * @param unit |
| 51 | + * The unit of time for the specified timeout. |
| 52 | + * @return A {@link Func1} which performs the throttle operation. |
| 53 | + */ |
| 54 | + public static <T> OnSubscribeFunc<T> throttleFirst(Observable<T> items, long windowDuration, TimeUnit unit) { |
| 55 | + return throttleFirst(items, windowDuration, unit, Schedulers.threadPoolForComputation()); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Throttles to first value in each window. |
| 60 | + * |
| 61 | + * @param items |
| 62 | + * The {@link Observable} which is publishing events. |
| 63 | + * @param windowDuration |
| 64 | + * Duration of windows within with the first value will be chosen. |
| 65 | + * @param unit |
| 66 | + * The unit of time for the specified timeout. |
| 67 | + * @param scheduler |
| 68 | + * The {@link Scheduler} to use internally to manage the timers which handle timeout for each event. |
| 69 | + * @return A {@link Func1} which performs the throttle operation. |
| 70 | + */ |
| 71 | + public static <T> OnSubscribeFunc<T> throttleFirst(final Observable<T> items, final long windowDuration, final TimeUnit unit, final Scheduler scheduler) { |
| 72 | + return new OnSubscribeFunc<T>() { |
| 73 | + @Override |
| 74 | + public Subscription onSubscribe(Observer<? super T> observer) { |
| 75 | + return items.window(windowDuration, unit, scheduler).flatMap(new Func1<Observable<T>, Observable<T>>() { |
| 76 | + |
| 77 | + @Override |
| 78 | + public Observable<T> call(Observable<T> o) { |
| 79 | + return o.takeFirst(); |
| 80 | + } |
| 81 | + }).subscribe(observer); |
| 82 | + } |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + public static class UnitTest { |
| 87 | + |
| 88 | + private TestScheduler scheduler; |
| 89 | + private Observer<String> observer; |
| 90 | + |
| 91 | + @Before |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + public void before() { |
| 94 | + scheduler = new TestScheduler(); |
| 95 | + observer = mock(Observer.class); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testThrottlingWithCompleted() { |
| 100 | + Observable<String> source = Observable.create(new OnSubscribeFunc<String>() { |
| 101 | + @Override |
| 102 | + public Subscription onSubscribe(Observer<? super String> observer) { |
| 103 | + publishNext(observer, 100, "one"); // publish as it's first |
| 104 | + publishNext(observer, 300, "two"); // skip as it's last within the first 400 |
| 105 | + publishNext(observer, 900, "three"); // publish |
| 106 | + publishNext(observer, 905, "four"); // skip |
| 107 | + publishCompleted(observer, 1000); // Should be published as soon as the timeout expires. |
| 108 | + |
| 109 | + return Subscriptions.empty(); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + Observable<String> sampled = Observable.create(OperationThrottleFirst.throttleFirst(source, 400, TimeUnit.MILLISECONDS, scheduler)); |
| 114 | + sampled.subscribe(observer); |
| 115 | + |
| 116 | + InOrder inOrder = inOrder(observer); |
| 117 | + |
| 118 | + scheduler.advanceTimeTo(1000, TimeUnit.MILLISECONDS); |
| 119 | + inOrder.verify(observer, times(1)).onNext("one"); |
| 120 | + inOrder.verify(observer, times(0)).onNext("two"); |
| 121 | + inOrder.verify(observer, times(1)).onNext("three"); |
| 122 | + inOrder.verify(observer, times(0)).onNext("four"); |
| 123 | + inOrder.verify(observer, times(1)).onCompleted(); |
| 124 | + inOrder.verifyNoMoreInteractions(); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testThrottlingWithError() { |
| 129 | + Observable<String> source = Observable.create(new OnSubscribeFunc<String>() { |
| 130 | + @Override |
| 131 | + public Subscription onSubscribe(Observer<? super String> observer) { |
| 132 | + Exception error = new TestException(); |
| 133 | + publishNext(observer, 100, "one"); // Should be published since it is first |
| 134 | + publishNext(observer, 200, "two"); // Should be skipped since onError will arrive before the timeout expires |
| 135 | + publishError(observer, 300, error); // Should be published as soon as the timeout expires. |
| 136 | + |
| 137 | + return Subscriptions.empty(); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + Observable<String> sampled = Observable.create(OperationThrottleFirst.throttleFirst(source, 400, TimeUnit.MILLISECONDS, scheduler)); |
| 142 | + sampled.subscribe(observer); |
| 143 | + |
| 144 | + InOrder inOrder = inOrder(observer); |
| 145 | + |
| 146 | + scheduler.advanceTimeTo(400, TimeUnit.MILLISECONDS); |
| 147 | + inOrder.verify(observer).onNext("one"); |
| 148 | + inOrder.verify(observer).onError(any(TestException.class)); |
| 149 | + inOrder.verifyNoMoreInteractions(); |
| 150 | + } |
| 151 | + |
| 152 | + private <T> void publishCompleted(final Observer<T> observer, long delay) { |
| 153 | + scheduler.schedule(new Action0() { |
| 154 | + @Override |
| 155 | + public void call() { |
| 156 | + observer.onCompleted(); |
| 157 | + } |
| 158 | + }, delay, TimeUnit.MILLISECONDS); |
| 159 | + } |
| 160 | + |
| 161 | + private <T> void publishError(final Observer<T> observer, long delay, final Exception error) { |
| 162 | + scheduler.schedule(new Action0() { |
| 163 | + @Override |
| 164 | + public void call() { |
| 165 | + observer.onError(error); |
| 166 | + } |
| 167 | + }, delay, TimeUnit.MILLISECONDS); |
| 168 | + } |
| 169 | + |
| 170 | + private <T> void publishNext(final Observer<T> observer, long delay, final T value) { |
| 171 | + scheduler.schedule(new Action0() { |
| 172 | + @Override |
| 173 | + public void call() { |
| 174 | + observer.onNext(value); |
| 175 | + } |
| 176 | + }, delay, TimeUnit.MILLISECONDS); |
| 177 | + } |
| 178 | + |
| 179 | + @SuppressWarnings("serial") |
| 180 | + private class TestException extends Exception { |
| 181 | + } |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments