Skip to content

Commit 819b318

Browse files
Adopt Burst's new TestInterceptors (#1685)
Co-authored-by: Jesse Wilson <[email protected]>
1 parent e4a258a commit 819b318

File tree

20 files changed

+203
-206
lines changed

20 files changed

+203
-206
lines changed

gradle/libs.versions.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[versions]
2+
burst = "2.7.0"
23
jmh = "1.37"
34
kotlin = "2.2.10"
45
ktlint = "0.48.2"
@@ -10,7 +11,8 @@ androidx-test-ext-junit = { module = "androidx.test.ext:junit", version = "1.3.0
1011
androidx-test-runner = { module = "androidx.test:runner", version = "1.5.2" }
1112
binaryCompatibilityValidator = { module = "org.jetbrains.kotlinx.binary-compatibility-validator:org.jetbrains.kotlinx.binary-compatibility-validator.gradle.plugin", version = "0.18.1" }
1213
bnd = { module = "biz.aQute.bnd:biz.aQute.bnd.gradle", version = "7.1.0" }
13-
burst-gradle-plugin = { module = "app.cash.burst:burst-gradle-plugin", version = "2.7.0" }
14+
burst-gradle-plugin = { module = "app.cash.burst:burst-gradle-plugin", version.ref = "burst" }
15+
burst-runtime = { module = "app.cash.burst:burst", version.ref = "burst" }
1416
dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version = "2.0.0" }
1517
jmh-core = { module = "org.openjdk.jmh:jmh-core", version.ref = "jmh" }
1618
jmh-generator = { module = "org.openjdk.jmh:jmh-generator-annprocess", version.ref = "jmh" }

okio-nodefilesystem/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.jetbrains.kotlin.gradle.dsl.JsModuleKind
55

66
plugins {
77
kotlin("multiplatform")
8+
id("app.cash.burst")
89
id("org.jetbrains.dokka")
910
id("com.vanniktech.maven.publish.base")
1011
id("binary-compatibility-validator")

okio-testing-support/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
kotlin("multiplatform")
3+
id("app.cash.burst")
34
id("build-support")
45
}
56

@@ -17,6 +18,7 @@ kotlin {
1718
dependencies {
1819
api(projects.okio)
1920
api(libs.kotlin.test)
21+
api(libs.burst.runtime)
2022
}
2123
}
2224

okio-testing-support/src/commonMain/kotlin/okio/AbstractFileSystemTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package okio
1717

18-
import kotlin.test.BeforeTest
18+
import app.cash.burst.InterceptTest
1919
import kotlin.test.Ignore
2020
import kotlin.test.Test
2121
import kotlin.test.assertContentEquals
@@ -43,16 +43,14 @@ abstract class AbstractFileSystemTest(
4343
val closeBehavior: CloseBehavior,
4444
temporaryDirectory: Path,
4545
) {
46-
val base: Path = temporaryDirectory / "${this::class.simpleName}-${randomToken(16)}"
46+
@InterceptTest
47+
private val baseTestDirectory = TestDirectory(fileSystem, temporaryDirectory)
48+
protected val base: Path get() = baseTestDirectory.path
49+
4750
private val isNodeJsFileSystem = fileSystem::class.simpleName?.startsWith("NodeJs") ?: false
4851
private val isWasiFileSystem = fileSystem::class.simpleName?.startsWith("Wasi") ?: false
4952
private val isWrappingJimFileSystem = this::class.simpleName?.contains("JimFileSystem") ?: false
5053

51-
@BeforeTest
52-
fun setUp() {
53-
fileSystem.createDirectories(base)
54-
}
55-
5654
@Test
5755
fun doesNotExistsWithInvalidPathDoesNotThrow() {
5856
if (isNodeJsFileSystemOnWindows()) return
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2025 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package okio
17+
18+
import app.cash.burst.TestFunction
19+
import app.cash.burst.TestInterceptor
20+
21+
/**
22+
* A temporary directory on [fileSystem] that's usable for the current test.
23+
*/
24+
class TestDirectory(
25+
val fileSystem: FileSystem,
26+
val temporaryDirectory: Path = FileSystem.SYSTEM_TEMPORARY_DIRECTORY,
27+
) : TestInterceptor {
28+
lateinit var path: Path
29+
private set
30+
31+
override fun intercept(testFunction: TestFunction) {
32+
path = temporaryDirectory / "${testFunction.functionName}-${randomToken(16)}"
33+
fileSystem.createDirectories(path)
34+
testFunction()
35+
}
36+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (C) 2025 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package okio
17+
18+
import app.cash.burst.TestFunction
19+
import app.cash.burst.TestInterceptor
20+
import java.util.concurrent.Executors
21+
import java.util.concurrent.Future
22+
import java.util.concurrent.ScheduledExecutorService
23+
import java.util.concurrent.ScheduledFuture
24+
import java.util.concurrent.ThreadFactory
25+
import java.util.concurrent.TimeUnit
26+
import kotlin.time.Duration
27+
28+
/**
29+
* Manages a `ExecutorService` and shuts it down after the test.
30+
*/
31+
class TestExecutor(
32+
private val corePoolSize: Int = 0,
33+
) : TestInterceptor {
34+
lateinit var executorService: ScheduledExecutorService
35+
private set
36+
37+
fun <T> submit(task: () -> T): Future<T> = executorService.submit<T>(task)
38+
39+
fun <T> schedule(delay: Duration, command: () -> T): ScheduledFuture<T> =
40+
executorService.schedule(command, delay.inWholeNanoseconds, TimeUnit.NANOSECONDS)
41+
42+
override fun intercept(testFunction: TestFunction) {
43+
executorService = when {
44+
isLoom -> Executors.newScheduledThreadPool(corePoolSize, newVirtualThreadFactory())
45+
else -> Executors.newScheduledThreadPool(corePoolSize)
46+
}
47+
try {
48+
testFunction()
49+
} finally {
50+
executorService.shutdown()
51+
}
52+
}
53+
54+
private companion object {
55+
val isLoom = System.getProperty("loomEnabled").toBoolean()
56+
57+
fun newVirtualThreadFactory(): ThreadFactory {
58+
val threadBuilder = Thread::class.java.getMethod("ofVirtual").invoke(null)
59+
return Class.forName("java.lang.Thread\$Builder").getMethod("factory")
60+
.invoke(threadBuilder) as ThreadFactory
61+
}
62+
}
63+
}

okio-testing-support/src/jvmMain/kotlin/okio/TestingExecutors.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.

okio-wasifilesystem/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
// TODO: Restore Dokka once this issue is resolved.
88
// https://github.com/Kotlin/dokka/issues/3038
99
// id("org.jetbrains.dokka")
10+
id("app.cash.burst")
1011
id("com.vanniktech.maven.publish.base")
1112
id("build-support")
1213
id("binary-compatibility-validator")

okio-wasifilesystem/src/wasmWasiTest/kotlin/okio/WasiFileSystemPreopensTest.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package okio
1717

18-
import kotlin.test.BeforeTest
18+
import app.cash.burst.InterceptTest
1919
import kotlin.test.Test
2020
import kotlin.test.assertEquals
2121
import kotlin.test.assertFailsWith
@@ -29,15 +29,14 @@ import okio.Path.Companion.toPath
2929
*/
3030
class WasiFileSystemPreopensTest {
3131
private val fileSystem = WasiFileSystem
32-
private val testId = "${this::class.simpleName}-${randomToken(16)}"
33-
private val baseA: Path = "/a".toPath() / testId
34-
private val baseB: Path = "/b".toPath() / testId
3532

36-
@BeforeTest
37-
fun setUp() {
38-
fileSystem.createDirectory(baseA)
39-
fileSystem.createDirectory(baseB)
40-
}
33+
@InterceptTest
34+
private val testDirectoryA = TestDirectory(fileSystem, "/a".toPath())
35+
private val baseA: Path get() = testDirectoryA.path
36+
37+
@InterceptTest
38+
private val testDirectoryB = TestDirectory(fileSystem, "/b".toPath())
39+
private val baseB: Path get() = testDirectoryB.path
4140

4241
@Test
4342
fun operateOnPreopens() {
@@ -75,7 +74,7 @@ class WasiFileSystemPreopensTest {
7574

7675
@Test
7776
fun cannotOperateOutsideOfPreopens() {
78-
val noPreopen = "/c".toPath() / testId
77+
val noPreopen = "/c/absent".toPath()
7978
assertFailsWith<FileNotFoundException> {
8079
fileSystem.createDirectory(noPreopen)
8180
}

okio-wasifilesystem/src/wasmWasiTest/kotlin/okio/WasiTest.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package okio
1717

18-
import kotlin.test.BeforeTest
18+
import app.cash.burst.InterceptTest
1919
import kotlin.test.Test
2020
import kotlin.test.assertEquals
2121
import kotlin.test.assertFailsWith
@@ -25,12 +25,10 @@ import okio.Path.Companion.toPath
2525

2626
class WasiTest {
2727
private val fileSystem = WasiFileSystem
28-
private val base: Path = "/tmp".toPath() / "${this::class.simpleName}-${randomToken(16)}"
2928

30-
@BeforeTest
31-
fun setUp() {
32-
fileSystem.createDirectory(base)
33-
}
29+
@InterceptTest
30+
private val testDirectory = TestDirectory(fileSystem, "/tmp".toPath())
31+
private val base: Path get() = testDirectory.path
3432

3533
@Test
3634
fun createDirectory() {

0 commit comments

Comments
 (0)