Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure close is called in all use cases #1190

Merged
merged 7 commits into from Jan 5, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions okio/src/commonMain/kotlin/okio/Okio.kt
Expand Up @@ -56,13 +56,13 @@ inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
result = block(this)
} catch (t: Throwable) {
thrown = t
}

try {
this?.close()
} catch (t: Throwable) {
if (thrown == null) thrown = t
else thrown.addSuppressed(t)
} finally {
try {
this?.close()
} catch (t: Throwable) {
if (thrown == null) thrown = t
else thrown.addSuppressed(t)
}
}

if (thrown != null) throw thrown
Expand Down
28 changes: 28 additions & 0 deletions okio/src/commonTest/kotlin/okio/UseTest.kt
@@ -0,0 +1,28 @@
package okio

import okio.Path.Companion.toPath
import okio.fakefilesystem.FakeFileSystem
import kotlin.test.Test

class UseTest {
val fakeFileSystem = FakeFileSystem(clock = FakeClock()).also { it.emulateUnix() }

val base = "/cache".toPath().also {
fakeFileSystem.createDirectories(it)
}

@Test
fun closesWithUseBlock() {
fun testMethodWithUse() {
val sink = fakeFileSystem.sink(base / "all-files-includes-file")

sink.use {
return@testMethodWithUse
}
}

testMethodWithUse()

fakeFileSystem.checkNoOpenFiles()
}
}