Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Example of TestScheduler and TestSubject (in Spock)

Example of TestScheduler and TestSubject (in Spock)

9eed44f137609e6ce3b6f1e14f80b9e1?s=128

Masayuki Izumi

January 22, 2016
Tweet

Transcript

  1. Example of TestScheduler / TestSubject ( in Spock) Masayuki IZUMI

    - @izumin5210 kyobashi.dex #2
  2. > Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at the

    Univ. of Tokyo > Strobo, Inc. / Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter
  3. DEB %SPJE,BJHJ %": 300.$

  4.  4DBO#-&EFWJDFT  $IFDLBWBJMBCJMJUZPGTDBOOFEEFWJDFT

  5. public Observable<ScanResult> scan(long timespan, TimeUnit unit) { return scanner.startScan() .filter(result

    -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> cache.removeAll().subscribe()); }
  6. public Observable<ScanResult> scan(long timespan, TimeUnit unit) { return scanner.startScan() .filter(result

    -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> cache.removeAll().subscribe()); }
  7. )PXEPXFXSJUFVOJUUFTU

  8. 5FTU4DIFEVMFS5FTU4VCKFDU BOE5FTU4VCTDSJCFS

  9. public final Observable<List<T>> buffer(long timespan, TimeUnit unit) { return buffer(timespan,

    unit, Integer.MAX_VALUE, Schedulers.computation()); } 0CTFSWBCMF5CVGGFS MPOH 5JNF6OJU VTFTDPNQVUBUJPOTDIFEVMFSJOEFGBVMU
  10. public Observable<ScanResult> scan(long timespan, TimeUnit unit, Scheduler scheduler) { return

    scanner.startScan() .filter(result -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit, scheduler) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> cache.removeAll().subscribe()); }
  11. public Observable<ScanResult> scan(long timespan, TimeUnit unit, Scheduler scheduler) { return

    scanner.startScan() .filter(result -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit, scheduler) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> cache.removeAll().subscribe()); }
  12. given: def unit = TimeUnit.MILLISECONDS def scheduler = new TestScheduler()

    def subject = TestSubject.<ScanResult>create(scheduler) def subscriber = new TestSubscriber() def subscription = repo.scan(3000, unit, schueduler) .subscribe(subscriber) // stubbing scanner.startScan() >> subject when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1 * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0) when: scheduler.advanceTimeBy(3000, unit) then: 1 * api.check({ it.size() == 1 }) >> Observable.just([...]) subscriber.assertValueCount(1)
  13. given: def unit = TimeUnit.MILLISECONDS def scheduler = new TestScheduler()

    def subject = TestSubject.<ScanResult>create(scheduler) def subscriber = new TestSubscriber() def subscription = repo.scan(3000, unit, schueduler) .subscribe(subscriber) // stubbing scanner.startScan() >> subject
  14. given: def unit = TimeUnit.MILLISECONDS def scheduler = new TestScheduler()

    def subject = TestSubject.<ScanResult>create(scheduler) def subscriber = new TestSubscriber() def subscription = repo.scan(3000, unit, schueduler) .subscribe(subscriber) // stubbing scanner.startScan() >> subject
  15. given: def unit = TimeUnit.MILLISECONDS def scheduler = new TestScheduler()

    def subject = TestSubject.<ScanResult>create(scheduler) def subscriber = new TestSubscriber() def subscription = repo.scan(3000, unit, schueduler) .subscribe(subscriber) // stubbing scanner.startScan() >> subject
  16. given: def unit = TimeUnit.MILLISECONDS def scheduler = new TestScheduler()

    def subject = TestSubject.<ScanResult>create(scheduler) def subscriber = new TestSubscriber() def subscription = repo.scan(3000, unit, schueduler) .subscribe(subscriber) // stubbing scanner.startScan() >> subject
  17. when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1

    * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0)
  18. when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1

    * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0)
  19. when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1

    * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0)
  20. when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1

    * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0) scanner.startScan() .filter(result -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit, scheduler) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> ...));
  21. when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1

    * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0) scanner.startScan() .filter(result -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit, scheduler) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> ...));
  22. when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1

    * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0) scanner.startScan() .filter(result -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit, scheduler) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> ...)); IBTOPUSFBDIFE
  23. when: scheduler.advanceTimeBy(3000, unit) then: 1 * api.check({ it.size() == 1

    }) >> Observable.just([...]) subscriber.assertValueCount(1)
  24. when: scheduler.advanceTimeBy(3000, unit) then: 1 * api.check({ it.size() == 1

    }) >> Observable.just([...]) subscriber.assertValueCount(1) DVSSFOUUJNFNTNT
  25. when: scheduler.advanceTimeBy(3000, unit) then: 1 * api.check({ it.size() == 1

    }) >> Observable.just([...]) subscriber.assertValueCount(1)
  26. when: scheduler.advanceTimeBy(3000, unit) then: 1 * api.check({ it.size() == 1

    }) >> Observable.just([...]) subscriber.assertValueCount(1) scanner.startScan() .filter(result -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit, scheduler) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> ...));
  27. when: scheduler.advanceTimeBy(3000, unit) then: 1 * api.check({ it.size() == 1

    }) >> Observable.just([...]) subscriber.assertValueCount(1) scanner.startScan() .filter(result -> !cache.isCached(result)) .flatMap(cache::put) .buffer(timespan, unit, scheduler) .filter(results -> !results.isEmpty()) .flatMap(this::checkAvailability) .doOnSubscribe(() -> ...));
  28.  5FTU4DIFEVMFS5FTU4VCKFDUBSFVTFGVMGPS   UFTUJOHUJNFEFQFOEFOUDPEF  *OUFSBDUJPOCBTFEUFTUJOHJO4QPDLJTBXFTPNF

  29. Example of TestScheduler / TestSubject ( in Spock) Masayuki IZUMI

    - @izumin5210 kyobashi.dex #2