这是 Flux.cache(history, ttl) 中的错误吗? [英] Is this bug in Flux.cache(history, ttl)?

查看:69
本文介绍了这是 Flux.cache(history, ttl) 中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码

        StepVerifier.withVirtualTime((Supplier<Publisher<?>>) () -> Flux.just(1, 2, 3).cache(2, Duration.ofSeconds(10)))
            .thenAwait(Duration.ofSeconds(5))
            .expectNext(2, 3)
            .verifyComplete();

因异常而失败

java.lang.AssertionError: expectation "expectNext(2)" failed (expected value: 2; actual value: 1)

如果我将预期值更改为

.expectNext(1, 2, 3)

它会过去的.所以它不尊重cache方法中提供的history?

it will pass. So it does not respect the history provided in cache method?

推荐答案

缓存和 TTL 的测试有点棘手,因为当你缓存时,你想要测试的是 <代码>订阅者看到了.对于原始缓存的 Fluxcache 操作符只是传递.这第一遍是 StepVerifier 测试的(它执行了第一次订阅).

Caching and TTL are a bit more tricky to test, because when you cache, what you want to test is what a second Subscriber sees. For the original cached Flux, the cache operator is just pass-through. This first pass is what StepVerifier tested (it performed the first subscription).

为了解决这个问题,只需在供应商内部提取 Suppliersubscribe 即可:

In order to fix that, simply extract the Supplier and subscribe immediately inside the supplier:

Supplier<Flux<Integer>> supplier = () -> {
        Flux<Integer> tested = Flux.just(1, 2, 3)
                                   .cache(2, Duration.ofSeconds(10))
                                   .log();
        tested.subscribe();
        return tested;
    };

StepVerifier.withVirtualTime(supplier)
            .thenAwait(Duration.ofSeconds(5))
            .expectNext(2, 3)
            .verifyComplete();

这会测试是否遵守了缓存的历史记录限制.

This tests that the history limit of the cache is respected.

TTL 是另一回事.根据源的到达节奏",它不是缓存项目的到期供以后重播,而是是否缓存项目的条件.因此,它更依赖于排放延迟的来源.请参阅 中的测试FluxCacheTest 以及他们如何使用 delayElements 做到这一点.

The TTL is yet another thing. It is not an expiry on cached items for later replay but rather a condition on whether or not to cache an item, according to the arrival "rythm" of the source. So it is more dependent on the source having delays in its emissions. See the tests in FluxCacheTest and how they use delayElements for that.

这篇关于这是 Flux.cache(history, ttl) 中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆