Skip to content

Commit

Permalink
Fix problems with concurrency in tests, take 8
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Nov 9, 2023
1 parent d5df449 commit da9d42e
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

fun stackSafeIteration(): Int = when (platform) {
Platform.JVM -> 20_000
Expand All @@ -15,7 +17,10 @@ fun stackSafeIteration(): Int = when (platform) {

// The normal dispatcher with 'runTest' does some magic
// which doesn't go well with 'parZip', 'parMap', and 'raceN'
fun runTestUsingDefaultDispatcher(testBody: suspend TestScope.() -> Unit): TestResult = runTest {
fun runTestUsingDefaultDispatcher(
timeout: Duration = 10.seconds,
testBody: suspend TestScope.() -> Unit
): TestResult = runTest(timeout = timeout) {
withContext(Dispatchers.Default) {
testBody()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class ParZip2JvmTest {
@Test fun parZip2ReturnsToOriginalContext() = runTestUsingDefaultDispatcher {
Expand All @@ -35,7 +36,7 @@ class ParZip2JvmTest {
}
}

@Test fun parZip2ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip2ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip2"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(2, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java.util.concurrent.Executors
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class ParZip3JvmTest {
@Test fun parZip3ReturnsToOriginalContext() = runTestUsingDefaultDispatcher {
Expand All @@ -35,7 +36,7 @@ class ParZip3JvmTest {
}
}

@Test fun parZip3ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip3ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip3"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(3, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java.util.concurrent.Executors
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class ParZip4JvmTest {
@Test fun parZip4ReturnsToOriginalContext() = runTestUsingDefaultDispatcher {
Expand Down Expand Up @@ -41,7 +42,7 @@ class ParZip4JvmTest {
}
}

@Test fun parZip4ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip4ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip4"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(4, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class ParZip5JvmTest {
val threadName: suspend CoroutineScope.() -> String =
Expand Down Expand Up @@ -42,7 +43,7 @@ class ParZip5JvmTest {
}
}

@Test fun parZip5ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip5ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip5"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(5, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class ParZip6JvmTest {
val threadName: suspend CoroutineScope.() -> String =
Expand Down Expand Up @@ -45,7 +46,7 @@ class ParZip6JvmTest {
}
}

@Test fun parZip6ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip6ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip6"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(6, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class ParZip7JvmTest {
val threadName: suspend CoroutineScope.() -> String =
Expand Down Expand Up @@ -46,7 +47,7 @@ class ParZip7JvmTest {
}
}

@Test fun parZip7ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip7ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip7"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(7, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class ParZip8JvmTest {
val threadName: suspend CoroutineScope.() -> String =
Expand Down Expand Up @@ -47,7 +48,7 @@ class ParZip8JvmTest {

}

@Test fun parZip8ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip8ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip8"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(8, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import java.util.concurrent.Executors
import kotlin.time.Duration.Companion.seconds

class ParZip9JvmTest {
val threadName: suspend CoroutineScope.() -> String =
Expand Down Expand Up @@ -57,7 +58,7 @@ class ParZip9JvmTest {

}

@Test fun parZip9ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun parZip9ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val zipCtxName = "parZip9"
resourceScope {
val zipCtx = executor { Executors.newFixedThreadPool(9, NamedThreadFactory(zipCtxName)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.kotest.property.checkAll
import kotlinx.coroutines.awaitCancellation
import kotlinx.coroutines.withContext
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds

class RaceNJvmTest {
@Test fun race2ReturnsToOriginalContext() = runTestUsingDefaultDispatcher {
Expand All @@ -32,7 +33,7 @@ class RaceNJvmTest {
}
}

@Test fun race2ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun race2ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val racerName = "race2"

checkAll(Arb.int(1..2), Arb.throwable()) { choose, e ->
Expand Down Expand Up @@ -90,7 +91,7 @@ class RaceNJvmTest {
}
}

@Test fun race3ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher {
@Test fun race3ReturnsToOriginalContextOnFailure() = runTestUsingDefaultDispatcher(20.seconds) {
val racerName = "race3"

checkAll(Arb.int(1..3), Arb.throwable()) { choose, e ->
Expand Down

0 comments on commit da9d42e

Please sign in to comment.