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

Example of TestScheduler and TestSubject (in Sp...

Example of TestScheduler and TestSubject (in Spock)

Masayuki Izumi

January 22, 2016
Tweet

More Decks by Masayuki Izumi

Other Decks in Programming

Transcript

  1. > Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at the

    Univ. of Tokyo > Strobo, Inc. / Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter
  2. 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()); }
  3. 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()); }
  4. public final Observable<List<T>> buffer(long timespan, TimeUnit unit) { return buffer(timespan,

    unit, Integer.MAX_VALUE, Schedulers.computation()); } 0CTFSWBCMF5CVGGFS MPOH 5JNF6OJU VTFTDPNQVUBUJPOTDIFEVMFSJOEFGBVMU
  5. 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()); }
  6. 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()); }
  7. 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)
  8. 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
  9. 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
  10. 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
  11. 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
  12. when: subject.onNext(Mock(ScanResult)) scheduler.triggerActions() then: 1 * cache.isCached(_) >> false 1

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

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

    * cache.put(_) >> { Observable.just(it) } 0 * api._ subscriber.assertValueCount(0)
  15. 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(() -> ...));
  16. 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(() -> ...));
  17. 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
  18. when: scheduler.advanceTimeBy(3000, unit) then: 1 * api.check({ it.size() == 1

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

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

    }) >> Observable.just([...]) subscriber.assertValueCount(1)
  21. 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(() -> ...));
  22. 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(() -> ...));