【问题标题】:Uni subscription not running when using retry policy使用重试策略时,Uni 订阅未运行
【发布时间】:2021-12-22 07:03:41
【问题描述】:

我在 Quarkus 中使用 Mutiny 时遇到了一些奇怪的行为。

我的问题是我正在尝试将现有方法包装到 Uni 中,并且我希望此方法重试一定次数,如果它们都失败,我希望调用我的失败订阅,但不是。

为了更好地理解这一点,我为它写了一个测试:

@Test
void mutinySubscriptionNotCalledAfterRetry() {
    final AtomicBoolean executed = new AtomicBoolean(false);
    Uni.createFrom().item(this::error)
       .onFailure()
       .retry()
       .withBackOff(Duration.ofSeconds(1)).withJitter(0.2)
       .atMost(5)
       .subscribe()
       .with(success -> fail(),
             failure -> executed.set(true));
    assertTrue(executed.get()); // Failing statement
}

private boolean error() {
    throw new RuntimeException();
}

问题是失败订阅从未运行,我不知道我是否无法理解某些内容,但这似乎是根据 Clement 的操场的有效用例:

https://gist.github.com/cescoffier/e9abce907a1c3d05d70bea3dae6dc3d5

有人能解释一下吗?

非常感谢。

【问题讨论】:

    标签: java quarkus mutiny exponential-backoff


    【解决方案1】:

    用例完全有效,但您的测试编写不正确。这种重试是异步的。因此,执行断言时尚未调用失败回调 (failure -> execute(set(true));)。你需要等到它被调用。在您的情况下,大约需要 35 秒。

    要等待,您可以使用Awaitility 并执行以下操作:await().until(() -> executed.get());。你也可以使用io.smallrye.mutiny.helpers.test.UniAssertSubscriber:

    @Test
    void mutinySubscriptionNotCalledAfterRetry() {
      UniAssertSubscriber<Boolean> subscriber = 
        Uni.createFrom().item(this::error)              
           .onFailure().retry()
              .withBackOff(Duration.ofSeconds(1)).withJitter(0.2)
              .atMost(5)
           .subscribe().withSubscriber(new UniAssertSubscriber<>());
    
      subscriber.awaitFailure(Duration.ofSeconds(35));
    }
    
    private boolean error() {
        throw new RuntimeException("boom");
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 2023-02-06
      • 2021-07-02
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多