|
| 1 | +package com.baeldung.rxjava; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 5 | +import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Ignore; |
| 8 | +import org.junit.Test; |
| 9 | +import rx.Observable; |
| 10 | +import rx.Scheduler; |
| 11 | +import rx.observers.TestSubscriber; |
| 12 | +import rx.schedulers.Schedulers; |
| 13 | +import rx.schedulers.TestScheduler; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.ExecutorService; |
| 18 | +import java.util.concurrent.ThreadFactory; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +import static java.util.concurrent.Executors.newFixedThreadPool; |
| 22 | +import static org.hamcrest.Matchers.hasItems; |
| 23 | +import static org.junit.Assert.assertThat; |
| 24 | + |
| 25 | +public class SchedulersTest { |
| 26 | + String result = ""; |
| 27 | + String result1 = ""; |
| 28 | + String result2 = ""; |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenScheduledWorker_whenScheduleAnAction_thenResultAction() throws InterruptedException { |
| 32 | + System.out.println("scheduling"); |
| 33 | + Scheduler scheduler = Schedulers.immediate(); |
| 34 | + Scheduler.Worker worker = scheduler.createWorker(); |
| 35 | + worker.schedule(() -> result += "action"); |
| 36 | + Assert.assertTrue(result.equals("action")); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void givenScheduledWorker_whenUnsubscribeOnWorker_thenResultFirstAction() throws InterruptedException { |
| 41 | + System.out.println("canceling"); |
| 42 | + Scheduler scheduler = Schedulers.newThread(); |
| 43 | + Scheduler.Worker worker = scheduler.createWorker(); |
| 44 | + worker.schedule(() -> { |
| 45 | + result += "First_Action"; |
| 46 | + worker.unsubscribe(); |
| 47 | + }); |
| 48 | + worker.schedule(() -> result += "Second_Action"); |
| 49 | + Thread.sleep(500); |
| 50 | + Assert.assertTrue(result.equals("First_Action")); |
| 51 | + } |
| 52 | + |
| 53 | + @Ignore //it's not safe, not every time is running correctly |
| 54 | + @Test |
| 55 | + public void givenWorker_whenScheduledOnNewThread_thenResultIsBoundToNewThread() throws InterruptedException { |
| 56 | + System.out.println("newThread_1"); |
| 57 | + Scheduler scheduler = Schedulers.newThread(); |
| 58 | + Scheduler.Worker worker = scheduler.createWorker(); |
| 59 | + worker.schedule(() -> { |
| 60 | + result += Thread.currentThread().getName() + "_Start"; |
| 61 | + worker.schedule(() -> result += "_worker_"); |
| 62 | + result += "_End"; |
| 63 | + }); |
| 64 | + Thread.sleep(2000); |
| 65 | + Assert.assertTrue(result.equals("RxNewThreadScheduler-1_Start_End_worker_")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenObservable_whenObserveOnNewThread_thenRunOnDifferentThreadEachTime() throws InterruptedException { |
| 70 | + System.out.println("newThread_2"); |
| 71 | + Observable.just("Hello") |
| 72 | + .observeOn(Schedulers.newThread()) |
| 73 | + .doOnNext(s -> |
| 74 | + result2 += Thread.currentThread().getName() |
| 75 | + ) |
| 76 | + .observeOn(Schedulers.newThread()) |
| 77 | + .subscribe(s -> |
| 78 | + result1 += Thread.currentThread().getName() |
| 79 | + ); |
| 80 | + Thread.sleep(500); |
| 81 | + Assert.assertTrue(result1.equals("RxNewThreadScheduler-1")); |
| 82 | + Assert.assertTrue(result2.equals("RxNewThreadScheduler-2")); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void givenWorker_whenScheduledOnImmediate_thenResultIsBoundToThread() throws InterruptedException { |
| 87 | + System.out.println("immediate_1"); |
| 88 | + Scheduler scheduler = Schedulers.immediate(); |
| 89 | + Scheduler.Worker worker = scheduler.createWorker(); |
| 90 | + worker.schedule(() -> { |
| 91 | + result += Thread.currentThread().getName() + "_Start"; |
| 92 | + worker.schedule(() -> result += "_worker_"); |
| 93 | + result += "_End"; |
| 94 | + }); |
| 95 | + Thread.sleep(500); |
| 96 | + Assert.assertTrue(result.equals("main_Start_worker__End")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void givenObservable_whenImmediateScheduled_thenExecuteOnMainThread() throws InterruptedException { |
| 101 | + System.out.println("immediate_2"); |
| 102 | + Observable.just("Hello") |
| 103 | + .subscribeOn(Schedulers.immediate()) |
| 104 | + .subscribe(s -> |
| 105 | + result += Thread.currentThread().getName() |
| 106 | + ); |
| 107 | + Thread.sleep(500); |
| 108 | + Assert.assertTrue(result.equals("main")); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + @Test |
| 113 | + public void givenObservable_whenTrampolineScheduled_thenExecuteOnMainThread() throws InterruptedException { |
| 114 | + System.out.println("trampoline_1"); |
| 115 | + Observable.just(2, 4, 6, 8) |
| 116 | + .subscribeOn(Schedulers.trampoline()) |
| 117 | + .subscribe(i -> result += "" + i); |
| 118 | + Observable.just(1, 3, 5, 7, 9) |
| 119 | + .subscribeOn(Schedulers.trampoline()) |
| 120 | + .subscribe(i -> result += "" + i); |
| 121 | + Thread.sleep(500); |
| 122 | + Assert.assertTrue(result.equals("246813579")); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void givenWorker_whenScheduledOnTrampoline_thenComposeResultAsBlocking() throws InterruptedException { |
| 127 | + System.out.println("trampoline_2"); |
| 128 | + Scheduler scheduler = Schedulers.trampoline(); |
| 129 | + Scheduler.Worker worker = scheduler.createWorker(); |
| 130 | + worker.schedule(() -> { |
| 131 | + result += Thread.currentThread().getName() + "Start"; |
| 132 | + worker.schedule(() -> { |
| 133 | + result += "_middleStart"; |
| 134 | + worker.schedule(() -> |
| 135 | + result += "_worker_" |
| 136 | + ); |
| 137 | + result += "_middleEnd"; |
| 138 | + }); |
| 139 | + result += "_mainEnd"; |
| 140 | + }); |
| 141 | + Thread.sleep(500); |
| 142 | + Assert.assertTrue(result |
| 143 | + .equals("mainStart_mainEnd_middleStart_middleEnd_worker_")); |
| 144 | + } |
| 145 | + |
| 146 | + private ThreadFactory threadFactory(String pattern) { |
| 147 | + return new ThreadFactoryBuilder() |
| 148 | + .setNameFormat(pattern) |
| 149 | + .build(); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void givenExecutors_whenSchedulerFromCreatedExecutors_thenReturnElementsOnEacheThread() throws InterruptedException { |
| 154 | + System.out.println("from"); |
| 155 | + ExecutorService poolA = newFixedThreadPool(10, threadFactory("Sched-A-%d")); |
| 156 | + Scheduler schedulerA = Schedulers.from(poolA); |
| 157 | + ExecutorService poolB = newFixedThreadPool(10, threadFactory("Sched-B-%d")); |
| 158 | + Scheduler schedulerB = Schedulers.from(poolB); |
| 159 | + |
| 160 | + Observable<String> observable = Observable.create(subscriber -> { |
| 161 | + subscriber.onNext("Alfa"); |
| 162 | + subscriber.onNext("Beta"); |
| 163 | + subscriber.onCompleted(); |
| 164 | + });; |
| 165 | + |
| 166 | + observable |
| 167 | + .subscribeOn(schedulerA) |
| 168 | + .subscribeOn(schedulerB) |
| 169 | + .subscribe( |
| 170 | + x -> result += Thread.currentThread().getName() + x + "_", |
| 171 | + Throwable::printStackTrace, |
| 172 | + () -> result += "_Completed" |
| 173 | + ); |
| 174 | + Thread.sleep(2000); |
| 175 | + Assert.assertTrue(result.equals("Sched-A-0Alfa_Sched-A-0Beta__Completed")); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void givenObservable_whenIoScheduling_thenReturnThreadName() throws InterruptedException { |
| 180 | + System.out.println("io"); |
| 181 | + Observable.just("io") |
| 182 | + .subscribeOn(Schedulers.io()) |
| 183 | + .subscribe(i -> result += Thread.currentThread().getName()); |
| 184 | + Thread.sleep(500); |
| 185 | + Assert.assertTrue(result.equals("RxIoScheduler-2")); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void givenObservable_whenComputationScheduling_thenReturnThreadName() throws InterruptedException { |
| 190 | + System.out.println("computation"); |
| 191 | + Observable.just("computation") |
| 192 | + .subscribeOn(Schedulers.computation()) |
| 193 | + .subscribe(i -> result += Thread.currentThread().getName()); |
| 194 | + Thread.sleep(500); |
| 195 | + Assert.assertTrue(result.equals("RxComputationScheduler-1")); |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + public void givenLetters_whenTestScheduling_thenReturnValuesControllingAdvanceTime() throws InterruptedException { |
| 200 | + List<String> letters = Arrays.asList("A", "B", "C"); |
| 201 | + TestScheduler scheduler = Schedulers.test(); |
| 202 | + TestSubscriber<String> subscriber = new TestSubscriber<>(); |
| 203 | + |
| 204 | + Observable<Long> tick = Observable.interval(1, TimeUnit.SECONDS, scheduler); |
| 205 | + |
| 206 | + Observable.from(letters) |
| 207 | + .zipWith(tick, (string, index) -> index + "-" + string) |
| 208 | + .subscribeOn(scheduler) |
| 209 | + .subscribe(subscriber); |
| 210 | + |
| 211 | + subscriber.assertNoValues(); |
| 212 | + subscriber.assertNotCompleted(); |
| 213 | + |
| 214 | + scheduler.advanceTimeBy(1, TimeUnit.SECONDS); |
| 215 | + subscriber.assertNoErrors(); |
| 216 | + subscriber.assertValueCount(1); |
| 217 | + subscriber.assertValues("0-A"); |
| 218 | + |
| 219 | + scheduler.advanceTimeTo(3, TimeUnit.SECONDS); |
| 220 | + subscriber.assertCompleted(); |
| 221 | + subscriber.assertNoErrors(); |
| 222 | + subscriber.assertValueCount(3); |
| 223 | + assertThat(subscriber.getOnNextEvents(), hasItems("0-A", "1-B", "2-C")); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + public void givenLetters_whenDelay_thenReturne() throws InterruptedException { |
| 228 | + ExecutorService poolA = newFixedThreadPool(10, threadFactory("Sched1-")); |
| 229 | + Scheduler schedulerA = Schedulers.from(poolA); |
| 230 | + Observable.just('A', 'B') |
| 231 | + .delay(1, TimeUnit.SECONDS, schedulerA) |
| 232 | + .subscribe(i -> result+= Thread.currentThread().getName() + i + " "); |
| 233 | + |
| 234 | + Thread.sleep(2000); |
| 235 | + Assert.assertTrue(result.equals("Sched1-A Sched1-B ")); |
| 236 | + } |
| 237 | + |
| 238 | +} |
0 commit comments