react核心虚拟dom_使用虚拟时间测试基于时间的React堆核心流
react核心虛擬dom
Reactor Core實(shí)現(xiàn)了Reactive Streams規(guī)范,并處理了(可能無(wú)限的)數(shù)據(jù)流。 如果您感興趣,請(qǐng)查看它提供的出色文檔 。 在這里,我假設(shè)對(duì)Reactor Core庫(kù)的Flux和Mono類(lèi)型有一些基本的了解,并且將介紹Reactor Core提供了對(duì)時(shí)間本身的抽象,從而可以測(cè)試依賴(lài)于時(shí)間的函數(shù)。
對(duì)于某些Reactor核心運(yùn)營(yíng)商來(lái)說(shuō),時(shí)間是一個(gè)重要的考慮因素-例如,“間隔”功能的一種變體,它在初始“延遲” 10秒后每5秒發(fā)出一個(gè)遞增的數(shù)字:
val flux = Flux.interval(Duration.ofSeconds(10), Duration.ofSeconds(5)).take(3)根據(jù)正常時(shí)間流逝測(cè)試這樣的數(shù)據(jù)流將是可怕的,這樣的測(cè)試將花費(fèi)大約20秒才能完成。
Reactor-Core提供了一種解決方案,一種對(duì)時(shí)間本身的抽象-基于虛擬時(shí)間的調(diào)度程序,它提供了一種確定性的方式來(lái)測(cè)試這些類(lèi)型的操作的簡(jiǎn)潔方法。
讓我以?xún)煞N方式展示它,一種明確的方式應(yīng)該使基于虛擬時(shí)間的調(diào)度程序的動(dòng)作非常清晰,然后推薦使用Reactor Core進(jìn)行測(cè)試。
import org.assertj.core.api.Assertions.assertThat import org.junit.Test import reactor.core.publisher.Flux import reactor.test.scheduler.VirtualTimeScheduler import java.time.Duration import java.util.concurrent.CountDownLatchclass VirtualTimeTest {@Testfun testExplicit() {val mutableList = mutableListOf<Long>()val scheduler = VirtualTimeScheduler.getOrSet()val flux = Flux.interval(Duration.ofSeconds(10), Duration.ofSeconds(5), scheduler).take(3)val latch = CountDownLatch(1)flux.subscribe({ l -> mutableList.add(l) }, { _ -> }, { latch.countDown() })scheduler.advanceTimeBy(Duration.ofSeconds(10))assertThat(mutableList).containsExactly(0L)scheduler.advanceTimeBy(Duration.ofSeconds(5))assertThat(mutableList).containsExactly(0L, 1L)scheduler.advanceTimeBy(Duration.ofSeconds(5))assertThat(mutableList).containsExactly(0L, 1L, 2L)latch.await()}}1.首先,將“ Flux.interval”功能的計(jì)劃程序設(shè)置為基于虛擬時(shí)間的計(jì)劃程序。
2.預(yù)計(jì)在10秒延遲后每5秒發(fā)射一次數(shù)據(jù)流
3. VirtualTimeScheduler提供了一種“ advanceTimeBy”方法來(lái)將虛擬時(shí)間提前一個(gè)持續(xù)時(shí)間,因此該時(shí)間將首先提前10秒的延遲時(shí)間,屆時(shí)將發(fā)出第一個(gè)元素(0)。
4.然后將其前進(jìn)5秒鐘兩次,分別得到1和2。
這是確定性的,測(cè)試可以快速完成。 但是,此版本的測(cè)試很丑陋,它使用列表來(lái)收集和聲明結(jié)果,并使用CountDownLatch控制何時(shí)終止測(cè)試。 測(cè)試Reactor-Core類(lèi)型的一種更簡(jiǎn)潔的方法是使用出色的StepVerifier類(lèi),并且使用該類(lèi)的測(cè)試如下所示:
import org.junit.Test import reactor.core.publisher.Flux import reactor.test.StepVerifier import reactor.test.scheduler.VirtualTimeScheduler import java.time.Durationclass VirtualTimeTest {@Testfun testWithStepVerifier() {VirtualTimeScheduler.getOrSet()val flux = Flux.interval(Duration.ofSeconds(10), Duration.ofSeconds(5)).take(3)StepVerifier.withVirtualTime({ flux }).expectSubscription().thenAwait(Duration.ofSeconds(10)).expectNext(0).thenAwait(Duration.ofSeconds(5)).expectNext(1).thenAwait(Duration.ofSeconds(5)).expectNext(2).verifyComplete()}}使用StepVerifier進(jìn)行的這項(xiàng)新測(cè)試可以很好地理解每步前進(jìn)的時(shí)間,并斷言當(dāng)時(shí)的期望值。
翻譯自: https://www.javacodegeeks.com/2017/09/testing-time-based-reactor-core-streams-virtual-time.html
react核心虛擬dom
總結(jié)
以上是生活随笔為你收集整理的react核心虚拟dom_使用虚拟时间测试基于时间的React堆核心流的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 纹理和基元_通过粘性仙人掌基元进行延迟加
- 下一篇: jax-rs jax-ws_迟来总比没有