Коммит 7ef65de0 создал по автору Bruce Hamilton's avatar Bruce Hamilton Зафиксировано автором Bruce Hamilton
Просмотр файлов

KTOR-7963 Handle ClosedWatchServiceException

владелец 23eee5a9
......@@ -7,8 +7,10 @@ package io.ktor.tests.server.cio
import io.ktor.server.cio.*
import io.ktor.server.engine.*
import io.ktor.server.testing.suites.*
import kotlin.system.*
import kotlin.test.*
import kotlin.system.measureTimeMillis
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertTrue
class CIOCompressionTest : CompressionTestSuite<CIOApplicationEngine, CIOApplicationEngine.Configuration>(CIO) {
init {
......
......@@ -100,28 +100,10 @@ actual constructor(
return@read currentApplication
}
val changes = packageWatchKeys.flatMap { it.pollEvents() }
if (changes.isEmpty()) {
if (getFileChanges().isNullOrEmpty()) {
return@read currentApplication
}
environment.log.info("Changes in application detected.")
var count = changes.size
while (true) {
Thread.sleep(200)
val moreChanges = packageWatchKeys.flatMap { it.pollEvents() }
if (moreChanges.isEmpty()) {
break
}
environment.log.debug("Waiting for more changes.")
count += moreChanges.size
}
environment.log.debug("Changes to $count files caused application restart.")
changes.take(5).forEach { environment.log.debug("... ${it.context()}") }
applicationInstanceLock.write {
destroyApplication()
val (application, classLoader) = createApplication()
......@@ -132,6 +114,39 @@ actual constructor(
return@read applicationInstance ?: error("EmbeddedServer was stopped")
}
private fun getFileChanges(): List<WatchEvent<*>>? {
try {
val changes = packageWatchKeys.flatMap { it.pollEvents() }
if (changes.isEmpty()) {
return changes
}
environment.log.info("Changes in application detected.")
var count = changes.size
while (true) {
Thread.sleep(200)
val moreChanges = packageWatchKeys.flatMap { it.pollEvents() }
if (moreChanges.isEmpty()) {
break
}
environment.log.debug("Waiting for more changes.")
count += moreChanges.size
}
environment.log.debug("Changes to $count files caused application restart.")
changes.take(5).forEach { environment.log.debug("... {}", it.context()) }
return changes
} catch (e: InterruptedException) {
environment.log.debug("Watch service was interrupted", e)
return null
} catch (e: ClosedWatchServiceException) {
environment.log.debug("Watch service was closed", e)
return null
}
}
private fun createApplication(): Pair<Application, ClassLoader> {
val classLoader = createClassLoader()
val currentThread = Thread.currentThread()
......@@ -383,10 +398,7 @@ actual constructor(
}
private fun cleanupWatcher() {
try {
watcher?.close()
} catch (_: NoClassDefFoundError) {
}
runCatching { watcher?.close() }
}
}
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать