Slide 1

Slide 1 text

Example of TestScheduler / TestSubject ( in Spock) Masayuki IZUMI - @izumin5210 kyobashi.dex #2

Slide 2

Slide 2 text

> Masayuki IZUMI a.k.a. @izumin5210 > Rekimoto Lab. at the Univ. of Tokyo > Strobo, Inc. / Wantedly, Inc. > Rubyist / Androider / {Java,Type}Scripter

Slide 3

Slide 3 text

DEB %SPJE,BJHJ %": 300.$

Slide 4

Slide 4 text

4DBO#-&EFWJDFT $IFDLBWBJMBCJMJUZPGTDBOOFEEFWJDFT

Slide 5

Slide 5 text

public Observable 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()); }

Slide 6

Slide 6 text

public Observable 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()); }

Slide 7

Slide 7 text

)PXEPXFXSJUFVOJUUFTU

Slide 8

Slide 8 text

5FTU4DIFEVMFS5FTU4VCKFDU BOE5FTU4VCTDSJCFS

Slide 9

Slide 9 text

public final Observable> buffer(long timespan, TimeUnit unit) { return buffer(timespan, unit, Integer.MAX_VALUE, Schedulers.computation()); } 0CTFSWBCMF5CVGGFS MPOH 5JNF6OJU VTFTDPNQVUBUJPOTDIFEVMFSJOEFGBVMU

Slide 10

Slide 10 text

public Observable 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()); }

Slide 11

Slide 11 text

public Observable 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()); }

Slide 12

Slide 12 text

given: def unit = TimeUnit.MILLISECONDS def scheduler = new TestScheduler() def subject = TestSubject.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)

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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(() -> ...));

Slide 21

Slide 21 text

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(() -> ...));

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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(() -> ...));

Slide 27

Slide 27 text

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(() -> ...));

Slide 28

Slide 28 text

5FTU4DIFEVMFS5FTU4VCKFDUBSFVTFGVMGPS UFTUJOHUJNFEFQFOEFOUDPEF *OUFSBDUJPOCBTFEUFTUJOHJO4QPDLJTBXFTPNF

Slide 29

Slide 29 text

Example of TestScheduler / TestSubject ( in Spock) Masayuki IZUMI - @izumin5210 kyobashi.dex #2