Skip to content

Instantly share code, notes, and snippets.

@cvoronin
Created November 9, 2016 08:19
Show Gist options
  • Save cvoronin/8f54d503c4b94a4ea49cca10392a9778 to your computer and use it in GitHub Desktop.
Save cvoronin/8f54d503c4b94a4ea49cca10392a9778 to your computer and use it in GitHub Desktop.
Emit items with delay: 1 ... delay ... 2 ... delay ...
@Test
fun testEmitWithDelays() {
val DELAY = 500L
val COUNT = 5
val latch = CountDownLatch(1)
val startMoment = System.currentTimeMillis()
var endMoment : Long = 0
Observable
.range(0, COUNT)
.flatMap( { Observable.just(it).delay(DELAY, TimeUnit.MILLISECONDS) }, 1) // maxConcurrent = 1
.subscribe(
{ println("... value: $it, ${System.currentTimeMillis() - startMoment}") },
{},
{
endMoment = System.currentTimeMillis()
latch.countDown()
})
latch.await()
assertTrue { endMoment - startMoment >= DELAY * COUNT }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment