Skip to content

Commit

Permalink
test: Attempt to fix InvalidWatchKeyException test
Browse files Browse the repository at this point in the history
  • Loading branch information
nhubbard committed Jun 6, 2024
1 parent ebe454e commit 6d55e02
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
9 changes: 6 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
## Medium Projects

* [ ] Figure out how to use newer versions of GraalVM on all platforms for the JS parser.
* [ ] Finish and test the comment writer functionality from PR 48.
* [ ] Add test coverage for any other code that isn't already covered.
* [ ] Write tests:
* [ ] PR #48 (write descriptions as comments in exported config values)
* [ ] File watcher on Windows/Linux (inc. `InvalidWatchKeyException`)
* [ ] Description of required, optional, and lazy config items
* [ ] Other random areas that aren't explicitly listed in this

## Large Projects

* [ ] Move all configuration format parsers to KotlinX Serialization instead of a mix of Jackson modules and various
third-party parsers.
* This would enable use of Konf on Kotlin Multiplatform projects!
* This could help enable use of Konf on Kotlin Multiplatform projects, but it would be extremely challenging.
8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ develocity {
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
// Shared versions
version("jackson", "2.17.1")
// WARNING!
// Don't upgrade the GraalVM dependency version!
// The newer versions have different coordinates and a bunch of unusual issues.
// There is no documented fix on non-Graal JDKs.
version("graal", "22.3.5")

// Gradle plugins
plugin("dokka", "org.jetbrains.dokka").version("1.9.20")
plugin("kover", "org.jetbrains.kotlinx.kover").version("0.8.0")
plugin("benchmark", "org.jetbrains.kotlinx.benchmark").version("0.4.10")
plugin("sonatype-publisher", "net.thebugmc.gradle.sonatype-central-portal-publisher").version("1.2.3")
plugin("solo-publisher", "ca.solo-studios.sonatype-publish").version("0.1.3")

// Dependencies
library("kotlinx-coroutines-core", "org.jetbrains.kotlinx", "kotlinx-coroutines-core").version("1.8.1")
library("reflections", "org.reflections", "reflections").version("0.10.2")
library("commons-text", "org.apache.commons", "commons-text").version("1.12.0")
Expand All @@ -69,11 +72,16 @@ dependencyResolutionManagement {
library("dom4j", "org.dom4j", "dom4j").version("2.1.4")
library("jaxen", "jaxen", "jaxen").version("2.0.0")
library("snakeyaml", "org.yaml", "snakeyaml").version("2.2")

// Test dependencies
library("junit-params", "org.junit.jupiter", "junit-jupiter-params").version("5.10.2")
library("spark", "com.sparkjava", "spark-core").version("2.9.4")
library("slf4j-simple", "org.slf4j", "slf4j-simple").version("2.0.13")

// Benchmark dependencies
library("kotlinx-benchmark-runtime", "org.jetbrains.kotlinx", "kotlinx-benchmark-runtime").version("0.4.10")

// Library bundles
bundle("jackson", listOf("jackson-core", "jackson-annotations", "jackson-databind", "jackson-kotlin", "jackson-jsr310"))
bundle("graal", listOf("graal-sdk", "graal-js"))
}
Expand Down
36 changes: 24 additions & 12 deletions src/test/kotlin/io/github/nhubbard/konf/TestInvalidWatchKey.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package io.github.nhubbard.konf

import io.github.nhubbard.konf.helpers.InvalidWatchKey
import org.junit.jupiter.api.AfterAll
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import java.nio.file.Paths
import java.util.concurrent.TimeUnit
import kotlin.io.path.deleteIfExists
import kotlin.io.path.writeText

Expand All @@ -34,24 +37,33 @@ class TestInvalidWatchKey {
| "second": "test_data"
|}
""".trimMargin()
private val inputFile = Paths.get("src/test/resources/invalid_watch.json")
private val inputFile = tempFileOf(input, suffix = ".json").toPath()

@BeforeAll
fun preTestSetup() {
inputFile.writeText(input)
}

@Test
fun testWatchKey_onBecomingInvalid_itThrows() {
// This test does not cover the correct branches on macOS
if ("mac" !in System.getProperty("os.name").lowercase()) {
// Write valid test data to input file
inputFile.writeText(input)
// Open valid data
Config {
val config = Config {
addSpec(InvalidWatchKey)
}.from.json.watchFile(inputFile.toFile())
// Overwrite the file with invalid data
inputFile.writeText("asofidmaposidfmpoasimdf")
}.from.json.watchFile(inputFile.toFile(), delayTime = 50, unit = TimeUnit.MILLISECONDS)
// Make watch key invalid???
inputFile.deleteIfExists()
// Wait for several seconds...
MainScope().launch {
delay(10000)
println(config[InvalidWatchKey.first])
println(config[InvalidWatchKey.second])
}
// Attempt to access invalid value???
config[InvalidWatchKey.second]
}
}

@AfterAll
fun postTestCleanup() {
inputFile.deleteIfExists()
}
}

0 comments on commit 6d55e02

Please sign in to comment.