Skip to content

Commit

Permalink
test: improve test stability
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Dec 17, 2024
1 parent 53c99cd commit 2ae696e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.foldright.aspect_test

import io.foldright.cffu.Cffu
import io.foldright.cffu.CffuFactory
import io.foldright.cffu.CompletableFutureUtils
import io.foldright.test_utils.*
import io.kotest.core.spec.style.AnnotationSpec
import io.kotest.inspectors.forAll
import org.junit.jupiter.api.Test
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletableFuture.completedFuture
import java.util.concurrent.CompletionStage
Expand All @@ -16,7 +17,10 @@ import java.util.function.Supplier
* Check the runtime type of return value of all method that returned CompletableFuture/Cffu.
*/
@Suppress("MoveLambdaOutsideParentheses")
private class CheckMinStageRuntimeTypeTests {
class CheckMinStageRuntimeTypeTests : AnnotationSpec() {
private val testExecutor = createThreadPool("CheckMinStageRuntimeTypeTests", queueCapacity = 1000_000)
private val testCffuFac = CffuFactory.builder(testExecutor).build();

private val cfThis: CompletableFuture<String> = completedFuture("cf this")
private val csThis: CompletionStage<String> = CompletableFutureUtils.completedStage("cs this")
private val cffuThis: Cffu<String> = testCffuFac.completedFuture("cffu this")
Expand Down Expand Up @@ -946,4 +950,11 @@ private class CheckMinStageRuntimeTypeTests {
// Casting CompletionStage instances to Cffu is not normal/public API usage!!
minCffuThis.newIncompleteFuture<Int>().shouldNotBeMinimalStage()
}

@AfterEach
fun afterEach() {
println("executor info of CheckMinStageRuntimeTypeTests:")
println("testExecutor: $testExecutor")
println("testFjExecutor: $testFjExecutor")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package io.foldright.test_utils
import io.foldright.cffu.CffuFactory
import io.kotest.assertions.fail
import io.kotest.core.config.AbstractProjectConfig
import io.kotest.core.listeners.AfterProjectListener
import io.kotest.core.listeners.BeforeProjectListener
import io.kotest.matchers.booleans.shouldBeFalse
import io.kotest.matchers.booleans.shouldBeTrue
Expand All @@ -19,7 +20,7 @@ import kotlin.random.nextULong
////////////////////////////////////////////////////////////////////////////////

@JvmField
val THREAD_COUNT_OF_POOL: Int = (Runtime.getRuntime().availableProcessors() * 3).coerceAtLeast(8).coerceAtMost(20)
val THREAD_COUNT_OF_POOL: Int = (Runtime.getRuntime().availableProcessors() * 4).coerceAtLeast(8).coerceAtMost(30)

@JvmField
val testExecutor = createThreadPool("ThreadPoolForCffuTesting")
Expand All @@ -31,15 +32,21 @@ val testCffuFac: CffuFactory = CffuFactory.builder(testExecutor).build()
val testFjExecutor = createThreadPool("ForkJoinPoolForCffuTesting", true)

@JvmOverloads
fun createThreadPool(threadNamePrefix: String, isForkJoin: Boolean = false): ExecutorService {
fun createThreadPool(
threadNamePrefix: String,
isForkJoin: Boolean = false,
queueCapacity: Int? = null
): ExecutorService {
val counter = AtomicLong()
val prefix = "${threadNamePrefix}_${Random.nextULong()}_"

val qc = queueCapacity ?: (THREAD_COUNT_OF_POOL * 500).coerceAtLeast(5000);

val executorService = if (!isForkJoin)
ThreadPoolExecutor(
/* corePoolSize = */ THREAD_COUNT_OF_POOL, /* maximumPoolSize = */ THREAD_COUNT_OF_POOL,
/* keepAliveTime = */ 2, TimeUnit.MINUTES,
/* workQueue = */ ArrayBlockingQueue((THREAD_COUNT_OF_POOL * 500).coerceAtLeast(5000))
/* keepAliveTime = */ 1, TimeUnit.MINUTES,
/* workQueue = */ ArrayBlockingQueue(qc)
) { r ->
Thread(r).apply {
name = "${prefix}${counter.getAndIncrement()}"
Expand All @@ -61,7 +68,7 @@ fun createThreadPool(threadNamePrefix: String, isForkJoin: Boolean = false): Exe
override fun unwrap(): ExecutorService = executorService

override fun toString(): String =
"test ${if (isForkJoin) "ForkJoinPool" else "ThreadPoolExecutor"} with thread name prefix `$prefix`"
"test ${if (isForkJoin) "ForkJoinPool" else "ThreadPoolExecutor"} with thread name prefix `$prefix`, $executorService"
}
}

Expand Down Expand Up @@ -147,7 +154,7 @@ fun shutdownExecutorService(vararg executors: ExecutorService) {
/**
* https://kotest.io/docs/framework/project-config.html
*/
object CffuKotestProjectConfig : AbstractProjectConfig(), BeforeProjectListener {
object CffuKotestProjectConfig : AbstractProjectConfig(), BeforeProjectListener, AfterProjectListener {
override suspend fun beforeProject() {
println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
println("call beforeProject")
Expand All @@ -161,4 +168,10 @@ object CffuKotestProjectConfig : AbstractProjectConfig(), BeforeProjectListener

warmupExecutorService(testExecutor, testFjExecutor)
}

override suspend fun afterProject() {
println("executor info afterProject:")
println("testExecutor: $testExecutor")
println("testFjExecutor: $testFjExecutor")
}
}

0 comments on commit 2ae696e

Please sign in to comment.