Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extensions for virtual time testing #19

Merged
merged 2 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added extensions for virtual time testing
  • Loading branch information
mickeelm committed Dec 28, 2020
commit 9e6310980d02551e880d3352c603819405dbea5e
36 changes: 36 additions & 0 deletions src/main/kotlin/reactor/kotlin/test/StepVerifierExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

package reactor.kotlin.test

import org.reactivestreams.Publisher
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
import reactor.test.StepVerifier.Assertions
import reactor.test.StepVerifier.LastStep
import reactor.test.StepVerifierOptions
import reactor.test.scheduler.VirtualTimeScheduler
import java.time.Duration
import kotlin.reflect.KClass

Expand Down Expand Up @@ -128,4 +130,38 @@ fun <T> Mono<T>.test(n: Long): StepVerifier.FirstStep<T> = StepVerifier.create(t
*/
fun <T> Mono<T>.test(options: StepVerifierOptions): StepVerifier.FirstStep<T> = StepVerifier.create(this, options)

/**
* Extension for testing the supplied [Publisher] with [StepVerifier] API, using a [VirtualTimeScheduler].
*
* @see [StepVerifier.withVirtualTime]
* @author Mikael Elm
*/
fun <T> (() -> Publisher<T>).testUsingVirtualTime(): StepVerifier.FirstStep<T> =
StepVerifier.withVirtualTime { invoke() }

/**
* Extension for testing the supplied [Publisher] with [StepVerifier] API, using a [VirtualTimeScheduler].
*
* @see [StepVerifier.withVirtualTime]
* @author Mikael Elm
*/
fun <T> (() -> Publisher<T>).testUsingVirtualTime(n: Long): StepVerifier.FirstStep<T> =
StepVerifier.withVirtualTime({ invoke() }, n)

/**
* Extension for testing a [Publisher] with [StepVerifier] API, using a [VirtualTimeScheduler].
*
* @see [StepVerifier.withVirtualTime]
* @author Mikael Elm
*/
fun <T> Publisher<T>.testUsingVirtualTime(vtsLookup: () -> VirtualTimeScheduler, n: Long): StepVerifier.FirstStep<T> =
StepVerifier.withVirtualTime({ this }, vtsLookup, n)

/**
* Extension for testing a [Publisher] with [StepVerifier] API, using a [VirtualTimeScheduler].
*
* @see [StepVerifier.withVirtualTime]
* @author Mikael Elm
*/
fun <T> Publisher<T>.testUsingVirtualTime(options: StepVerifierOptions): StepVerifier.FirstStep<T> =
StepVerifier.withVirtualTime({ this }, options)
50 changes: 50 additions & 0 deletions src/test/kotlin/reactor/kotlin/test/StepVerifierExtensionsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
package reactor.kotlin.test

import org.junit.Test
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import reactor.test.StepVerifierOptions
import reactor.test.scheduler.VirtualTimeScheduler
import java.time.Duration

class StepVerifierExtensionsTests {

Expand Down Expand Up @@ -53,4 +58,49 @@ class StepVerifierExtensionsTests {
.verifyError<IllegalStateException>()
}

@Test
fun `testUsingVirtualTime()`() {
{ Mono.just("foo").delayElement(Duration.ofDays(1)) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe, if anything goes wrong, use Duration.ofMinutes(10) or something long enough that vts is significant, but if somehow it ends up using realtime the CI won't get stuck for too long?

.testUsingVirtualTime()
.thenAwait(Duration.ofDays(1))
.expectNext("foo")
.verifyComplete()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, each of these tests could use the .expectComplete().verify(Duration.ofSeconds(1)) style here, which would put a timeout on the whole verification

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's the better approach, so I decided to go with that one. As a bonus, the code is now indented in the same style as Sergei's tests above :)

}

@Test
fun `testUsingVirtualTime() requesting n elements`() {
{ Flux.just("foo", "bar").delayElements(Duration.ofDays(1)) }
.testUsingVirtualTime(1)
.thenAwait(Duration.ofDays(1))
.expectNext("foo")
.expectNoEvent(Duration.ofDays(7))
.thenRequest(1)
.thenAwait(Duration.ofDays(1))
.expectNext("bar")
.verifyComplete()
}

@Test
fun `testUsingVirtualTime() passing scheduler and requesting n elements`() {
val vts = VirtualTimeScheduler.create()
Flux.just("foo", "bar").delayElements(Duration.ofDays(1), vts)
.testUsingVirtualTime({vts}, 1)
.thenAwait(Duration.ofDays(1))
.expectNext("foo")
.expectNoEvent(Duration.ofDays(7))
.thenRequest(1)
.thenAwait(Duration.ofDays(1))
.expectNext("bar")
.verifyComplete()
}

@Test
fun `testUsingVirtualTime() passing options`() {
val vts = VirtualTimeScheduler.create()
Mono.just("foo").delayElement(Duration.ofDays(1), vts)
.testUsingVirtualTime(StepVerifierOptions.create().virtualTimeSchedulerSupplier { vts })
.thenAwait(Duration.ofDays(1))
.expectNext("foo")
.verifyComplete()
}
}