|
| 1 | +package rx.lang.scala.concurrency |
| 2 | + |
| 3 | +import rx.Subscription |
| 4 | +import java.util.Date |
| 5 | +import scala.concurrent.duration.Duration |
| 6 | +import rx.lang.scala.ImplicitFunctionConversions._ |
| 7 | +import rx.util.functions.Func2 |
| 8 | +import rx.lang.scala.Scheduler |
| 9 | +import rx.lang.scala.Observer |
| 10 | +import org.scalatest.junit.JUnitSuite |
| 11 | +import org.junit.Before |
| 12 | +import rx.lang.scala.Observable |
| 13 | + |
| 14 | + |
| 15 | +class GenericScheduler[+S <: rx.Scheduler](val asJava: S) extends AnyVal { |
| 16 | + /** |
| 17 | + * Schedules a cancelable action to be executed. |
| 18 | + * |
| 19 | + * @param state |
| 20 | + * State to pass into the action. |
| 21 | + * @param action |
| 22 | + * Action to schedule. |
| 23 | + * @return a subscription to be able to unsubscribe from action. |
| 24 | + */ |
| 25 | + def schedule[T](state: T, action: (Scheduler, T) => Subscription): Subscription = { |
| 26 | + asJava.schedule(state, action) |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Schedules a cancelable action to be executed in delayTime. |
| 31 | + * |
| 32 | + * @param state |
| 33 | + * State to pass into the action. |
| 34 | + * @param action |
| 35 | + * Action to schedule. |
| 36 | + * @param delayTime |
| 37 | + * Time the action is to be delayed before executing. |
| 38 | + * @param unit |
| 39 | + * Time unit of the delay time. |
| 40 | + * @return a subscription to be able to unsubscribe from action. |
| 41 | + */ |
| 42 | + def schedule[T](state: T, action: (Scheduler, T) => Subscription, delayTime: Duration): Subscription = { |
| 43 | + asJava.schedule(state, action, delayTime.length, delayTime.unit) |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Schedules a cancelable action to be executed periodically. |
| 48 | + * This default implementation schedules recursively and waits for actions to complete (instead of potentially executing |
| 49 | + * long-running actions concurrently). Each scheduler that can do periodic scheduling in a better way should override this. |
| 50 | + * |
| 51 | + * @param state |
| 52 | + * State to pass into the action. |
| 53 | + * @param action |
| 54 | + * The action to execute periodically. |
| 55 | + * @param initialDelay |
| 56 | + * Time to wait before executing the action for the first time. |
| 57 | + * @param period |
| 58 | + * The time interval to wait each time in between executing the action. |
| 59 | + * @return A subscription to be able to unsubscribe from action. |
| 60 | + */ |
| 61 | + def schedulePeriodically[T](state: T, action: (Scheduler, T) => Subscription, initialDelay: Duration, period: Duration): Subscription = { |
| 62 | + asJava.schedulePeriodically(state, action, initialDelay.length, initialDelay.unit.convert(period.length, period.unit), initialDelay.unit) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Schedules a cancelable action to be executed at dueTime. |
| 67 | + * |
| 68 | + * @param state |
| 69 | + * State to pass into the action. |
| 70 | + * @param action |
| 71 | + * Action to schedule. |
| 72 | + * @param dueTime |
| 73 | + * Time the action is to be executed. If in the past it will be executed immediately. |
| 74 | + * @return a subscription to be able to unsubscribe from action. |
| 75 | + */ |
| 76 | + def schedule[T](state: T, action: (Scheduler, T) => Subscription, dueTime: Date): Subscription = { |
| 77 | + asJava.schedule(state, action, dueTime) |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Schedules an action to be executed. |
| 82 | + * |
| 83 | + * @param action |
| 84 | + * action |
| 85 | + * @return a subscription to be able to unsubscribe from action. |
| 86 | + */ |
| 87 | + def schedule(action: () => Unit): Subscription = { |
| 88 | + asJava.schedule(action) |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Schedules an action to be executed in delayTime. |
| 93 | + * |
| 94 | + * @param action |
| 95 | + * action |
| 96 | + * @return a subscription to be able to unsubscribe from action. |
| 97 | + */ |
| 98 | + def schedule(action: () => Unit, delayTime: Duration): Subscription = { |
| 99 | + asJava.schedule(action, delayTime.length, delayTime.unit) |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Schedules an action to be executed periodically. |
| 104 | + * |
| 105 | + * @param action |
| 106 | + * The action to execute periodically. |
| 107 | + * @param initialDelay |
| 108 | + * Time to wait before executing the action for the first time. |
| 109 | + * @param period |
| 110 | + * The time interval to wait each time in between executing the action. |
| 111 | + * @return A subscription to be able to unsubscribe from action. |
| 112 | + */ |
| 113 | + def schedulePeriodically(action: () => Unit, initialDelay: Duration, period: Duration): Subscription = { |
| 114 | + asJava.schedulePeriodically(action, initialDelay.length, initialDelay.unit.convert(period.length, period.unit), initialDelay.unit) |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return the scheduler's notion of current absolute time in milliseconds. |
| 119 | + */ |
| 120 | + def now: Long = { |
| 121 | + asJava.now |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Parallelism available to a Scheduler. |
| 126 | + * |
| 127 | + * This defaults to {@code Runtime.getRuntime().availableProcessors()} but can be overridden for use cases such as scheduling work on a computer cluster. |
| 128 | + * |
| 129 | + * @return the scheduler's available degree of parallelism. |
| 130 | + */ |
| 131 | + def degreeOfParallelism: Int = { |
| 132 | + asJava.degreeOfParallelism |
| 133 | + } |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +class UnitTest extends JUnitSuite { |
| 138 | + import org.mockito.Matchers._ |
| 139 | + import org.mockito.Mockito._ |
| 140 | + import org.junit.Test |
| 141 | + import org.junit.Before |
| 142 | + import scala.concurrent.duration._ |
| 143 | + import scala.language.postfixOps |
| 144 | + |
| 145 | + var scheduler: TestScheduler = null |
| 146 | + var observer: Observer[Long] = null |
| 147 | + var observer2: Observer[Long] = null |
| 148 | + |
| 149 | + @Before def before() { |
| 150 | + scheduler = TestScheduler() |
| 151 | + observer = mock(classOf[rx.Observer[Long]]) |
| 152 | + observer2 = mock(classOf[rx.Observer[Long]]) |
| 153 | + } |
| 154 | + |
| 155 | + @Test def testInterval() { |
| 156 | + val w = Observable.interval(1 seconds) |
| 157 | + val sub = w.subscribe(observer) |
| 158 | + |
| 159 | + verify(observer, never()).onNext(0L) |
| 160 | + verify(observer, never()).onCompleted() |
| 161 | + verify(observer, never()).onError(any(classOf[Throwable])) |
| 162 | + |
| 163 | + scheduler.advanceTimeTo(2 seconds) |
| 164 | + |
| 165 | + val inOrdr = inOrder(observer); |
| 166 | + inOrdr.verify(observer, times(1)).onNext(0L) |
| 167 | + inOrdr.verify(observer, times(1)).onNext(1L) |
| 168 | + inOrdr.verify(observer, never()).onNext(2L) |
| 169 | + verify(observer, never()).onCompleted(); |
| 170 | + verify(observer, never()).onError(any(classOf[Throwable])) |
| 171 | + |
| 172 | + sub.unsubscribe(); |
| 173 | + scheduler.advanceTimeTo(4 seconds) |
| 174 | + verify(observer, never()).onNext(2L) |
| 175 | + verify(observer, times(1)).onCompleted() |
| 176 | + verify(observer, never()).onError(any(classOf[Throwable])) |
| 177 | + } |
| 178 | +} |
| 179 | + |
0 commit comments