Skip to content

Commit

Permalink
Remove checkAll from Racing tests, and use runBlocking on JVM. (#3468)
Browse files Browse the repository at this point in the history
Co-authored-by: Alejandro Serrano <trupill@gmail.com>
  • Loading branch information
nomisRev and serras authored Jul 8, 2024
1 parent 71b6d80 commit 49c3701
Show file tree
Hide file tree
Showing 28 changed files with 331 additions and 289 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlin.time.ExperimentalTime
class BracketCaseTest {
@Test
fun immediateAcquireBracketCaseFinishesSuccessfully() = runTest {
checkAll(Arb.int(), Arb.int()) { a, b ->
checkAll(10, Arb.int(), Arb.int()) { a, b ->
var once = true
bracketCase(
acquire = { a },
Expand All @@ -34,7 +34,7 @@ class BracketCaseTest {

@Test
fun suspendedAcquireBracketCaseFinishedSuccessfully() = runTest {
checkAll(Arb.int(), Arb.int()) { a, b ->
checkAll(10, Arb.int(), Arb.int()) { a, b ->
var once = true
bracketCase(
acquire = { a.suspend() },
Expand All @@ -49,7 +49,7 @@ class BracketCaseTest {

@Test
fun immediateErrorInAcquireStaysTheSameError() = runTest {
checkAll(Arb.throwable()) { e ->
checkAll(10, Arb.throwable()) { e ->
Either.catch {
bracketCase<Unit, Int>(
acquire = { throw e },
Expand All @@ -62,7 +62,7 @@ class BracketCaseTest {

@Test
fun suspendErrorInAcquireStaysTheSameError() = runTest {
checkAll(Arb.throwable()) { e ->
checkAll(10, Arb.throwable()) { e ->
Either.catch {
bracketCase<Unit, Int>(
acquire = { e.suspend() },
Expand All @@ -75,7 +75,7 @@ class BracketCaseTest {

@Test
fun immediateUseBracketCaseFinishedSuccessfully() = runTest {
checkAll(Arb.int(), Arb.int()) { a, b ->
checkAll(10, Arb.int(), Arb.int()) { a, b ->
var once = true
bracketCase(
acquire = { a },
Expand All @@ -90,7 +90,7 @@ class BracketCaseTest {

@Test
fun suspendedUseBracketCaseFinishesSuccessfully() = runTest {
checkAll(Arb.int(), Arb.int()) { a, b ->
checkAll(10, Arb.int(), Arb.int()) { a, b ->
var once = true
bracketCase(
acquire = { a },
Expand All @@ -105,7 +105,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustRunReleaseTaskOnUseImmediateError() = runTest {
checkAll(Arb.int(), Arb.throwable()) { i, e ->
checkAll(10, Arb.int(), Arb.throwable()) { i, e ->
val promise = CompletableDeferred<ExitCase>()

Either.catch {
Expand All @@ -124,7 +124,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustRunReleaseTaskOnUseSuspendedError() = runTest {
checkAll(Arb.int(), Arb.throwable()) { x, e ->
checkAll(10, Arb.int(), Arb.throwable()) { x, e ->
val promise = CompletableDeferred<Pair<Int, ExitCase>>()

Either.catch {
Expand All @@ -143,7 +143,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustAlwaysRunImmediateRelease() = runTest {
checkAll(Arb.int()) { x ->
checkAll(10, Arb.int()) { x ->
val promise = CompletableDeferred<Pair<Int, ExitCase>>()

Either.catch {
Expand All @@ -162,7 +162,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustAlwaysRunSuspendedRelease() = runTest {
checkAll(Arb.int()) { x ->
checkAll(10, Arb.int()) { x ->
val promise = CompletableDeferred<Pair<Int, ExitCase>>()

Either.catch {
Expand All @@ -182,7 +182,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustAlwaysRunImmediateReleaseError() = runTest {
checkAll(Arb.int(), Arb.throwable()) { n, e ->
checkAll(10, Arb.int(), Arb.throwable()) { n, e ->
Either.catch {
bracketCase(
acquire = { n },
Expand All @@ -195,7 +195,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustAlwaysRunSuspendedReleaseError() = runTest {
checkAll(Arb.int(), Arb.throwable()) { n, e ->
checkAll(10, Arb.int(), Arb.throwable()) { n, e ->
Either.catch {
bracketCase(
acquire = { n },
Expand All @@ -211,7 +211,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustComposeImmediateUseAndImmediateReleaseError() = runTest {
checkAll(Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
checkAll(10, Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
Either.catch {
bracketCase<Int, Unit>(
acquire = { n },
Expand All @@ -224,7 +224,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustComposeSuspendUseAndImmediateReleaseError() = runTest {
checkAll(Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
checkAll(10, Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
Either.catch {
bracketCase<Int, Unit>(
acquire = { n },
Expand All @@ -237,7 +237,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustComposeImmediateUseAndSuspendReleaseError() = runTest {
checkAll(Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
checkAll(10, Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
Either.catch {
bracketCase<Int, Unit>(
acquire = { n },
Expand All @@ -250,7 +250,7 @@ class BracketCaseTest {

@Test
fun bracketCaseMustComposeSuspendUseAndSuspendReleaseError() = runTest {
checkAll(Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
checkAll(10, Arb.int(), Arb.throwable(), Arb.throwable()) { n, e, e2 ->
Either.catch {
bracketCase<Int, Unit>(
acquire = { n },
Expand Down Expand Up @@ -338,7 +338,7 @@ class BracketCaseTest {

@Test
fun acquireOnBracketCaseIsNotCancellable() = runTest {
checkAll(Arb.int(), Arb.int()) { x, y ->
checkAll(10, Arb.int(), Arb.int()) { x, y ->
val mVar = Channel<Int>(1).apply { send(x) }
val latch = CompletableDeferred<Unit>()
val p = CompletableDeferred<ExitCase>()
Expand Down Expand Up @@ -367,7 +367,7 @@ class BracketCaseTest {

@Test
fun releaseOnBracketCaseIsNotCancellable() = runTest {
checkAll(Arb.int(), Arb.int()) { x, y ->
checkAll(10, Arb.int(), Arb.int()) { x, y ->
val mVar = Channel<Int>(1).apply { send(x) }
val latch = CompletableDeferred<Unit>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import kotlin.test.Test
class CountDownLatchSpec {
@Test
fun shouldRaiseAnExceptionWhenConstructedWithNegativeOrZeroCapacity() = runTest {
checkAll(Arb.long(Long.MIN_VALUE, 0)) { i ->
checkAll(10, Arb.long(Long.MIN_VALUE, 0)) { i ->
shouldThrow<IllegalArgumentException> { CountDownLatch(i) }.message shouldBe
"CountDownLatch must be constructed with positive non-zero initial count, but was $i"
}
}

@Test
fun releaseAndThenAwaitShouldComplete() = runTest {
checkAll(Arb.long(1, 20)) { count ->
checkAll(10, Arb.long(1, 20)) { count ->
val latch = CountDownLatch(count)
repeat(count.toInt()) { latch.countDown() }
latch.await() shouldBe Unit
Expand All @@ -36,7 +36,7 @@ class CountDownLatchSpec {

@Test
fun awaitAndThenReleaseShouldComplete() = runTest {
checkAll(Arb.long(1, 20)) { count ->
checkAll(10, Arb.long(1, 20)) { count ->
val latch = CountDownLatch(count)
val job = launch { latch.await() }
repeat(count.toInt()) { latch.countDown() }
Expand All @@ -46,7 +46,7 @@ class CountDownLatchSpec {

@Test
fun awaitWithMoreThanOneLatchUnreleasedShouldBlock() = runTest {
checkAll(Arb.long(1, 20)) { count ->
checkAll(10, Arb.long(1, 20)) { count ->
val latch = CountDownLatch(count)
repeat(count.toInt() - 1) { latch.countDown() }
withTimeoutOrNull(1) { latch.await() }.shouldBeNull()
Expand All @@ -56,7 +56,7 @@ class CountDownLatchSpec {

@Test
fun multipleAwaitsShouldAllComplete() = runTest {
checkAll(Arb.long(1, 20)) { count ->
checkAll(10, Arb.long(1, 20)) { count ->
val latch = CountDownLatch(count)
val jobs = (0 until count).map { launch { latch.await() } }
repeat(count.toInt()) { latch.countDown() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ import kotlin.test.Test
class CyclicBarrierSpec {
@Test
fun shouldRaiseAnExceptionWhenConstructedWithNegativeOrZeroCapacity() = runTest {
checkAll(Arb.int(Int.MIN_VALUE, 0)) { i ->
checkAll(10, Arb.int(Int.MIN_VALUE, 0)) { i ->
shouldThrow<IllegalArgumentException> { CyclicBarrier(i) }.message shouldBe
"Cyclic barrier must be constructed with positive non-zero capacity $i but was $i > 0"
}
}

@Test
fun barrierOfCapacity1IsANoOp() = runTest {
checkAll(Arb.constant(Unit)) {
checkAll(10, Arb.constant(Unit)) {
val barrier = CyclicBarrier(1)
barrier.await()
}
}

@Test
fun awaitingAllInParallelResumesAllCoroutines() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(1, 20)) { i ->
checkAll(10, Arb.int(1, 20)) { i ->
val barrier = CyclicBarrier(i)
(0 until i).parMap { barrier.await() }
}
}

@Test
fun shouldResetOnceFull() = runTestUsingDefaultDispatcher {
checkAll(Arb.constant(Unit)) {
checkAll(10, Arb.constant(Unit)) {
val barrier = CyclicBarrier(2)
parZip({ barrier.await() }, { barrier.await() }) { _, _ -> }
barrier.capacity shouldBe 2
Expand All @@ -60,7 +60,7 @@ class CyclicBarrierSpec {

@Test
fun awaitIsCancelable() = runTest {
checkAll(Arb.int(2, Int.MAX_VALUE)) { i ->
checkAll(10, Arb.int(2, Int.MAX_VALUE)) { i ->
val barrier = CyclicBarrier(i)
val exitCase = CompletableDeferred<ExitCase>()

Expand All @@ -77,15 +77,15 @@ class CyclicBarrierSpec {

@Test
fun shouldCleanUpUponCancellationOfAwait() = runTest {
checkAll(Arb.constant(Unit)) {
checkAll(10, Arb.constant(Unit)) {
val barrier = CyclicBarrier(2)
launch(start = CoroutineStart.UNDISPATCHED) { barrier.await() }.cancelAndJoin()
}
}

@Test
fun resetCancelsAllAwaiting() = runTest {
checkAll(Arb.int(2, 20)) { i ->
checkAll(10, Arb.int(2, 20)) { i ->
val barrier = CyclicBarrier(i)
val exitCase = CompletableDeferred<ExitCase>()

Expand All @@ -103,7 +103,7 @@ class CyclicBarrierSpec {

@Test
fun shouldCleanUpUponReset() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(2, 20)) { i ->
checkAll(10, Arb.int(2, 20)) { i ->
val barrier = CyclicBarrier(i)
val exitCase = CompletableDeferred<ExitCase>()

Expand All @@ -119,7 +119,7 @@ class CyclicBarrierSpec {

@Test
fun raceFiberCancelAndBarrierFull() = runTestUsingDefaultDispatcher {
checkAll(Arb.constant(Unit)) {
checkAll(10, Arb.constant(Unit)) {
val barrier = CyclicBarrier(2)
val job = launch(start = CoroutineStart.UNDISPATCHED) { barrier.await() }
when (raceN({ barrier.await() }, { job.cancelAndJoin() })) {
Expand All @@ -133,7 +133,7 @@ class CyclicBarrierSpec {

@Test
fun reset() = runTest {
checkAll(Arb.int(2..10)) { n ->
checkAll(10, Arb.int(2..10)) { n ->
val barrier = CyclicBarrier(n)

val exits = (0 until n - 1).map { CompletableDeferred<ExitCase>() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class FlowTest {

@Test
fun parMapConcurrentEqualOneMinusIdentity() = runTestUsingDefaultDispatcher {
checkAll(Arb.flow(Arb.int(), range = 1 .. 20)) { flow ->
checkAll(10, Arb.flow(Arb.int(), range = 1 .. 20)) { flow ->
flow.parMap(1) { it }
.toList() shouldBe flow.toList()
}
}

@Test
fun parMapRunsInParallel() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.int(1..2)) { i, n ->
checkAll(10, Arb.int(), Arb.int(1..2)) { i, n ->
val latch = CompletableDeferred<Int>()
flowOf(1, 2).parMap { index ->
if (index == n) latch.await()
Expand Down Expand Up @@ -77,7 +77,7 @@ class FlowTest {

@Test
fun parMapExceptionInParMapCancelsAllRunningTasks() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.throwable(), Arb.int(1..2)) { i, e, n ->
checkAll(10, Arb.int(), Arb.throwable(), Arb.int(1..2)) { i, e, n ->
val latch = CompletableDeferred<Unit>()
val exit = CompletableDeferred<Pair<Int, ExitCase>>()

Expand All @@ -104,7 +104,7 @@ class FlowTest {

@Test
fun parMapCancellingParMapCancelsAllRunningJobs() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.int()) { i, i2 ->
checkAll(10, Arb.int(), Arb.int()) { i, i2 ->
val latch = CompletableDeferred<Unit>()
val exitA = CompletableDeferred<Pair<Int, ExitCase>>()
val exitB = CompletableDeferred<Pair<Int, ExitCase>>()
Expand Down Expand Up @@ -136,15 +136,15 @@ class FlowTest {

@Test
fun parMapUnorderedConcurrentEqualOneMinusIdentity() = runTestUsingDefaultDispatcher {
checkAll(Arb.flow(Arb.int(), range = 1 .. 20)) { flow ->
checkAll(10, Arb.flow(Arb.int(), range = 1 .. 20)) { flow ->
flow.parMapUnordered(concurrency = 1) { it }
.toSet() shouldBe flow.toSet()
}
}

@Test
fun parMapUnorderedRunsInParallel() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.int(1..2)) { i, n ->
checkAll(10, Arb.int(), Arb.int(1..2)) { i, n ->
val latch = CompletableDeferred<Int>()
flowOf(1, 2).parMapUnordered { index ->
if (index == n) latch.await()
Expand Down Expand Up @@ -178,7 +178,7 @@ class FlowTest {

@Test
fun parMapUnorderedExceptionInParMapCancelsAllRunningTasks() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.throwable(), Arb.int(1..2)) { i, e, n ->
checkAll(10, Arb.int(), Arb.throwable(), Arb.int(1..2)) { i, e, n ->
val latch = CompletableDeferred<Unit>()
val exit = CompletableDeferred<Pair<Int, ExitCase>>()

Expand All @@ -205,7 +205,7 @@ class FlowTest {

@Test
fun parMapUnorderedCancellingParMapCancelsAllRunningJobs() = runTestUsingDefaultDispatcher {
checkAll(Arb.int(), Arb.int()) { i, i2 ->
checkAll(10, Arb.int(), Arb.int()) { i, i2 ->
val latch = CompletableDeferred<Unit>()
val exitA = CompletableDeferred<Pair<Int, ExitCase>>()
val exitB = CompletableDeferred<Pair<Int, ExitCase>>()
Expand Down Expand Up @@ -255,7 +255,7 @@ class FlowTest {

@Test @ExperimentalTime
fun fixedDelay() = runTest {
checkAll(Arb.long(10L .. 50L), Arb.int(3..20)) { waitPeriodInMillis, n ->
checkAll(10, Arb.long(10L .. 50L), Arb.int(3..20)) { waitPeriodInMillis, n ->
val waitPeriod = waitPeriodInMillis.milliseconds
val emissionDuration = (waitPeriodInMillis / 10L).milliseconds
var state: ComparableTimeMark? = null
Expand All @@ -281,7 +281,7 @@ class FlowTest {

@Test @ExperimentalTime
fun fixedRate() = runTest {
checkAll(Arb.long(10L..50L), Arb.int(3..20)) { waitPeriodInMillis, n ->
checkAll(10, Arb.long(10L..50L), Arb.int(3..20)) { waitPeriodInMillis, n ->
val waitPeriod = waitPeriodInMillis.milliseconds
val emissionDuration = (waitPeriodInMillis / 10L).milliseconds
var state: ComparableTimeMark? = null
Expand Down
Loading

0 comments on commit 49c3701

Please sign in to comment.