File: CommonPoolTest.kt

package info (click to toggle)
kotlinx-coroutines 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,628 kB
  • sloc: xml: 418; sh: 322; javascript: 60; makefile: 17; java: 8
file content (58 lines) | stat: -rw-r--r-- 2,165 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines

import kotlinx.coroutines.*
import org.junit.*
import org.junit.Assert.*
import java.lang.reflect.*
import java.util.concurrent.*

@Suppress("DEPRECATION")
class CommonPoolTest {
    private inline fun <T> Try(block: () -> T) = try { block() } catch (e: Throwable) { null }

    @Test
    fun testIsGoodCommonPool() {
        // Test only on JDKs that has all we need
        val fjpClass = Try { Class.forName("java.util.concurrent.ForkJoinPool") } ?: return
        val wtfClass = Try { Class.forName("java.util.concurrent.ForkJoinPool${'$'}ForkJoinWorkerThreadFactory") } ?: return
        val dwtfClass = Try { Class.forName("java.util.concurrent.ForkJoinPool${'$'}DefaultForkJoinWorkerThreadFactory") } ?: return
        // We need private constructor to create "broken" FJP instance
        val fjpCtor = Try { fjpClass.getDeclaredConstructor(
            Int::class.java,
            wtfClass,
            Thread.UncaughtExceptionHandler::class.java,
            Int::class.java,
            String::class.java
        ) } ?: return
        fjpCtor.isAccessible = true
        val dwtfCtor = Try { dwtfClass.getDeclaredConstructor() } ?: return
        dwtfCtor.isAccessible = true
        // Create bad pool
        val fjp0: ExecutorService = createFJP(0, fjpCtor, dwtfCtor) ?: return
        assertFalse(CommonPool.isGoodCommonPool(fjpClass, fjp0))
        fjp0.shutdown()
        // Create good pool
        val fjp1: ExecutorService = createFJP(1, fjpCtor, dwtfCtor) ?: return
        assertTrue(CommonPool.isGoodCommonPool(fjpClass, fjp1))
        fjp1.shutdown()
        println("CommonPool.isGoodCommonPool test passed")
    }

    private fun createFJP(
        parallelism: Int,
        fjpCtor: Constructor<out Any>,
        dwtfCtor: Constructor<out Any>
    ): ExecutorService? = Try {
        fjpCtor.newInstance(
            parallelism,
            dwtfCtor.newInstance(),
            Thread.getDefaultUncaughtExceptionHandler(),
            0,
            "Worker"
        )
    } as? ExecutorService
}