Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.sun.jna.platform.win32.Ole32
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory

internal interface WindowsComRuntime {
fun initializeSta(): Int
Expand All @@ -23,15 +24,21 @@ internal object JnaWindowsComRuntime : WindowsComRuntime {
}
}

internal object WindowsDialogThreadFactory : ThreadFactory {
override fun newThread(runnable: Runnable): Thread = Thread(runnable, THREAD_NAME).apply {
isDaemon = true
}

private const val THREAD_NAME = "FileKit-Windows-Dialog"
}

internal class WindowsDialogExecutor(
private val comRuntime: WindowsComRuntime,
threadFactory: ThreadFactory = WindowsDialogThreadFactory,
) : AutoCloseable {
private val dispatcher = Executors
.newSingleThreadExecutor { runnable ->
Thread(runnable, THREAD_NAME).apply {
isDaemon = true
}
}.asCoroutineDispatcher()
.newSingleThreadExecutor(threadFactory)
.asCoroutineDispatcher()

suspend fun <T> execute(block: () -> T): T = withContext(dispatcher) {
val initializationResult = comRuntime.initializeSta()
Expand All @@ -53,7 +60,6 @@ internal class WindowsDialogExecutor(
}

private companion object {
const val THREAD_NAME = "FileKit-Windows-Dialog"
const val S_OK = 0
const val S_FALSE = 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class WindowsDialogExecutorIntegrationTest {
}

assertNotEquals(callerThread, dialogThread)
assertEquals("FileKit-Windows-Dialog", dialogThread.name)
} finally {
dialogExecutor.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import java.util.concurrent.CountDownLatch
import java.util.concurrent.RejectedExecutionException
import java.util.concurrent.ThreadFactory
import java.util.concurrent.TimeUnit
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue

@Suppress("ktlint:standard:function-naming", "FunctionName")
Expand Down Expand Up @@ -49,15 +51,23 @@ class WindowsDialogExecutorTest {
}

@Test
fun WindowsDialogExecutor_execute_usesNamedDaemonThread() = runBlocking {
fun WindowsDialogThreadFactory_newThread_usesStableDaemonIdentity() {
val worker = WindowsDialogThreadFactory.newThread {}

assertEquals("FileKit-Windows-Dialog", worker.name)
assertTrue(worker.isDaemon)
}

@Test
fun WindowsDialogExecutor_execute_usesFactoryCreatedThread() = runBlocking {
val comRuntime = FakeWindowsComRuntime(initializationResult = S_OK)
val executor = WindowsDialogExecutor(comRuntime)
val threadFactory = RecordingThreadFactory(WindowsDialogThreadFactory)
val executor = WindowsDialogExecutor(comRuntime, threadFactory)

try {
val worker = executor.execute { Thread.currentThread() }

assertEquals("FileKit-Windows-Dialog", worker.name)
assertTrue(worker.isDaemon)
assertSame(threadFactory.createdThread, worker)
} finally {
executor.close()
}
Expand Down Expand Up @@ -182,6 +192,17 @@ class WindowsDialogExecutorTest {
}
}

private class RecordingThreadFactory(
private val delegate: ThreadFactory,
) : ThreadFactory {
lateinit var createdThread: Thread
private set

override fun newThread(runnable: Runnable): Thread = delegate.newThread(runnable).also {
createdThread = it
}
}

private class FakeWindowsComRuntime(
private val initializationResult: Int,
) : WindowsComRuntime {
Expand Down