Reactive Programming with Rxjava-Chapter7:Testing and Troubleshooting(2)

Testing and Debugging

Virtual Time

Schedulers in Unit Testing

TestScheduler has two intriguing methods:advanceTimeBy() and advanceTimeTo() ,they are capable of advancing the time manually;otherwise,it`s frozen forever.

As you can see,TestScheduler is actually much more clever than an ordinary fake Clock abstraction.Not only do we have full control over current time,but we can also arbitrarily postpone all events. One caveat is that you must pass TestSchedule*r everywhere,basically to every operator that has an optional *Scheduler argument….From a testability point of view,you should prefer passing an explicit Scheduler.Moreover,consider dependency injection and provide *Scheduler*s from the outside.

Monitoring and Debugging

Every Observable has a set of callback methods that you can use to peek into various events,namely:

  • doOnCompleted
  • doOnEach()
  • doOnError()
  • doOnNext()
  • doOnRequest()
  • doOnSubscribe()
  • doOnTerminate()
  • doOnUnSubscribe()

Measuring and Monitoring

observable
    .flatMap(x ->
        makeNetworkCall(x)
            .doOnSubscribe(counter::inc)
            .doOnTerminate(counter::dec)
    )
    .subscribe( ... );
Observable<Long> external = //...

Timer timer = metricRegistry.timer("timer");

Observable<Long> externalWithTimer = Observable
        .defer(() ->Observable.just(timer.time()))
        .flatMap(timerCtx ->
                external.doOnCompleted(timerCtx::stop));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
At the moment, technology plays a major role in the success of businesses and in reaching out to more users as early as possible. This demand will increase day by day. Along with the increase in the demand, the user expectations also have increased. Now, the users are demanding a quick, more responsive, and reliable response. Reactive programming is a programming model that helps in tackling the essential complexity that comes with writing such applications. We, as Java developers, are very much familiar with the imperative style of programming; however, now to tackle the essential complexity, reactive programming uses declarative and functional paradigms to build the programs. This book aims at making the paradigm shift easily by discussing the concepts about functional programming in depth. This book begins with explaining what reactive programming and the Reactive Manifesto is, and about the Reactive Streams specification. It uses Java 9 to introduce the declarative and functional paradigm, which is very necessary to write programs in reactive style. It explains Java 9's Flow API, an adoption of the Reactive Streams specification. From this point on, it focuses on RxJava 2.0, covering topics like such as creating, transforming, filtering, combining, and testing Observables. It will then talk about how to use Java's popular framework, Spring, to build event-driven, reactive apps and its deployment on the server. By the end of the book, readers you will be fully equipped with the tools and techniques to implement robust event-driven reactive applications.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值