简体   繁体   English

RxJava2 Completable未按预期组合

[英]RxJava2 Completable does not combine as expected

My intention was to be able to pipeline a set amount of Completable however, it does not work as expected as seen from the following test case. 我的意图是能够传递一定数量的Completable,但是,如以下测试案例所示,它无法按预期工作。

@Test
public void test_and_then() {

    Completable c1 = Completable.fromAction(() -> {
        System.out.println("<1>");
    });

    Completable c2 = Completable.fromAction(() -> {
        System.out.println("<2.1>");
        boolean test = true;
        if (test) {
            throw new Exception();
        }
        System.out.println("<2.2>");
    });

    Completable c3 = Completable.fromAction(() -> {
        System.out.println("<3>");
    });


    Completable.complete()
            .andThen(c1)
            .andThen(c2)
            .andThen(c3)
            .test()
            .assertError(t -> true)
            .dispose();

    c3.test().assertNotSubscribed();
}

This results in the following output. 这将导致以下输出。

<1>
<2.1>
<3>
java.lang.AssertionError: Subscribed! (latch = 0, values = 0, errors = 0, completions = 1)

As seen in the documentation of andThen it looks like it should not subscribe to any Completables added later if an error is detected in previous Completable. andThe文档中可以看出,如果在先前的Completable中检测到错误它似乎不应订阅以后添加的任何Completable。 My problem is, why "<3>" is printed, basically, that Completable is run. 我的问题是,为什么打印“ <3>”,基本上是运行Completable。 How can I prevent this and simply end the chain of compietables instead? 我该如何防止这种情况,而只是结束可竞争者链? And why does it subscribe to the Completable c3 when the documentation indicates that this should not happen? 当文档指示不应发生这种情况时,为什么还要订阅Completable c3?

Actually, it works as you expected. 实际上,它按预期工作。 However, you are subscribing to c3 : 但是,您正在订阅c3

c3.test().assertNotSubscribed();

This will subscribe to c3 and print <3> . 这将订阅c3并打印<3>

If you remove that line, it'll work as you expect. 如果您删除该行,它将按预期工作。

test() subscribes and gives you a test observer that you can do assertions on. test()订阅,并为您提供可以对其进行断言的测试观察者。 It's just unfortunate the output order - it makes you think the stream is not stopped. 只是不幸的是输出顺序-它使您认为流没有停止。

Edit 编辑

Regarding the comment "what's the purpose of assertNotSubscribed if it always fails". 关于注释“如果assertNotSubscribed总是失败,它的目的是什么”。

This is quite a misconception to be honest. 说实话,这是一个误解。 test() is not the single way you have to create TestObserver s. test()不是创建TestObserver的唯一方法。 You can create a test observer like so 您可以像这样创建一个测试观察者

TestObserver<Void> testObserver = new TestObserver<>();

You can use assertNotSubscribed to make sure you haven't subscribed the test observer yet. 您可以使用assertNotSubscribed来确保尚未订阅测试观察者。 Actually, that method is supposed to check that onSubscribe hasn't been called. 实际上,该方法应该检查是否已调用onSubscribe It's not meant to test that dispose() was called. 这并不是要测试dispose()被调用。

For this scenario, you'd have to do something like assertTrue(testObserver.isDisposed()) . 在这种情况下,您必须执行assertTrue(testObserver.isDisposed()) You can follow the discussion here . 您可以在此处关注讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM